From Clomosy Docs
keyword Uses(Uses Unit1 {Unit2, ...};
The uses clause is defined to specify which other units a unit will use. This clause is important for the organization and reusability of code. In a Clomosy program, there can be multiple units, and the uses clause is used to organize the interactions between these units.
Each unit is imported effectively—making all public or published routines and data within that unit accessible.
Usage Example:
The uses clause is placed at the beginning of a unit, followed by the names of the other units to be used, separated by commas.
Main Code:
uses uLogin;
var
...
{
...
}
In the above example, the unit uLogin is used within the "Main Code." This allows access to the functions and properties of the uLogin unit within the "Main Code."
For access to the "Main Code" unit from another created unit, it must be called using "uses Main".
Example
Unit: uLogin
uses Main;
var
...
{
...
}
Importance:
The uses clause allows your code to be modular and keeps specific functionalities in separate modules. This helps make the code more organized, easier to maintain, and reusable. Additionally, when you want to use a specific function or component, you only need to add the relevant unit to the uses clause.
Example 1: Procedure Usage
In the example, a procedure named "uCalculateTransaction" is defined in "MainCode" that takes two numbers, calculates their sum, and prints the result on the screen.
Main Code:
uses uCalculateTransaction;
var
value1, value2 : Integer;
{
value1 = 10;
value2 = 20;
uCalculateTransaction.AddNumbers(value1, value2);
}
uCalculateTransaction:
void AddNumbers(A, B: Integer);
var
Sum : Integer;
{
Sum = A + B;
ShowMessage('Total: '+ IntToStr(Sum));
}
Example 2: Function Usage
The function usage in the example above works as follows: two numbers are taken in the "MainCode" unit, sent to the "uCalculateTransaction" unit for their sum to be calculated, and the returned value is printed on the screen.
Main Code:
uses uCalculateTransaction;
var
Num1, Num2,Sum : Integer;
{
Num1 = 15;
Num2 = 25;
Sum = uCalculateTransaction.AddNumbers(Num1, Num2);
ShowMessage('Total: '+ IntToStr(Sum));
}
uCalculateTransaction:
function AddNumbers(A, B: Integer): Integer;
{
Result = A + B;
}
Example 3: Defining and Using Components
In the example, a form is created within the Main Code, and the TclLayout and TclButton functions defined in the uAddComponent unit are called within the Main Code to create two components.
Main Code:
uses uAddComponent;
var
Form1 : TclForm;
buton1 : TclButton;
layout1 : TclLayout;
void GetShow;
{
ShowMessage('Welcome!');
}
{
Form1 = TCLForm.Create(Self);
layout1 = uAddComponent.addLayout(Form1,'layout1');
buton1 = uAddComponent.addButton(Form1,layout1,'buton1','Show');
Form1.AddNewEvent(buton1,tbeOnClick,'GetShow');
Form1.Run;
}
uAddComponent:
function addLayout(AMyForm,ALayout):TclLayout;
var
customLayout: TclLayout;
{
customLayout = AMyForm.AddNewLayout(AMyForm,ALayout);
customLayout.Align = alCenter;
customLayout.Height = AMyForm.clHeight /2;
customLayout.Width = AMyForm.clWidth /2;
Result = customLayout;
}
function addButton(AMyForm,ALayout,AButton,ACaption):TclButon;
var
customButton : TclButton;
{
customButton = AMyForm.AddNewButton(ALayout,AButton,ACaption);
customButton.Align = alBottom;
customButton.Height = ALayout.Height / 4;
customButton.Margins.Bottom= 10;
Result = customButton;
}
Example 4: Value Sharing Between Two Units
Below is an example demonstrating the creation of two units (uData and uDisplay) and how to use a value obtained from one unit in the other within the "Main Code".
When the "uData" button is clicked, the value obtained in the "Main Code" is assigned to a variable in the "uData" unit. When the "uDisplay" button is clicked, it navigates to the "uDisplay" unit, where the value from the "uData" unit is displayed.
Main Code:
uses uAddComponent, uData, uDisplay;
var
Form1 : TclForm;
buton1,buton2 : TclButton;
layout1 : TclLayout;
void GetUData;
{
uData.SetNumber(42); // Setting the number using the SetNumber procedure in the uData unit.
}
void GetUDisplay;
{
uDisplay.ShowNumber;
}
{
Form1 = TCLForm.Create(Self);
layout1 = uAddComponent.addLayout(Form1,'layout1');
buton1 = uAddComponent.addButton(Form1,layout1,'buton1','uData');
buton2 = uAddComponent.addButton(Form1,layout1,'buton2','uDisplay');
Form1.AddNewEvent(buton1,tbeOnClick,'GetUData');
Form1.AddNewEvent(buton2,tbeOnClick,'GetUDisplay');
Form1.Run;
}
uAddComponent:
function addLayout(AMyForm,ALayout):TclLayout;
var
customLayout: TclLayout;
{
customLayout = AMyForm.AddNewLayout(AMyForm,ALayout);
customLayout.Align = alCenter;
customLayout.Height = AMyForm.clHeight /2;
customLayout.Width = AMyForm.clWidth /2;
Result = customLayout;
}
function addButton(AMyForm,ALayout,AButton,ACaption):TclButon;
var
customButton : TclButton;
{
customButton = AMyForm.AddNewButton(ALayout,AButton,ACaption);
customButton.Align = alBottom;
customButton.Height = ALayout.Height / 4;
customButton.Margins.Bottom= 10;
Result = customButton;
}
uData:
var
Number: Integer;
void SetNumber(Value1: Integer);
{
Number = Value1;
ShowMessage('The value has been assigned to the Number variable.');
}
uDisplay:
uses uData;
void ShowNumber;
{
ShowMessage('Number: '+ IntToStr(uData.Number)); // Prints the value of the Number variable to the screen.
}