Board index » delphi » Help! Pointers to objects

Help! Pointers to objects

Okay, am I a moron??  I want to mimic an indexed array of label
objects, like Visual Basic can do with its Index property.
I declare:
Type
  MyWindow = class(TForm)
  Label1: TLabel;
  Label2: TLabel;
  Label3: TLabel;

var
  Labels: Array[1..3] of ^TLabel;

Procedure FormCreate  (yes, I've tried it in FormActivate and
FormShow)
begin
  MyWindow.Labels[1] := @MyWindow.Label1;   *
  MyWindow.Labels[2] := @MyWindow.Label2;
  MyWindow.Labels[3] := @MyWindow.Label3;
end;

Then, I reference them like this:
MyWindow.Labels[1]^.Caption := 'Hi there This is label 1.';

I get a GPF on the above line marked with a '*'.  Any ideas?  Is this
a good way to make an array of similar objects?

Thanks,
--
Dave Eisenberg  -=[ dave @ delbruck.pharm.sunysb.edu ]=-

 

Re:Help! Pointers to objects


Quote
d...@delbruck.pharm.sunysb.edu (David Eisenberg) wrote:
>Okay, am I a moron??  I want to mimic an indexed array of label
>objects, like Visual Basic can do with its Index property.
>I declare:
>Type
>  MyWindow = class(TForm)
>  Label1: TLabel;
>  Label2: TLabel;
>  Label3: TLabel;
>var
>  Labels: Array[1..3] of ^TLabel;

This lines says you are creating an array of pointers to pointers of
TLabel.  Delphi's objects by default are pointers.  Remove the ^.

Quote
>Procedure FormCreate  (yes, I've tried it in FormActivate and
>FormShow)
>begin
>  MyWindow.Labels[1] := @MyWindow.Label1;   *
>  MyWindow.Labels[2] := @MyWindow.Label2;
>  MyWindow.Labels[3] := @MyWindow.Label3;
>end;

Remove the @ signs so you're not passing the address of the pointer
that points to the label.

Quote
>Then, I reference them like this:
>MyWindow.Labels[1]^.Caption := 'Hi there This is label 1.';

Again, you can remove the ^ since Delphi automaticly does this.

Quote
>I get a GPF on the above line marked with a '*'.  Any ideas?  Is this
>a good way to make an array of similar objects?
>Thanks,
>--
>Dave Eisenberg  -=[ dave @ delbruck.pharm.sunysb.edu ]=-

Hope that helps...
Brien King
bk...@primenet.com

Re:Help! Pointers to objects


In article <4e3lkd$...@abel.cc.sunysb.edu>,
   d...@delbruck.pharm.sunysb.edu (David Eisenberg) wrote:
Quote
>Okay, am I a moron??  I want to mimic an indexed array of label
>objects, like Visual Basic can do with its Index property.
>I declare:
>Type
>  MyWindow = class(TForm)
>  Label1: TLabel;
>  Label2: TLabel;
>  Label3: TLabel;

>var
>  Labels: Array[1..3] of ^TLabel;

>Procedure FormCreate  (yes, I've tried it in FormActivate and
>FormShow)
>begin
>  MyWindow.Labels[1] := @MyWindow.Label1;   *
>  MyWindow.Labels[2] := @MyWindow.Label2;
>  MyWindow.Labels[3] := @MyWindow.Label3;
>end;

>Then, I reference them like this:
>MyWindow.Labels[1]^.Caption := 'Hi there This is label 1.';

>I get a GPF on the above line marked with a '*'.  Any ideas?  Is this
>a good way to make an array of similar objects?

>Thanks,

AFAIK in Delphi all objects are implemented as pointers, so try:
var
  Labels: Array[1..3] of TLabel;
..
  MyWindow.Labels[1] := MyWindow.Label1;  
  MyWindow.Labels[2] := MyWindow.Label2;
  MyWindow.Labels[3] := MyWindow.Label3;
(untested)

Regards,
Matthias

----
Matthias Romppel                   email: mrom...@gwdg.de
Institute for Psychology
Gosslerstr. 14
37073 Goettingen
Germany

Re:Help! Pointers to objects


Quote
d...@delbruck.pharm.sunysb.edu (David Eisenberg) wrote:
>Okay, am I a moron??  I want to mimic an indexed array of label
>objects, like Visual Basic can do with its Index property.
>I declare:
>Type
>  MyWindow = class(TForm)
>  Label1: TLabel;
>  Label2: TLabel;
>  Label3: TLabel;

>var
>  Labels: Array[1..3] of ^TLabel;

>Procedure FormCreate  (yes, I've tried it in FormActivate and
>FormShow)
>begin
>  MyWindow.Labels[1] := @MyWindow.Label1;   *
>  MyWindow.Labels[2] := @MyWindow.Label2;
>  MyWindow.Labels[3] := @MyWindow.Label3;
>end;

>Then, I reference them like this:
>MyWindow.Labels[1]^.Caption := 'Hi there This is label 1.';

