Solved

Passing properties as parameters

Posted on 1998-03-09
19
242 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
Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

 
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

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

This article explains how to create forms/units independent of other forms/units object names in a delphi project. Have you ever created a form for user input in a Delphi project and then had the need to have that same form in a other Delphi proj…
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…
Although Jacob Bernoulli (1654-1705) has been credited as the creator of "Binomial Distribution Table", Gottfried Leibniz (1646-1716) did his dissertation on the subject in 1666; Leibniz you may recall is the co-inventor of "Calculus" and beat Isaac…
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …

820 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