What data type is this?

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

Guest

This is probably an easy question but i apparently can't find the right
Google key words

i am passed a string and the name of the data type i'm hoping that string is
(technically, i'm passed a System.TypeCode). My job is to verify that the
item really is a valid bool value, int, single, whatever

Not quite seeing what i want in reflection. Thought the various datatype
..Parse methods would work but not directly. Maybe i could try Parse and catch
the FormatException but i'm wondering if there's a more direct approach
 
By the way, here's the code i'm using. Just want to know if there's a more
elegant way to do this:

bool isExpectedType = true;
switch (criteria.DataType)
{
case TypeCode.Single:
try { float temp = float.Parse(value); }
catch (FormatException ex) { isExpectedType = false; }
return isExpectedType;
 
baylor said:
This is probably an easy question but i apparently can't find the right
Google key words

i am passed a string and the name of the data type i'm hoping that string is
(technically, i'm passed a System.TypeCode). My job is to verify that the
item really is a valid bool value, int, single, whatever

Not quite seeing what i want in reflection. Thought the various datatype
.Parse methods would work but not directly. Maybe i could try Parse and catch
the FormatException but i'm wondering if there's a more direct approach

You can use Convert.ChangeType to try to convert it to the right type,
but that will probably use the current culture etc, which may not be
what you're after. (You'd then have to catch the exception generated if
it the value was incorrect.)
 

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