Passing properties as parameters

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;
LVL 5
ronit051397Asked:
Who is Participating?

Improve company productivity with a Business Account.Sign Up

x
 
JimBob091197Connect With a Mentor Commented:
Hi again Ronit.  In response to your e-mail, here is the answer.

Cheers, JB
0
 
ZifNabCommented:
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
 
ronit051397Author Commented:
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
Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

 
ZifNabCommented:
Yeah, that's through, didn't thought on that..... let see at it!
0
 
ZifNabCommented:
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
 
ronit051397Author Commented:
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
 
MatveyCommented:
Ronit, are you using Delphi 3? have you looked at the new variant type like in VB?

Just a guess...
0
 
JimBob091197Commented:
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
 
ZifNabCommented:
Hi JimBob, can you send this unit to me? 10x.

0
 
JimBob091197Commented:
Hi Zif.  I have sent it to you.
JB
0
 
ronit051397Author Commented:
Can you please send me the code to  cmr@netvision.net.il

Thanks,
Ronit
0
 
JimBob091197Commented:
Hi Ronit
  I have sent it it to you.
JB
0
 
bcrotazCommented:
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
 
ronit051397Author Commented:
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
 
ronit051397Author Commented:
I think it has something to do with GetComponent(Index: Integer): TPersistent;
in the TPropertyEditor.

0
 
JimBob091197Commented:
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
 
ronit051397Author Commented:
Your code works only with Published properties.
0
 
JimBob091197Commented:
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
 
ronit051397Author Commented:
Thanks.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.