|
Delphi |
|
Unit is a source-code module used for constructing the program. Each unit must start from the unit name line, and the name must be unique within a project:
unit MainForm;
Each unit requires two parts: interface with public declarations and implementation for private declarations and code implementation. Each unit must have end. at the end of file:
unit MainForm;
interface
// public declarations
implementation
// private declarations and code implementation
end.
You can use two optional section after implementation section: initialization which can contains the code executed when program starts and finalization with code executed when program terminates. These section can, for instance, for creating and destroying some global objects, for loading and saving some program data, etc.:
unit Data;
interface
var
Data: string;
implementation
// some code here
initialization
// Data loading code here
finalization
// Data saving code here
end;
To use unit in your program or in another unit just use uses clause with list of used units. Both, interface and implementation sections can have own uses:
unit MainForm;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs;
...
implementation
uses Data;
...
end;
If you have several versions of some units in different folder on your disk, you can speciafy the absolute or relative file path:
uses Data in 'C:\MyProjects\Data\Data.pas', Service in '..\Service.pas';
|
|
C# |
|
Real files (assemblies) must be added to project by References section of Visual Studio solution. The using directives at the begin of file are used only for short notation:
System.Windows.Forms.Button btn;
can look like this with the using:
using System.Windows.Forms;
...
Button btn;
|
|
|
|
|
|
|