Determine if an Object is convertible to double at runtime

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

Hello,

I have a function that takes variable number of parameters -
[ParamArray]Object* args[]. In the function, I need to iterate through args
and find those arguments that can be converted (implicitly or explicitly) to
double, e.g. integers and doubles themselves. If the conversion is possible
I'd like to get the double value of that object. How to do that?

Besides double, I'm interested to know if an object is DateTime or String
type. In such cases, I think I have to use a construct like this:

if (args->GetType() == __typeof(String))
{
String* s = args->ToString();
}

I wonder, if there's a more effective way to do this, besides of caching the
return value of __typeof(String) in a variable.
 
Off the top of my head having thought about it for only a few moments, you
could put the whole thing in a try/catch structure. This requires the
problem manifest at RUN time though, in contrast to being a 'can't-compile'
problem...

[==P==]
 
Ruslan,
Instead of checking the Type itself, I would probably check the TypeCode of
the object.

if (Convert::GetTypeCode(args) == TypeCode::String)
| {
| String* s = args->ToString();
| }

I would call Convert::GetTypeCode once, then compare it with the respective
TypeCode values I'm interested in.


--
Hope this helps
Jay [MVP - Outlook]
..NET Application Architect, Enthusiast, & Evangelist
T.S. Bradley - http://www.tsbradley.net


| Hello,
|
| I have a function that takes variable number of parameters -
| [ParamArray]Object* args[]. In the function, I need to iterate through
args
| and find those arguments that can be converted (implicitly or explicitly)
to
| double, e.g. integers and doubles themselves. If the conversion is
possible
| I'd like to get the double value of that object. How to do that?
|
| Besides double, I'm interested to know if an object is DateTime or String
| type. In such cases, I think I have to use a construct like this:
|
| if (args->GetType() == __typeof(String))
| {
| String* s = args->ToString();
| }
|
| I wonder, if there's a more effective way to do this, besides of caching
the
| return value of __typeof(String) in a variable.
|
| --
| Best regards,
| Ruslan Popov
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top