From Clomosy Docs
No edit summary |
No edit summary |
||
| (4 intermediate revisions by 2 users not shown) | |||
| Line 20: | Line 20: | ||
In the above example, the unit <b>uLogin</b> is used within the "Main Code." This allows access to the functions and properties of the <b>uLogin</b> unit within the "Main Code."<br><br> | In the above example, the unit <b>uLogin</b> is used within the "Main Code." This allows access to the functions and properties of the <b>uLogin</b> unit within the "Main Code."<br><br> | ||
<div class="alert alert-success" role="alert" data-bs-theme="light"> | |||
For access to the "Main Code" unit from another created unit, it must be called using "uses Main".<br> | |||
<b> Example </b><br> | |||
<i> Unit: uLogin</i> | |||
<pre> | |||
uses Main; | |||
var | |||
... | |||
{ | |||
... | |||
} | |||
</pre> | |||
</div> | |||
<b>Importance:</b><br> | <b>Importance:</b><br> | ||
| Line 62: | Line 76: | ||
Num1 = 15; | Num1 = 15; | ||
Num2 = 25; | Num2 = 25; | ||
Sum = AddNumbers(Num1, Num2); | Sum = uCalculateTransaction.AddNumbers(Num1, Num2); | ||
ShowMessage('Total: '+ IntToStr(Sum)); | ShowMessage('Total: '+ IntToStr(Sum)); | ||
} | } | ||
| Line 122: | Line 136: | ||
Result = customButton; | Result = customButton; | ||
} | |||
</pre> | |||
<h2>Example 4: Value Sharing Between Two Units</h2><br> | |||
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".<br> | |||
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.<br> | |||
<i>Main Code:</i><br> | |||
<pre> | |||
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; | |||
} | |||
</pre> | |||
<i>uAddComponent:</i><br> | |||
<pre> | |||
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; | |||
} | |||
</pre> | |||
<i>uData:</i><br> | |||
<pre> | |||
var | |||
Number: Integer; | |||
void SetNumber(Value1: Integer); | |||
{ | |||
Number = Value1; | |||
ShowMessage('The value has been assigned to the Number variable.'); | |||
} | |||
</pre> | |||
<i>uDisplay:</i><br> | |||
<pre> | |||
uses uData; | |||
void ShowNumber; | |||
{ | |||
ShowMessage('Number: '+ IntToStr(uData.Number)); // Prints the value of the Number variable to the screen. | |||
} | } | ||
</pre> | </pre> | ||
| Line 129: | Line 222: | ||
* [[RunUnit | RunUnit]] | * [[RunUnit | RunUnit]] | ||
* [[TclUnit | TclUnit]] | * [[TclUnit | TclUnit]] | ||
{{#seo:|title=Uses Using in Clomosy - Clomosy Docs}} | |||
{{#seo:|description=Learn how the Uses clause in Clomosy organizes and shares functions and components between units for better code modularity and reusability.}} | |||
Latest revision as of 11:55, 20 February 2025
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.
}