delphifiremonkeydelphi-xe4tvalue

Automatically validate TEditBox.data to TValue.TypeInfo


I have a FMX edit box, and want to assign the entered value (TEdit.Data) to a TValue property of some object. The object also contains a TypeInfo pointer which specifies of which type the data entered in the edit box should be.

   TAttrib = class(TObject)
    public
      property WantedType: PTypeInfo;
      property Value: TValue;
    end;

What i would like to accomplish is that i can assign the entered data to TAttrib.Value and then validate if the entered data is in the correct format, according to WantedType, like this:

Attrib.Value := TEditbox.Data;
Valid := Attrib.Value.TryCast(Attrib.Wantedtype, lVal) and (not lVal.IsEmpty);

This fails because Value is always of type string, and TryCast does not convert from string to i.e extended even if the entered string would convert nicely with i.e. StrToFloat()

How can i accomplish this?


Solution

  • You've already found TryCast, the function that converts between types for TValue. And its supported conversions does not include some of the conversions that you need.

    There is no built in support for what you are trying to achieve. You will have to roll your own, or find some other third party library that does what you want.