?
Solved

Guidance about a CLASS formation....

Posted on 2005-05-09
4
Medium Priority
?
149 Views
Last Modified: 2010-04-05
Hello all

Need some more pro information about CLASS formation in general and about CONSTRUCTOR & DESTRUCTOR in more detail.
How to relate or interlink between CLASS that is in UNIT to a FORM.

I have some knowledge about it but I dont think its enough.

Code examples will help a lot.

Lior.
0
Comment
Question by:liorb
  • 2
4 Comments
 
LVL 5

Expert Comment

by:paulb1989
ID: 13963344
A classes constructor is what is called when the class is created, and the destructor when it is destroyed. I don't know exactly what you mean by linking with a form, but if you want to use a class from a form you simply have to create it and you can. If you mean building a component to go on a form then you need to descend the class from TComponent and implement a register procedure to add the component to the palette.
0
 
LVL 11

Accepted Solution

by:
pcsentinel earned 800 total points
ID: 13966038
Your not very specific in your request but here are some general notes

Say you wanted to create an object that contained a list of strings then you might have something like this in a unit

TPrintDef = class(TObject)
    FSt: TStringlist;
  published
    constructor Create;
    destructor destroy; override;
  public
   procedure AddString(Stri: String);
  end;

implementation

{ TPrintVars }

constructor TPrintDef.Create;
begin
  inherited;
  Fst:=TStringlist.Create;
end;

destructor TPrintDef.Destory;
begin
  FreeAndNil(FSt);
  inherited;
end;

procedure TPrintDef.AddString(Str: String);
begin
  FSt.Add(Str);
end;


This is very simple and just an example.

What we are doing is saying that we want the Stringlist object to exist for the life of
the TPrintDef object. So in the constructor we create the Stringlist, and in the destructor we Destroy it.

Now in you Project form you might set a form level variable called

PrintDef1: TPrintDef;

make sure you add the unit that contains the code for the TPrintDef into to the 1st uses clause on that form.


On the Form Create you would call

PrintDef1:=TPrintDef.Create;

this has then created a TPrintDef object called PrintDef1 and within PrintDef1 there is a TStringlist.

Hope this helps
0
 
LVL 1

Author Comment

by:liorb
ID: 13966103
pcsentinel
What the 'Inherited' word stands for?
0
 
LVL 11

Expert Comment

by:pcsentinel
ID: 13966200
the inherited calls the create procedure from the class you have inherited your custom class from

so in the example

TPrintDef is inherited from TObject

as in TPrintDef = class(TObject)

so when you add inherited in the create it calls TObject.Create before you add in the things that relate to your custom class

and when you add inherited in the destructor it calls the TObject destructor

You should generally always keep this when subclassing, i.e. deriving a class from another class.

you can also use this in  other procedures and functions where you are enhancing a function of the class you are deriving from.

So say you wanted a new TEdit control, but everytime the user typed a key you wanted to play a ping on the speaker

now TEdit is derived TCustomEdit
TCustomEdit is derived from TWinControl

in TWinControl the code is

function TWinControl.DoKeyUp(var Message: TWMKey): Boolean;
var
  ShiftState: TShiftState;
  Form: TCustomForm;
begin
  Result := True;
  Form := GetParentForm(Self);
  if (Form <> nil) and (Form <> Self) and Form.KeyPreview and
    TWinControl(Form).DoKeyUp(Message) then Exit;
  with Message do
  begin
    ShiftState := KeyDataToShiftState(KeyData);
    if not (csNoStdEvents in ControlStyle) then
    begin
      KeyUp(CharCode, ShiftState);
      if CharCode = 0 then Exit;
    end;
  end;
  Result := False;
end;


the actual code is not important to this example but bear with me

Your control is TMyEdit and that has a procedure

function TMyEdit.DoKeyUp(var Message: TWMKey): Boolean;
begin
  inherited
  Ping;
end;

so what happens is TMyEdit calls the inherited TEdit.DoKeyUp which in turn calls TCustomEdit.DoKeyUp which calls TWinControl.DoKeyUp which runs the above code

then your code does the ping



Hope this helps
0

Featured Post

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

A lot of questions regard threads in Delphi.   One of the more specific questions is how to show progress of the thread.   Updating a progressbar from inside a thread is a mistake. A solution to this would be to send a synchronized message to the…
In this tutorial I will show you how to use the Windows Speech API in Delphi. I will only cover basic functions such as text to speech and controlling the speed of the speech. SAPI Installation First you need to install the SAPI type library, th…
Despite its rising prevalence in the business world, "the cloud" is still misunderstood. Some companies still believe common misconceptions about lack of security in cloud solutions and many misuses of cloud storage options still occur every day. …
Whether it be Exchange Server Crash Issues, Dirty Shutdown Errors or Failed to mount error, Stellar Phoenix Mailbox Exchange Recovery has always got your back. With the help of its easy to understand user interface and 3 simple steps recovery proced…
Suggested Courses
Course of the Month14 days, 21 hours left to enroll

840 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question