Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Passing properties as parameters

Posted on 1998-03-09
19
Medium Priority
?
247 Views
Last Modified: 2013-11-23
Is it possible to pass a property as a parameter?

For example:

SetProperty(Panel1.Width,50);

procedure SetProperty(FProperty: Some type/class; FValue: Integer);
begin
  FProperty:=FValue;
end;
0
Comment
Question by:ronit051397
[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
  • 7
  • 6
  • 4
  • +2
19 Comments
 
LVL 8

Expert Comment

by:ZifNab
ID: 1360323
Don't think so.... At least I don't know

Because parameters are always some sort of type... e.g. integer, real and if you pass them like this Panel1.Width you just pass the value (integer,...) not the property itself. Maybe sending the component itself? (TPanel). Then you already can change width of several panels if you use a function like SetWidthPanelProperty(vTPanel,...).

Interesting question though.
0
 
LVL 5

Author Comment

by:ronit051397
ID: 1360324
But I have many kinds of properties, related to many kinds of Components.
Maybe it has something to do with TPropertyEditor.
In the RX Library you have a component called TFormStorage which restore properties of all kind of visual components.
0
 
LVL 8

Expert Comment

by:ZifNab
ID: 1360325
Yeah, that's through, didn't thought on that..... let see at it!
0
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
LVL 8

Expert Comment

by:ZifNab
ID: 1360326
Well, they only have to save the position of the  component. That's always the same! e.g. Top and Left property which are always available! They just search for all the components on the form and then save these two properties...
0
 
LVL 5

Author Comment

by:ronit051397
ID: 1360327
Actually, they can resore all kinds of properies.
The TFormStorge has a propery called 'StoredProps' which is a TStringList that stores the components' properties.
0
 
LVL 3

Expert Comment

by:Matvey
ID: 1360328
Ronit, are you using Delphi 3? have you looked at the new variant type like in VB?

Just a guess...
0
 
LVL 5

Expert Comment

by:JimBob091197
ID: 1360329
Hi

Referring back to Zif's original comment, you cannot pass panel1.Width because that's just an integer.  And you cannot do the following with properties:

procedure SetIntProp(var SomeIntProp: Integer; NewValue: Integer);

I have written a component which sounds similar to the RX one you are talking about.  I store the component name and the property name in a TStringList.  (E.g. 'Panel1.Width', 'CheckBox1.Checked', etc...)  I load all the property values specified in the stringlist when the app starts, and save these property values when the app closes.

I wrote a unit (very useful, I think  ;-)  ) to set properties if you have name of the property.  Here is a sample of some of the code from the unit, and how to use it:  (I can mail the unit to whoever wants it.)

// General property routines.
function GetPropertyCount(AObject: TObject): Integer;
function GetPropertyName(AObject: TObject; Index: Integer): string;
function GetPropertyIndex(AObject: TObject; APropertyName: string): Integer;
function GetPropertyKind(AObject: TObject; Index: Integer): TTypeKind;
function IsStoredProperty(AObject: TObject; Index: Integer): Boolean;

// Property Get/Set routines.
function GetBoolValue(AObject: TObject; Index: Integer): Boolean;
procedure SetBoolValue(AObject: TObject; Index: Integer; AValue: Boolean);

function GetIntValue(AObject: TObject; Index: Integer): Integer;
procedure SetIntValue(AObject: TObject; Index, AValue: Integer);

function GetFloatValue(AObject: TObject; Index: Integer): Extended;
procedure SetFloatValue(AObject: TObject; Index: Integer; AValue: Extended);

function GetStrValue(AObject: TObject; Index: Integer): string;
procedure SetStrValue(AObject: TObject; Index: Integer; const AValue: string);


You can then use these functions as follows:
(This sample searches for a component called 'MyCheckBox' and when found searches for a property called 'Checked', which gets set to True.)

procedure TForm1.Button1Click(Sender: TObject);
var
  MyComp: TComponent;
  MyPropIx: Integer;
begin
  MyComp := FindComponent('MyCheckBox');
  if (MyComp <> nil) then begin
    MyPropIx := GetPropertyIndex(MyComp, 'Checked');
    if (MyPropIx >= 0) then
      SetBoolValue(MyComp, MyPropIx, True);
  end;
end;

Regards,
JB

P.S.  Sorry Ronit, we've got a bit off the topic here...

0
 
LVL 8

Expert Comment

by:ZifNab
ID: 1360330
Hi JimBob, can you send this unit to me? 10x.

0
 
LVL 5

Expert Comment

by:JimBob091197
ID: 1360331
Hi Zif.  I have sent it to you.
JB
0
 
LVL 5

Author Comment

by:ronit051397
ID: 1360332
Can you please send me the code to  cmr@netvision.net.il

Thanks,
Ronit
0
 
LVL 5

Expert Comment

by:JimBob091197
ID: 1360333
Hi Ronit
  I have sent it it to you.
JB
0
 
LVL 5

Accepted Solution

by:
JimBob091197 earned 800 total points
ID: 1360334
Hi again Ronit.  In response to your e-mail, here is the answer.

Cheers, JB
0
 
LVL 1

Expert Comment

by:bcrotaz
ID: 1360335
Wow.
Please may I have a copy too?
I have just posted a question on getting properties when you only have the name.  You should take the points for that one too!
Send me an answer, and I'll grade it an A.
0
 
LVL 5

Author Comment

by:ronit051397
ID: 1360336
How do I assign a name to objects?
for example, if I write myDBGrid.Columns[0].Width=50 then the object is
myDBGrid.Columns[0], and I can't use FindComponent for objects, do you know the eqvivalent for it, or should I add new procedures for the property type of tkClass?

Thanks,
Ronit
0
 
LVL 5

Author Comment

by:ronit051397
ID: 1360337
I think it has something to do with GetComponent(Index: Integer): TPersistent;
in the TPropertyEditor.

0
 
LVL 5

Expert Comment

by:JimBob091197
ID: 1360338
Hi all

bcrotaz:  Yeah, I saw your question and have made comments there.

Ronit:  Mmm...  Good question.  Your comment about "GetComponent(Index: Integer): TPersistent" seems to provide the clue, but TPersistent doesn't have a "Name" property.  (TComponent has "Name".)

If you find a solution first, or add procedures for tkClass, please add a comment here.

Bye,
JB
0
 
LVL 5

Author Comment

by:ronit051397
ID: 1360339
Your code works only with Published properties.
0
 
LVL 5

Expert Comment

by:JimBob091197
ID: 1360340
Yes, that is correct.
The names of properties in objects (like variables in procedures) are hidden at run-time.  The exception is published properties for Delphi objects, which can be accessed through Borland's RTTI (Run-Time Type Information) functions.  This only works for classes compiled in the {$M+} or {$TYPEINFO ON} states (e.g. TPersistent, and everything inherited from it).

JB
0
 
LVL 5

Author Comment

by:ronit051397
ID: 1360341
Thanks.
0

Featured Post

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!

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…
Have you ever had your Delphi form/application just hanging while waiting for data to load? This is the article to read if you want to learn some things about adding threads for data loading in the background. First, I'll setup a general applica…
Add bar graphs to Access queries using Unicode block characters. Graphs appear on every record in the color you want. Give life to numbers. Hopes this gives you ideas on visualizing your data in new ways ~ Create a calculated field in a query: …
Do you want to know how to make a graph with Microsoft Access? First, create a query with the data for the chart. Then make a blank form and add a chart control. This video also shows how to change what data is displayed on the graph as well as form…
Suggested Courses

705 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