Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6370107
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T00:53:17+00:00 2026-05-25T00:53:17+00:00

In the question here a method for creating a compatible TValue to use with

  • 0

In the question here a method for creating a compatible TValue to use with SetValue is shown. I’m trying to make a generic version of this, to use RTTI to store a class into an INI file. This is my cut down code:

procedure TMyClass.LoadRTTI(xObject: TObject);
var
  LContext: TRttiContext;
  LClass: TRttiInstanceType;
  xField : TRttiField;
  szNewValue : String;
  xValue : TValue;
begin
  LContext := TRttiContext.Create;
  LClass := LContext.GetType(xObject.ClassType) as TRttiInstanceType;

  for xField in LClass.GetDeclaredFields do
  begin
    szNewValue := IniFile.ReadString(szSection, xField.Name, '');
    if szNewValue <> '' then // emumerated will be '0' (zero) as that is what GetValue.AsString returns
    begin
      case xField.FieldType.TypeKind of
      tkEnumeration: xValue := StrToIntDef(szNewValue, xField.GetValue(xObject).AsOrdinal);
      end;
      xField.SetValue(xObject, xValue); // FAILS HERE with 'Invalid calss typecast
    end;
  end;
end;

In the answer referenced, the solution was to get the value using the TValue.From() method, but that appears to require a variable of the appropriate type. I don’t have such a type as my code doesn’t know what it is.

I am seeking an example of a generic way to obtain a value in a string from the RTTI, and put it back again afterward. I’ve not found a good tutorial that covers this yet.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-25T00:53:18+00:00Added an answer on May 25, 2026 at 12:53 am

    You must get an instance to the TValue to set before assing a value, and then convert the string to the enumerated value using the GetEnumValue function

    Try this code :

    procedure TMyClass.LoadRTTI(xObject: TObject);
    var
      LContext: TRttiContext;
      LClass: TRttiInstanceType;
      xField : TRttiField;
      szNewValue : String;
      xValue : TValue;
    begin
      LContext := TRttiContext.Create;
      LClass := LContext.GetType(xObject.ClassType) as TRttiInstanceType;
    
      for xField in LClass.GetDeclaredFields do
      begin
        szNewValue := IniFile.ReadString(szSection, xField.Name, '');
        if szNewValue <> '' then // emumerated will be '0' (zero) as that is what GetValue.AsString returns
        begin
          case xField.FieldType.TypeKind of
          tkEnumeration: 
                       begin
                         //get the instance to the TValue to set
                         xValue:=xField.GetValue(xObject);
                         //convert the data to a valid TValue
                         xValue:=TValue.FromOrdinal(xValue.TypeInfo,GetEnumValue(xValue.TypeInfo,szNewValue));
                       end;
    
          end;
          //assign the new value from the TValue
          xField.SetValue(xObject, xValue); 
        end;
      end;
    end;
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

So I had plenty of help on here creating this method and getting it
I have seen this question here, and was wondering if the same method of
As a secondary method to my question here: Show Cell Range on UserForm; then
Here's a question to expose my lack of experience: I have a method DoSomething()
Here's a quicky question. Which method name makes the most sense for an Objective-C
this is my first question here so I hope I can articulate it well
This may just be a yes or no type of question but here goes
This question is sort of a follow-up to my original question here . Let's
Having wrestled with NSPopUpButton in a previous question here I am now trying to
This question has something to do with the question I posted here: Iphone Core

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.