Solved

Referencing TObjects

Posted on 2001-06-23
6
322 Views
Last Modified: 2010-04-06
There has to be a better way to do this...

I want to reference an Object depending on the procedure's sender. Sadly, I am still intimidated by pointers.


Here is an example of my current code (simplified)

// Bad way
procedure TForm1.ButtonClick(sender: TObject);
begin
 if sender = Button1 then
  begin
  Panel1.Visible := TRUE;
  Panel1.Top := 100;
  Panel1.Left := 100;
  end;
 if sender = Button2 then
  begin
  Panel2.Visible := TRUE;
  Panel2.Top := 100;
  Panel2.Left := 100;
  end;
 if sender = Button3 then
  begin
  Panel3.Visible := TRUE;
  Panel3.Top := 100;
  Panel3.Left := 100;
  end;

//etc....

end;




// better way ???

procedure TForm1.ButtonClick(sender: TObject);
 var // P: pointer?
begin
 P.Create;

 if sender = Button1 then P := Panel1;
 if sender = Button2 then P := Panel2;
 if sender = Button3 then P := Panel3;

 with P do
 Visible := TRUE;
 Top := 100;
 Left := 100;
 end;

 P.Free;
end;
 
1) Please help decaring the reference object and anything else that must be done to keep the code clean..
2) If I P.Free, will it also free Panel1?

Many Thanks.
Don

0
Comment
Question by:d32coder
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
  • 2
  • 2
6 Comments
 
LVL 27

Accepted Solution

by:
kretzschmar earned 50 total points
ID: 6221213
// better way ???
maybe

                     procedure TForm1.ButtonClick(sender: TObject);
                     var  P: TPanel;
                     begin
                     //P.Create; no we have an existing one

                     if sender = Button1 then P := Panel1;
                     if sender = Button2 then P := Panel2;
                     if sender = Button3 then P := Panel3;

                     with P do
                     Visible := TRUE;
                     Top := 100;
                     Left := 100;
                     end;

                     //P.Free;  //no this would free the panel
                     end;
                     
0
 
LVL 14

Expert Comment

by:AvonWyss
ID: 6221484
You could also use the Tag property (if not already in use for something else) to store your reference:

        procedure TForm1.FormCreate(sender: TObject);
        begin
                Button1.Tag:=Longint(Panel1);
                Button2.Tag:=Longint(Panel2);
                Button3.Tag:=Longint(Panel3);
        end;

        procedure TForm1.ButtonClick(sender: TObject);
        begin
                with TPanel(TComponent(Sender).Tag) do begin
                        Visible := True;
                        Top := 100;
                        Left := 100;
                end;
        end;

Nice, eh? The only thing you have to do is to set the Tag property once for each TButton component as shown for this to work.
0
 

Author Comment

by:d32coder
ID: 6222924
I would NEVER have thought of doing that with tags.  How does it work? I thought a tag was an integer value??
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 27

Expert Comment

by:kretzschmar
ID: 6223407
its just a conversion

integer is a 4 Byte value
pointer is a 4 byte value

so you can store a pointer via conversion into a integer field/property

the conversion is shown by avonwyss
0
 
LVL 14

Expert Comment

by:AvonWyss
ID: 6224329
Thanks kretzschmar for the explanation. Since all information contained in a Pointer can be stored in an Integer, I just do the "conversion" (actually, it's only a typecast) back and forth when needed. This gives me compact and fast code then, avoiding any IF's after the initialisation.
0
 

Author Comment

by:d32coder
ID: 6225767
I used both examples and each solved a different problem.  
AvonWyss, I'll send you some points too.
Thanks to all.

Don
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Suggested Solutions

Introduction Raise your hands if you were as upset with FireMonkey as I was when I discovered that there was no TListview.  I use TListView in almost all of my applications I've written, and I was not going to compromise by resorting to TStringGrid…
In my programming career I have only very rarely run into situations where operator overloading would be of any use in my work.  Normally those situations involved math with either overly large numbers (hundreds of thousands of digits or accuracy re…
Are you ready to implement Active Directory best practices without reading 300+ pages? You're in luck. In this webinar hosted by Skyport Systems, you gain insight into Microsoft's latest comprehensive guide, with tips on the best and easiest way…
Finding and deleting duplicate (picture) files can be a time consuming task. My wife and I, our three kids and their families all share one dilemma: Managing our pictures. Between desktops, laptops, phones, tablets, and cameras; over the last decade…

737 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