Programming Using Classes
English · 日本語
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:
var
MyStringList: TStringList;
begin
MyStringList := TStringList.Create;
try
MyStringList.LoadFromFile('path_to_my_file.txt');
// ...
finally
MyStringList.Free;
end;
end;See Also
From the Free Pascal wiki, page "Programming Using Classes", as it stood on 2020-02-23 after a change by Trev, out of the wiki's dump of 2024-05-26. Copyright stays with the author who wrote it, according to the old wiki's Licensing page. If you have concerns about this material, message the admin.
Talk about this page
Nobody has opened a bench for this page yet. Sign in to open one.
