-1

I have a bunch of different methods for creating, calling and setting properties of different objects using Delphi's RTTI. But now I have come to an error where setting a TDateTime triggers an error like: "Cannot convert variant into double". Google doesn't help when searching for this error.

So far, I'm defining an object of any type, for example:

TExample = class
private
  FDateField : TDateTime;
published
  property DateField : TDateTime read FDateField write FDateField;
end;

I'm then putting this object in a TObjectList, and then looping some internal logic that's not really relevant to the problem. But when I come to the DateField property, it triggers the error. I'm trying to set it like this:

objPropValue := '12/02/2018 12:25:00';
objPropName := 'DateField';
if IsPublishedProp(parameterObject, objPropName) then
begin
  SetPropValue(parameterObject, objPropName, objPropValue); <- doesn't work on DateField
end;

This is only a hardcoded example, the objPropValue and Name are set in a loop and can be of any other type. I tried different formatting as well, but I can't seem to find the correct way to do this.

Remy Lebeau
  • 555,201
  • 31
  • 458
  • 770
John
  • 261
  • 2
  • 16

1 Answers1

1

Despite the error message, what you are trying to do here is assign a string to a date, which you just can't do. If instead you did this

    objPropValue := '12/02/2018 12:25:00';
    objPropName := 'DateField';
    if IsPublishedProp(parameterObject, objPropName)  then
    begin
      SetPropValue(parameterObject, objPropName, StrToDateTime(objPropValue));
    end;

it would work fine. That is just for illustration, of course. If objPropValue is a variant (which you don't show) you could use

    objPropValue := StrToDateTime('12/02/2018 12:25:00');
    objPropName := 'DateField';
    if IsPublishedProp(parameterObject, objPropName)  then
    begin
      SetPropValue(parameterObject, objPropName, objPropValue);
    end;

instead.

Dsm
  • 5,870
  • 20
  • 24
  • This is an example. I don't know what is PropValue so i can't just assum and to StrToDateTime. Values can be of any type. Right now i'm trying to find a way to check the property type and then make conversion through there. – John Feb 12 '18 at 11:57
  • Nevertheless, this is what you must do. Of course you can check the parameter type, but without seeing your code I can't possibly know where you get the property value from. I feel I have answered your question as presented. – Dsm Feb 12 '18 at 12:02
  • "This is only a hardcoded example the objPropValue and Name are set in a loop and can be of any other type." I've written this in my question. This is just an example for the problem i have the type is not alway datetime. It's written. So thank you for you effort but this is not the answer to my quesiton. – John Feb 12 '18 at 12:11
  • If you want a better answer please provide a [MCVE] that in particular illustrates what objPropValue is and how it is obtained. Without that your question is meaningless (IMHO). There are quite a few people on the site who are experts in RTTI and should give you what you need if you give us enough information. – Dsm Feb 12 '18 at 12:18
  • @john could you please write an answer yourself, how you got it to work? – dummzeuch Feb 13 '18 at 11:26