Delphi what is a unit




















Cyclic uses relations are instantly available and browsable. A Cyclic dependencies filter helps locating just those files that have cyclic uses relations with other units.

To help analyzing complex Cyclic uses relations the cycle can be viewed in a Dependency Structure Matrix. Skip to content Analyzing Unit dependencies is an important task in Delphi development: It helps managing coupling and re-usability on unit level.

Each unit comprises a set of interface definitions. Do not confuse these with class interfaces - the unit interface is the external interface to the module. It defines to any user of the unit what it contains. These interface definitions define private, protected, public and Published data, types and routine declarations.

Any routines defined in the interface must be implemented in the Implementation section. All function and procedure definitions must have exactly redefined arguments - see procedure and function information. Units and Scope In Pascal, units are the key to encapsulation and visibility, and they are probably even more important than the private and public keywords of a class. The scope of an identifier such as a variable, procedure, function, or a data type is the portion of the code in which the identifier is accessible.

You cannot use an identifier outside its scope. Here are some examples. Local variables: If you declare a variable within the block defining a routine or a method, you cannot use this variable outside that procedure.

The scope of the identifier spans the whole procedure, including nested routines unless an identifier with the same name in the nested routine hides the outer definition. The memory for this variable is allocated on the stack when the program executes the routine defining it. As soon as the routine terminates, the memory on the stack is automatically released.

Global hidden variables: If you declare an identifier in the implementation portion of a unit, you cannot use it outside the unit, but you can use it in any block and procedure defined within the unit.

The memory for this variable is allocated as soon as the program starts and exists until it terminates. You can use the initialization section of the unit to provide a specific initial value. Global variables: If you declare an identifier in the interface portion of the unit, its scope extends to any other unit that uses the one declaring it.

This variable uses memory and has the same lifetime as the previous group; the only difference is in its visibility. Any declarations in the interface portion of a unit are accessible from any part of the program that includes the unit in its uses clause. Variables of form classes are declared in the same way, so that you can refer to a form and its public fields, methods, properties, and components from the code of any other form.

Besides the obvious memory consumption problems, using global variables makes a program less easy to maintain and update. In short, you should use the smallest possible number of global variables. Units as Namespaces The uses statement is the standard technique to access the scope of another unit. At that point you can access the definitions of the unit. But it might happen that two units you refer to declare the same identifier; that is, you might have two classes or two routines with the same name.

In this case you can simply use the unit name to prefix the name of the type or routine defined in the unit.



0コメント

  • 1000 / 1000