Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations biv343 on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How do I make a User made Object Global 2

Status
Not open for further replies.

GrantBeginner

Programmer
Nov 3, 2007
2
I am having my first go at making a component and have a VERY basic question. Why is it that when I declare the component at the start in the VAR section it produces errors, but when I declare it in a procedure where it is not global it works fine.

The error is produced at runtime
Project raised exception class EAccessViolation with message 'Access Violation etc etc etc

// This one works fine

TMyObject = class(TObject)
private
{ Private declarations }
public
{ Public declarations }
C : Char;
constructor Create (Character : Char);
end;

var
Form1: TForm1;

implementation

{$R *.dfm}

constructor TMyObject.Create(Character : Char);
begin
C := Character;
end;

procedure TForm1.FormActivate(Sender: TObject);
var
Obj : TMyObject;
Letter : Char;
begin
Letter := 'a';
Obj.Create(Letter);
end;
end.


// This one produces the error

TMyObject = class(TObject)
private
{ Private declarations }
public
{ Public declarations }
C : Char;
constructor Create (Character : Char);
end;

var
Form1: TForm1;
Obj : TMyObject;

implementation

{$R *.dfm}

constructor TMyObject.Create(Character : Char);
begin
C := Character;
end;

procedure TForm1.FormActivate(Sender: TObject);
var
Letter : Char;
begin
Letter := 'a';
Obj.Create(Letter);
end;
 
Figured it out. Really simple. You declare it in the TForm1 = class(TForm) area. But.....

How do you make sure that the object's constructor is called. I thought it was called automatically when the object was declared who assigned, but if you need to pass some data to the constructor and it was forgotten...?

eg:
type
TMyObject = class(TObject)
private
{ Private declarations }
Sentence : String;
public
{ Public declarations }
LengthOfSentence : Integer;
constructor create(Sentence : String);
end;

TForm1 = class(TForm)
Button1: TButton;
Memo1: TMemo;
procedure Button1Click(Sender: TObject);
procedure FormActivate(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }

MyObject : TMyObject;
end;

var
Form1: TForm1;

implementation

{$R *.dfm}

constructor TMyObject.create(Sentence : String);
begin
LengthOfSentence := Length(Sentence);
end;

procedure TForm1.Button1Click(Sender: TObject);
begin
Memo1.Lines.Add(IntToStr(MyObject.LengthOfSentence));
end;

procedure TForm1.FormActivate(Sender: TObject);
begin
// MyObject := TMyObject.create('Sentence'); maybe this is forgotten or missed
end;
 
A form is also an object.

when it is created it fires an event called FormCreate. when it is being destroyed it fires an event called FormDestroy. so create and (don't forget) to destroy your objects there.

Code:
...
interface

 TMyObject = class(TObject)
  private
    { Private declarations }
    Sentence : String;
  public
    { Public declarations }
    LengthOfSentence : Integer;
    constructor create(Sentence : String);
  end;

  TForm1 = class(TForm)
    Button1: TButton;
    Memo1: TMemo;
    procedure Button1Click(Sender: TObject);
    procedure FormActivate(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }

    MyObject : TMyObject;
  end;

...
implementation

procedure TForm1.FormCreate(Sender: TObject);
begin
 MyObject := TMyObjects.Create('test');
end;

procedure TForm1.Button1Click(Sender: TObject);
begin
 Memo1.Lines.Add(IntToStr(MyObject.LengthOfSentence));
end;

procedure TFrm_main.FormDestroy(Sender: TObject);
begin
 // don't forget to destroy your object or else you got a memory leak
 FreeAndNil(MyObject);
end;

/Daddy


-----------------------------------------------------
What You See Is What You Get
Never underestimate tha powah of tha google!
 
TObject is a lowest level ancestor of all classes. You could have used any of the higher level descendant of TObject within the objects hierarchy.

Highlight TForm in your class type definition. (interface section of your unit) Press F1 to invoke help and click "hierarchy". Select each and review. TControl or TComponent is usually a good choice, depending on your needs. Use "inherit" to inherit the lower level initialization, methods, and properties. TObject is abstract and has no methods or properties.

Don't confuse ownership (child/parent) relationships with hierarchy.

What Daddy has so wisly shown you is how to create "TMyObject" as a child of "TForm1".

TForm1 owns MyObject.
TForm1 is a decendant of TObject (but not a direct decendant.)
TForm1 is a direct decendant of TCustomForm.
TMyObject is direct decendant of TObject (and only TObject.)

Just like:
You own your dog.
Your dog is a decendant of its mother.
You have no relation to your dog's mother. (Unless you own her too, but that's another seperate object.)

FormActivate() is not a good place to call create because it will get called everytime your form is activated. See Daddy's example.

I sure hope that all makes sense.
Welcome to OOP and let us know if we can assist.


Roo
Delphi Rules!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top