In Delphi I want to determine whether a particular OleVariant can be cast to a particular data type without raising an exception if it can’t. Exceptions are not for program flow, right?
What I want is something like this, where Type could be anything supported by an OleVariant:
if TryVarAsType(variant, value) then ...
What I don’t want is
try
value := Type(variant);
// case where the variant could be converted to a Type
except
// case where the variant could not be converted to a Type
end;
The case where the variant could not be converted to a Boolean is just a normal case that occurs regularly and doesn’t indicate any kind of error.
you can construct such function using the
VariantChangeTypeExfunction.and use like this
this will work with only with ole compatible Variant types
for the another types (pascal variants) like
varString,varAnyyou must check the source and destinationTVarTypeand write your own test cases.UPDATE
As @David point me out, the locale settings can produce different results for the same values, so you must consider this answer just as initial step or tip to construct your own function and you must aware of locale settings issues caused in the proposed function.