Solved

Passing properties as parameters

Posted on 1998-03-09
19
240 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
  • 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
3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

 
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 200 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

Optimizing Cloud Backup for Low Bandwidth

With cloud storage prices going down a growing number of SMBs start to use it for backup storage. Unfortunately, business data volume rarely fits the average Internet speed. This article provides an overview of main Internet speed challenges and reveals backup best practices.

Question has a verified solution.

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

Suggested Solutions

A lot of questions regard threads in Delphi.   One of the more specific questions is how to show progress of the thread.   Updating a progressbar from inside a thread is a mistake. A solution to this would be to send a synchronized message to the…
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…
Two types of users will appreciate AOMEI Backupper Pro: 1 - Those with PCIe drives (and haven't found cloning software that works on them). 2 - Those who want a fast clone of their boot drive (no re-boots needed) and it can clone your drive wh…
This video shows how to quickly and easily add an email signature for all users on Exchange 2016. The resulting signature is applied on a server level by Exchange Online. The email signature template has been downloaded from: www.mail-signatures…

777 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