Mega Code Archive

 
Categories / Delphi / Examples
 

Anatomy Of A Project 101

Title: Anatomy Of A Project 101 Question: In the "Anatomy Of A Project" series, I'm going to be going through all of the base pieces that make up your application. From the base unit code to enumerations to class composition and visibility, this will be a non-functional series, and will be more of a reference than a tutorial. Class 101 will focus on the code generated when you create a new form. Answer: unit Training; // The unit name consists only of the filename of the current unit. interface // The interface section contains all class, property, variable and // method declarations. uses // The interface uses clause defines unit methods that will be // referenced by the declarations AND the implementations. // There is a danger in this uses section of circular // references. A circular reference is when Unit1 // references Unit2 in the interface section, and Unit2 // references Unit1 in the interface section. Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs; type // The type clause contains all class declaractions in the current // unit. TfrmTraining = class( TForm ) // The form in question is automatically created as a class. // The type is the form component's Name property, with // a 'T' preceding it. In general, all forms are decended // from TForm, though it is possible to inherit from // any Object Pascal class. private // Private declarations will be visible only to the // class in which they reside (the form, in this case), // any decendants of the class, or to anything physically // implemented within this unit. public // Public declarations will be visible to any external units // or classes. end; var // The var clause in the interface section defines unit-level // variables. By default, the form is the only variable // declared, with the component name declared as the // previously defined class. It is considered bad form in // general to make your own variable declarations here, as // they would be globals. This is not to say that globals // are always a bad idea, and there are cases where // globals are desirable. In a future article, I will // discuss this. But for now, I will simply point out // that if you are using global variables or functions, // that you have a reason that you are not encapsulating // them into a class. frmTraining: TfrmTraining; implementation // The implementation section is where the actual implementation of // the previously defined classes will be written. If you have // written a class method in the interface section, you can press // CTRL-SHIFT-C to have the IDE automatically write the shell for // the method's implementation. In addition, you can // quickly navigate between a method's interface and // implementation by pressing CTRL-SHIFT-DOWN ARROW or // CTRL_SHIFT_UP ARROW. {$R *.DFM} // This is a compiler directive. We're not going to go into them // now, though I'll provide the short explanation that this // includes as resource files ($R) the file in the directory with // the unit name and a DFM extension (*.DFM). The DFM file stores // your form's design-time information and internal component // properties. end. // The "end." statement marks the end of the unit. No code beyond // this point will be valid.