Solved

Passing properties as parameters

Posted on 1998-03-09
19
238 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
 
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
Free Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

 
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

What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

Join & Write a Comment

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…
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…
Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.
In this tutorial you'll learn about bandwidth monitoring with flows and packet sniffing with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're interested in additional methods for monitoring bandwidt…

707 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

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now