469,631 Members | 1,218 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,631 developers. It's quick & easy.

How do I convert from string to Type?

I have an unknown numeric Type object passed into a function. I want to run
a conversion on a string to convert the string to that Type object and
return an object of that type. Is there some way to do a generic cast or
conversion on the type?

Here is sort of what I want to do:

object MyFunc(Type T, String Str)
{
object o;

o = // Want to convert Str to be of type T here.

return o; // Want to return an object of Type T here that has the
converted value.
}

--
-----------------------------------
Ken Varn
Senior Software Engineer
Diebold Inc.

EmailID = varnk
Domain = Diebold.com
-----------------------------------
Nov 16 '05 #1
4 17415
Try Convert.ChangeType

"Ken Varn" <nospam> wrote in message
news:ue**************@TK2MSFTNGP14.phx.gbl...
I have an unknown numeric Type object passed into a function. I want to run a conversion on a string to convert the string to that Type object and
return an object of that type. Is there some way to do a generic cast or
conversion on the type?

Here is sort of what I want to do:

object MyFunc(Type T, String Str)
{
object o;

o = // Want to convert Str to be of type T here.

return o; // Want to return an object of Type T here that has the
converted value.
}

--
-----------------------------------
Ken Varn
Senior Software Engineer
Diebold Inc.

EmailID = varnk
Domain = Diebold.com
-----------------------------------

Nov 16 '05 #2
But you dont know the type? So dont you use a GetType() method?
"Ken Varn" <nospam> wrote in message
news:ue**************@TK2MSFTNGP14.phx.gbl...
I have an unknown numeric Type object passed into a function. I want to run a conversion on a string to convert the string to that Type object and
return an object of that type. Is there some way to do a generic cast or
conversion on the type?

Here is sort of what I want to do:

object MyFunc(Type T, String Str)
{
object o;

o = // Want to convert Str to be of type T here.

return o; // Want to return an object of Type T here that has the
converted value.
}

--
-----------------------------------
Ken Varn
Senior Software Engineer
Diebold Inc.

EmailID = varnk
Domain = Diebold.com
-----------------------------------

Nov 16 '05 #3

"Ken Varn" wrote...
I have an unknown numeric Type object passed into a function.
I want to run a conversion on a string to convert the string
to that Type object and return an object of that type. Is
there some way to do a generic cast or conversion on the type?

Here is sort of what I want to do:

object MyFunc(Type T, String Str)
{
object o;

o = // Want to convert Str to be of type T here.

return o; // Want to return an object of Type T here that has the
converted value.
}


It's not obvious what you're trying to accomplish. As what your method
returns only is of the type "object", you'll probably still need to cast it
anyway after the call.

As you still have to cast the *result*, you actually would'nt even have to
supply the numeric type. You could try something simple like:

static decimal MyParse(String str)
{
return decimal.Parse(str);
}

// Bjorn A

Nov 16 '05 #4
Simply:

object MyFunc(Type T, String Str)
{
return Convert.ChangeType(Str, T);
}

The Convert.ChangeType() method tries to retrieve an IConvertible interface
for the first argument and then calls the respective conversion method.

HTH,
Stefan

"Ken Varn" <nospam> wrote in message
news:ue**************@TK2MSFTNGP14.phx.gbl...
I have an unknown numeric Type object passed into a function. I want to
run
a conversion on a string to convert the string to that Type object and
return an object of that type. Is there some way to do a generic cast or
conversion on the type?

Here is sort of what I want to do:

object MyFunc(Type T, String Str)
{
object o;

o = // Want to convert Str to be of type T here.

return o; // Want to return an object of Type T here that has the
converted value.
}

--
-----------------------------------
Ken Varn
Senior Software Engineer
Diebold Inc.

EmailID = varnk
Domain = Diebold.com
-----------------------------------

Nov 16 '05 #5

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

4 posts views Thread by John Cotsell | last post: by
5 posts views Thread by Allerdyce.John | last post: by
3 posts views Thread by Ursula | last post: by
3 posts views Thread by Shawn Ferguson | last post: by
1 post views Thread by neeraj | last post: by
5 posts views Thread by 9966 | last post: by
2 posts views Thread by moondaddy | last post: by
reply views Thread by gheharukoh7 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.