INTELLIGENT WORK FORUMS
FOR COMPUTER PROFESSIONALS

Log In

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips Forums!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!

*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

Jobs

Delphi - The Basics

What are the basics of using objects in Delphi? by djjd47130
Posted: 13 Dec 11

In this short tutorial, I'll talk about one of the most common practices in programming: Declaring and Using Objects. This lesson is for beginners to understand the basics of objects.

Summary

An object, as you probably already know, is the most raw type of class. It allows you to put together a unique collection of fields, properties, methods, etc. to be able to wrap common functionality into one single.. well.. object. It's practically the base class for most Delphi programming. Many people also tend to call a 'class' an 'object' quite often too.

More to be written in this FAQ to come!

Sample Code

We're going to create a new unit for this example. Within a Delphi project, go to File > New > Unit. Then copy this code below and replace whatever's in that nearly empty unit.

CODE

unit Unit2;

interface

uses
  Classes, Windows, SysUtils;

type
  TMyObject = class;
  TMyOtherObject = class;

  TSomeType = (stOne, stTwo, stThree);
  TSomeTypes = set of TSomeType;

  TMyObject = class(TObject)
  private
    FSomeData: String;
    FSomeTypes: TSomeTypes;
    FMyOtherObject: TMyOtherObject;
    procedure SetSomeData(const Value: String);
    procedure SetSomeTypes(const Value: TSomeTypes);
  protected
    { This is for another lesson }
  public
    constructor Create(SomeParam: String);
    destructor Destroy; override;
    property MyOtherObject: TMyOtherObject read FMyOtherObject;
  published
    property SomeData: String read FSomeData write SetSomeData;
    property SomeTypes: TSomeTypes read FSomeTypes write SetSomeTypes;
  end;

  TMyOtherObject = class(TObject)
  public
    SomeString: String;
    SomeInteger: Integer;
  end;

implementation

constructor TMyObject.Create(SomeParam: String);
begin
  //Create my other object
  FMyOtherObject:= TMyOtherObject.Create;
  //Set some param data
  FSomeData:= SomeParam;
  //Set some default values
  FSomeTypes:= [stOne, stTwo, stThree];
end;

destructor TMyObject.Destroy;
begin
  //Free my other object
  FMyOtherObject.Free;
end;

procedure TMyObject.SetSomeData(const Value: String);
begin
  FSomeData:= Value;
end;

procedure TMyObject.SetSomeTypes(const Value: TSomeTypes);
begin
  FSomeTypes:= Value;
end;

end.

Don't let the double declaration of 'TMyObject' fool you. It's a good practice to first declare just the name of your object right under the 'type' clause, then further down continue its structure. This allows you to be able to refer to that object in any other object you may have declared before it. In other words, if you have two objects, and you want to declare each of them within each other, then you have to first pre-declare those classes just below `type`.

Of course the 'published' section doesn't do much good in this example, because it's only really meant for components to show their properties in the Object Inspector (where the component's properties are set). But it still works perfect if you want to put a clean object together.

Using Objects

Now you want to use this new TMyObject from your form's unit. Simply declare your new unit's name ('Unit2' in this example) in the uses clause at the very top of the form's unit. In Delphi, unit names are equivalent to a Namespace.

CODE

unit Unit1;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, Unit2, StdCtrls;

type
  TForm1 = class(TForm)
    Edit1: TEdit;
    Button1: TButton;
    procedure FormCreate(Sender: TObject);
    procedure FormDestroy(Sender: TObject);
    procedure Button1Click(Sender: TObject);
  private
    //Declare object in form's class if it goes hand-in-hand with the form
    FMyObject: TMyObject;
  public
    property MyObject: TMyObject read FMyObject;
  end;

var
  Form1: TForm1;

implementation

{$R *.dfm}

procedure TForm1.FormCreate(Sender: TObject);
var
  O: TMyOtherObject;
begin
  //Next 3 lines are just a trick to show the form before anything is done...
  Show;
  BringToFront;
  Application.ProcessMessages;
  //Temporarily create/free an object to save memory...
  O:= TMyOtherObject.Create;
  try
    //Do something with 'O'
    O.SomeString:= 'This is some string';
    O.SomeInteger:= 2011;
  finally
    O.Free;
  end;
  //Create our new object...
  FMyObject:= TMyObject.Create('This is some sample parameter');
  //Set some default values...
  FMyObject.SomeTypes:= FMyObject.SomeTypes - [stThree];
  //Get something from our new object
  ShowMessage(MyObject.SomeData);
  Edit1.Text:= MyObject.SomeData;
  if stThree in MyObject.SomeTypes then begin
    ShowMessage('Three is selected!');
  end else begin
    ShowMessage('Three is NOT selected!');
  end;
end;

procedure TForm1.FormDestroy(Sender: TObject);
begin
  //Now we have to free our new object
  FMyObject.Free;
end;

procedure TForm1.Button1Click(Sender: TObject);
begin
  Close;
end;

end.

And right after you replace this code in your form, you need to immediately right-click the form, go to 'View as Text' and replace the 'DFM' code you see there with the code below. Then right-click the code and select 'View as Form'...

CODE

object Form1: TForm1
  Left = 444
  Top = 444
  Width = 313
  Height = 221
  Caption = 'My New Object Sample'
  Color = clBtnFace
  Font.Charset = DEFAULT_CHARSET
  Font.Color = clWindowText
  Font.Height = -11
  Font.Name = 'MS Sans Serif'
  Font.Style = []
  OldCreateOrder = False
  Position = poScreenCenter
  OnCreate = FormCreate
  OnDestroy = FormDestroy
  DesignSize = (
    297
    183)
  PixelsPerInch = 96
  TextHeight = 13
  object Edit1: TEdit
    Left = 24
    Top = 40
    Width = 249
    Height = 21
    Anchors = [akLeft, akTop, akRight]
    TabOrder = 0
  end
  object Button1: TButton
    Left = 80
    Top = 88
    Width = 121
    Height = 33
    Anchors = [akLeft, akTop, akRight]
    Caption = 'Close'
    Font.Charset = DEFAULT_CHARSET
    Font.Color = clWindowText
    Font.Height = -11
    Font.Name = 'MS Sans Serif'
    Font.Style = [fsBold]
    ParentFont = False
    TabOrder = 1
    OnClick = Button1Click
  end
end

Don't try to modify anything in this DFM, unless you really know what you're doing.

When the application starts, it will first show the form, then it will pop up a message with the default parameter that was passed into the object when it was created. After this message, another one will appear saying that Three is not selected. Looking at the end of the code in the form create procedure will show you how to use a 'set'.

I will write more on this topic and add more data types in the future, when I have more spare time.

PS - Just for the record, I wrote most of the code above directly into this website, copied it over to my Delphi 7, and it compiled perfectly :D
 

Back to Embarcadero: Delphi FAQ Index
Back to Embarcadero: Delphi Forum

My Archive

Resources

Close Box

Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close