[Webinar] Streamline your web hosting managementRegister Today

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 254
  • Last Modified:

Accessing properties by name

Hi,

I am trying to access properties by using a text string and am not sure how to go about it, or indeed if it is possible. The sort of thing I am trying to do is as follows:

procedure TForm1.Button1Click(Sender: TObject);
var
xx : TComponent;
yy : TProperty;
ss : string;

begin
  xx  := FindComponent('Edit1');//find the component
  yy := (xx as xx.ClassType).FindProperty('Text');//type cast the component and find the Text property
  yy := 'Test Data'; //write to property
  ss := yy; //read from property
end;

I know this is rubbish but it illustrates the sort of thing I am trying to do.

Please illustrate any possible answers with code.

Thanks

Chris
0
ChrisBerry
Asked:
ChrisBerry
  • 4
  • 3
  • 3
  • +2
1 Solution
 
DidierDCommented:
Hi,


Add TypInfo to the uses. Put an editbox and a button on the form and use the code below.

procedure TForm1.Button1Click(Sender: TObject);
var
  xx: TComponent;
  ss : string;
  PropInfo: PPropInfo;
begin
  xx := FindComponent('Edit1');
  PropInfo := GetPropInfo(xx.ClassInfo,'Text');
  if PropInfo <> nil then
  begin
    if PropInfo^.PropType^.Kind = tkLString then
    begin
      SetStrProp(xx, PropInfo, 'test');
      ShowMessage(GetStrProp(xx, PropInfo));
    end;
  end;
end;

See in TypInfo unit for more information. For integer check for tkInteger, etc..
 
TTypeKind = (tkUnknown, tkInteger, tkChar, tkEnumeration, tkFloat,
    tkString, tkSet, tkClass, tkMethod, tkWChar, tkLString, tkWString,
    tkVariant, tkArray, tkRecord, tkInterface, tkInt64, tkDynArray);

In typeInfo you can also find the other getters and setters.


Didier
0
 
DidierDCommented:
The other getters and setters

function GetOrdProp(Instance: TObject; PropInfo: PPropInfo): Longint; overload;
procedure SetOrdProp(Instance: TObject; PropInfo: PPropInfo;
  Value: Longint); overload;

function GetEnumProp(Instance: TObject; PropInfo: PPropInfo): string; overload;
procedure SetEnumProp(Instance: TObject; PropInfo: PPropInfo;
  const Value: string); overload;

function GetSetProp(Instance: TObject; PropInfo: PPropInfo;
  Brackets: Boolean = False): string; overload;
procedure SetSetProp(Instance: TObject; PropInfo: PPropInfo;
  const Value: string); overload;

function GetObjectProp(Instance: TObject; PropInfo: PPropInfo;
  MinClass: TClass = nil): TObject; overload;
procedure SetObjectProp(Instance: TObject; PropInfo: PPropInfo;
  Value: TObject); overload;
function GetObjectPropClass(Instance: TObject; PropInfo: PPropInfo): TClass; overload;

function GetStrProp(Instance: TObject; PropInfo: PPropInfo): string; overload;
procedure SetStrProp(Instance: TObject; PropInfo: PPropInfo;
  const Value: string); overload;

function GetFloatProp(Instance: TObject; PropInfo: PPropInfo): Extended; overload;
procedure SetFloatProp(Instance: TObject; PropInfo: PPropInfo;
  Value: Extended); overload;

function GetVariantProp(Instance: TObject; PropInfo: PPropInfo): Variant; overload;
procedure SetVariantProp(Instance: TObject; PropInfo: PPropInfo;
  const Value: Variant); overload;

function GetMethodProp(Instance: TObject; PropInfo: PPropInfo): TMethod; overload;
procedure SetMethodProp(Instance: TObject; PropInfo: PPropInfo;
  const Value: TMethod); overload;

function GetInt64Prop(Instance: TObject; PropInfo: PPropInfo): Int64; overload;
procedure SetInt64Prop(Instance: TObject; PropInfo: PPropInfo;
  const Value: Int64); overload;
0
Keep up with what's happening at Experts Exchange!

Sign up to receive Decoded, a new monthly digest with product updates, feature release info, continuing education opportunities, and more.

 
DidierDCommented:
:-(

I just checked out edey's comment and his answer is exactly the same. So don't give me the points.

Didier
0
 
EpsylonCommented:
listening...
0
 
edeyCommented:
There is an alternative method - for activeX controls.  Use the MS scripting control, then all of your parsing, evaluating & property/method access is taken care of.  For these very reasons I've wrapped most of the common controls (very easy in pro+ ver's of delphi).

GL
Mike
0
 
ChrisBerryAuthor Commented:
Hi Mike and Didier,

Thanks for your answers, I will check it out.

Mike, could you please amplify your scripting comment. I have never used this.

Regards

Chris
0
 
DragonSlayerCommented:
listening to what Mike has to say :)
0
 
ChrisBerryAuthor Commented:
I have tested and it works great for simple properties such as a string, integer etc. How do I deal with a property such as a Font or TStrings etc? I know the kind returned is tkClass but I don't know how to access the methods and properties of that class.

Thanks

Chris
0
 
edeyCommented:
A quick scripting example:

uses
 ..., comobj;

procedure TForm1.Button1Click(sender:TObject)
var
 scripting : variant;
begin
 scripting := createOLEObject('ScriptControl');
 scripting.language := 'JScript';
 scripting.addObject('doc',webBrowser1.document.true);
 scripting.eval('doc.innerHTML = 'hello, and welcome to a <b>scripted</b> world');
end;

This snippet assumes the form has a TWebBrowser named webBrowser1 on it.

As for accessing the properties of the member object, you know it's a pointer as you've got a type of tkClass, the pointer can then be simply recast as a descendant of TObject.  Unfortunately I don't know of any way to determine an arbitrary type cast at runtime - you'll have to know what it's likely to be at design time.  Of course any tobject desc. can be cast as tobject, you just lose any functionality not inhernet to TObject.

GL
Mike
0
 
ChrisBerryAuthor Commented:
Mike,

Thanks for your help.

Regards

Chris
0
 
edeyCommented:
Glad to be of service :)

GL
Mike
0

Featured Post

The 14th Annual Expert Award Winners

The results are in! Meet the top members of our 2017 Expert Awards. Congratulations to all who qualified!

  • 4
  • 3
  • 3
  • +2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now