Difference between revisions of "Programming Using Classes"

From Lazarus wiki
Jump to navigationJump to search
(New page: ==Using an Object Pascal Class== To use a class one can either call class methods or create an instance of the class and then use it's fields, properties and methods. The instance of an...)
 
Line 1: Line 1:
 
 
==Using an Object Pascal Class==
 
==Using an Object Pascal Class==
  
Line 21: Line 20:
 
end;
 
end;
 
</delphi>
 
</delphi>
 +
 +
==See Also==
 +
 +
*[[Object_Oriented_Programming_with_FreePascal_and_Lazarus]]

Revision as of 22:22, 17 November 2010

Using an Object Pascal Class

To use a class one can either call class methods or create an instance of the class and then use it's fields, properties and methods.

The instance of an Object Pascal class is commonly called "object", but don't confuse it with Object Pascal Objects. To create an instance of a class, one should call one of it's constructors. The constructor will return the instance of the class. Assign the returned instance to a variable in order to be able to use it and remember to free the instance later using the Free method. Descendents of TComponent can have an owner specified in their constructor. The instance will then be freed when the owner is freed and calling Free manually isn't necessary.

The following examples demonstrates the syntax that can be used to create a class and access it's methods:

<delphi> var

 MyStringList: TStringList;

begin

 MyStringList := TStringList.Create;
 try
   MyStringList.LoadFromFile('path_to_my_file.txt');
   // ...
 finally
   MyStringList.Free;
 end;

end; </delphi>

See Also