Results 1 to 3 of 3

Thread: Accesing propertys by name

  1. #1

    Accesing propertys by name

    Hi guys!
    Is it posible to acces properties by name. I'm trying to create a function wich would be able to replace portion of text (property name) with value of certain property. The function would be similar to Format function for string formating, but would only take text as an input. For instance the function would replace "#i#MyValue# " portion of the input text with actual value from MyValue wich is an integer variable.
    Last edited by SilverWarior; 29-10-2011 at 02:34 AM.

  2. #2
    You could RTTI for that, it's quite easy once you get the hang of it. Assuming you use Delphi or FreePascal in Delphi mode, you need to set {M+} in your unit where you defined the class.
    Only published properties work and the properties are case-sensitive.

    To set a property using RTTI, look at this small example:
    Code:
    uses
      typinfo;
    
    [...]
    
    {M+}
    TMyClass = class
      procedure SetProperty(const aName: String; aValue: AnsiString); 
    end;
    
    [...]
    
    procedure TelObject.SetPropertyStr(const aName: String; aValue: AnsiString);
    begin
      if not IsPublishedProp(aName) then Exit;
    
      case PropType(Self, aName) of
        tkInteger, tkInt64, tkQWord: SetOrdProp(Self, aName, StrToInt(aValue));
        tkFloat: SetFloatProp(Self, tmpName, StrToFloat(aValue));
        tkSString, tkLString, tkAString, tkWString, tkUString: SetStrProp(Self, aName, aValue);
        tkVariant: SetVariantProp(Self, aName, aValue);
        tkBool: SetOrdProp(Self, aName, Ord(LowerCase(aValue) = 'true'));
      end;
    end;
    This example checks if the property exists and sets the property according to its type. So if the property is an integer, it will set as an integer (even though aValue is a string) and the same goes for every other type.
    Freeze Development | Elysion Game Framework | Twitter: @Stoney_FD
    Check out my new book: Irrlicht 1.7.1 Realtime 3D Engine Beginner's Guide (It's C++ flavored though)

    Programmer: A device for converting coffein into software.

  3. #3

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •