[Last Call] Learn about multicloud storage options and how to improve your company's cloud strategy. Register Now

x
?
Solved

Referencing TObjects

Posted on 2001-06-23
6
Medium Priority
?
327 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 200 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
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
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

New feature and membership benefit!

New feature! Upgrade and increase expert visibility of your issues with Priority Questions.

Question has a verified solution.

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

Objective: - This article will help user in how to convert their numeric value become words. How to use 1. You can copy this code in your Unit as function 2. than you can perform your function by type this code The Code   (CODE) The Im…
Creating an auto free TStringList The TStringList is a basic and frequently used object in Delphi. On many occasions, you may want to create a temporary list, process some items in the list and be done with the list. In such cases, you have to…
Have you created a query with information for a calendar? ... and then, abra-cadabra, the calendar is done?! I am going to show you how to make that happen. Visualize your data!  ... really see it To use the code to create a calendar from a q…
Want to learn how to record your desktop screen without having to use an outside camera. Click on this video and learn how to use the cool google extension called "Screencastify"! Step 1: Open a new google tab Step 2: Go to the left hand upper corn…
Suggested Courses

650 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