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;
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;