Click here to Skip to main content
15,886,137 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I'm trying to create something like System.TVariant. I can't use TVariant because it stores the data in a type which it thinks it should be, i.e it will store a small number in another type than Real... Anyways, if there is a workaround for that, I don't want that, I just want to know 'how' it works and how I can create a data type that can hold varying data types, Something like this:
Delphi
type
  TStrInt = (AnsiString or Integer);

var
  TVr: TStrInt;

begin
  TVr := 56;
  WriteLn(TVr);
end.

As you can see, the 'or' gives error, and so does 'TVr := 56'. Anyway to do this?
Posted

I guess you have to use a Variant Record, see, for instance "Storing groups of data together"[^].
 
Share this answer
 
Comments
Sergey Alexandrovich Kryukov 16-Sep-15 10:27am    
This is a different thing, my 5 for mentioning it. However, I think the root problem is very different: I see no justification in using variants here. Please see Solution 2. Yes, it's based just on some claims, but it's not so easy to provide logical evidence in a Quick Answer...
—SA
CPallini 16-Sep-15 10:57am    
Thank you.
Listen for a good friendly advice: never ever use Variants, unless they are required by some 3rd-party lame API. Imaginary benefits of this type is a pretty usual delusion which damages part of developers. Delphi and many other technologies are oriented to strong typing, https://en.wikipedia.org/wiki/Strong_and_weak_typing[^].

Don't get me wrong, different typing paradigms, such as loose typing, can be extremely productive, especially in some scripting (interpretive) languages, but trust me, most of such technologies are way better than the lame COM variants. Also, COM is badly obsolete, there is no a reason to follow its worst drawbacks.

—SA
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900