By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
424,831 Members | 1,031 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 424,831 IT Pros & Developers. It's quick & easy.

How do I convert from string to Type?

P: n/a
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
Share this Question
Share on Google+
4 Replies


P: n/a
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

P: n/a
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

P: n/a

"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

P: n/a
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.