Jump to content
Sign in to follow this  
dmitrybv

Methods to convert a string TValue to the desired simple type (Like Integer, Float, DateTime);

Recommended Posts

Are there methods in the TValue class or in Rtti to convert a string to the desired simple type (Like Integer, Float, DateTime)?

 

TValue has a method that converts a simple value to a string - TValue.ToString, but there is no reverse method TValue.FromString.


I tried using StrValue.Cast<Integer>();
but this method throws 'Invalid class typecast' exception.

 

procedure TFormSimpleDraw2.Button3Click(Sender: TObject);
var
  StrValue: TValue;
  IntValue: TValue;
  Str: String;
begin
  IntValue := 10;
  StrValue := IntValue.ToString();
//  IntValue := TValue.FromString(Str);
  IntValue := StrValue.Cast<Integer>();
end;

 

Share this post


Link to post
Posted (edited)

TValue is designed to hold any type, not just "simple" ones. There is no way to marshal a string representation like "(record)" or "(interface @ 02549BD0)" back into what they once were.

 

Maybe what you are looking for is a mechanism to serialize any record/object in memory into a string (or raw bytes), store them, and later de-serialize them?

Edited by Der schöne Günther

Share this post


Link to post

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
Sign in to follow this  

×