>I get a GPF on the above line marked with a '*'.  Any ideas?  Is this
>a good way to make an array of similar objects?

        First, you don't need the ^'s and @'s, because objects are actually
pointers to objects already. You should be able to say

var
  Labels: Array[1..3] of TLabel;

and later

MyWindow.Labels[1] := MyWindow.Label1;

and

MyWindow.Labels[1].Caption := 'Hi there This is label 1.';

instead. Not that what you're doing is illegal, but you're using pointers
to pointers when pointers (ie object variables) would do.

        You say you "declare"

Type
  MyWindow = class(TForm)
  Label1: TLabel;
{etc}

 Does this mean that you're not adding the labels to the form in the IDE?
IF you're dragging the labels onto the form in the IDE then things should
work, so I bet you're not. The easy answer is do.

        If you really want to create them at runtime you need to Create them.
The variables of type TLabel are in fact just pointers - they don't point to
anything unless you say something like

Label1:=TLabel.Create;

(in the Form.OnCreate, say). Then you would have to set their size and postion,
set their parent to the form, and finally Show them. It's much easier to add objects
in the IDE .

        Just to be certain I hadn't left anything out I made a form with an
array[1..1] of labels that I created at runtime - the following works:

type
  TForm1 = class(TForm)
    procedure FormCreate(Sender: TObject);
  private
    { Private declarations }
  public  ALabel: array[1..1] of TLabel;
    { Public declarations }
  end;

var
  Form1: TForm1;

implementation

{$R *.DFM}

procedure TForm1.FormCreate(Sender: TObject);
begin
ALabel[1]:=TLabel.Create(Self);
with ALabel[1] do  begin
left:=0;
top:=0;
width:=100;
height:=20;
parent:=Self;
caption:='Tada!';
Show;
end;

        An array[1..3] is the same but more so.
--
David Ullrich
Don't you guys find it tedious typing the same thing
after your signature each time you post something?
I know I do, but when in Rome...

Re:Help! Pointers to objects


In article <4e3lkd$...@abel.cc.sunysb.edu>,
   d...@delbruck.pharm.sunysb.edu (David Eisenberg) wrote:

Quote
>Okay, am I a moron??  I want to mimic an indexed array of label
>objects, like Visual Basic can do with its Index property.
>I declare:
>Type
>  MyWindow = class(TForm)
>  Label1: TLabel;
>  Label2: TLabel;
>  Label3: TLabel;

>var
>  Labels: Array[1..3] of ^TLabel;

Change this to :
  Labels: Array[1..3] of TLabel;

Quote
>Procedure FormCreate  (yes, I've tried it in FormActivate and
>FormShow)
>begin
>  MyWindow.Labels[1] := @MyWindow.Label1;   *
>  MyWindow.Labels[2] := @MyWindow.Label2;
>  MyWindow.Labels[3] := @MyWindow.Label3;
>end;

Get rid of the '@' operators and everything should work fine.

Quote
>Then, I reference them like this:
>MyWindow.Labels[1]^.Caption := 'Hi there This is label 1.';

>I get a GPF on the above line marked with a '*'.  Any ideas?  Is this
>a good way to make an array of similar objects?

Classes are actually pointers. This is new in Delphi's implementation of
Object Pascal.  This becomes really apparent when you are trying to use
Sizeof(TLabel) and get back a value of 4.

You were actually placeing pointers to pointers in the array.

-----------------------------------------------
Mike Chapin
Powder River
mcha...@vcn.com
http://www.vcn.com/server/netizens/mchapin/first.html
Gillette, WY

Not the end of the earth but you can see it from
there.
-----------------------------------------------

Re:Help! Pointers to objects


Quote
David Eisenberg wrote:
> I want to mimic an indexed array of label
> objects, like Visual Basic can do with its Index property.
> I declare:
> Type
>   MyWindow = class(TForm)
>   Label1: TLabel;
> var
>   Labels: Array[1..3] of ^TLabel;

> Procedure FormCreate  (yes, I've tried it in FormActivate and
> FormShow)
> begin
>   MyWindow.Labels[1] := @MyWindow.Label1;   *
> I get a GPF on the above line.

Actually TLabel IS already a pointer to label object. So try this way:

Type
  TMyWindow = class(TForm)
  Label1: TLabel;
  Label2: TLabel;
var
  Labels: Array[1..2] of TLabel;

Procedure TMyWindow.FormCreate...  
begin
  Labels[1] := Label1;  
  Labels[2] := Label2;  
  Labels[2].Caption := 'Second label.';

Tip of the day:

Insert following code to FormCreate:

  for i := 1 to NumOfLabels do
    Labels[i].Tag := i;

Now you can find out index of label in event procedures:

procedure LabelClick(Sender: TObject);
var Index: integer;
begin
  Index := TLabel(Sender).Tag;
  Labels[Index].Caption := 'You clicked this label.';
end;

-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
Ari Hirviniemi, Lappeenranta, Finland (ari.hirvini...@ktieto.fi)
All my opinions and comments are personal and have nothing to do
with my employer.

Other Threads