469,271 Members | 1,431 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

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

Convert string to Type

Basically I have a class called Dog and when using the objectdatasource I
can use a string parameter to pass into the selectmethod. so I pass in a
string = 'Dog' now once in the selectmethod I need to convert that string
into and object of type Dog...I hope this makes sense. Do anyone know how to
do this???? i guess I need a similar method to Eval in javascript.

Please need urgent so could replies also go to jo*********@hotmail.com

cheers John

Jan 19 '06 #1
4 25148
Me
Not positive if I understand correctly but if I do then take a look at
reflection in MSDN. You can create instances of objects given the string
name.

"John Cotsell" <jo**********@formicary.net> wrote in message
news:u5**************@TK2MSFTNGP14.phx.gbl...
Basically I have a class called Dog and when using the objectdatasource I
can use a string parameter to pass into the selectmethod. so I pass in a
string = 'Dog' now once in the selectmethod I need to convert that string
into and object of type Dog...I hope this makes sense. Do anyone know how
to do this???? i guess I need a similar method to Eval in javascript.

Please need urgent so could replies also go to jo*********@hotmail.com

cheers John

Jan 19 '06 #2
Play around with the following - it may help you if I understand the
need.. It basically creates a object from a string. (System.Random is
"Dog" in your example.)

Activator.CreateInstance(Type.GetType("System.Rand om"))

Jan 19 '06 #3
Hi,

"John Cotsell" <jo**********@formicary.net> wrote in message
news:u5**************@TK2MSFTNGP14.phx.gbl...
Basically I have a class called Dog and when using the objectdatasource I
can use a string parameter to pass into the selectmethod. so I pass in a
string = 'Dog' now once in the selectmethod I need to convert that string
into and object of type Dog...I hope this makes sense. Do anyone know how
to do this???? i guess I need a similar method to Eval in javascript.
No really, eval does other thing, it does evaluate an expression if you
pass 'dog' it does nothing, if OTOH pass 'new dog() ' it does what you want.

Take a look at CreateInstance method, there are several of them belongin to
several types, depending of what you have/know you use one over the other.

Please need urgent so could replies also go to jo*********@hotmail.com


sorry, no email access here :(
just check the NG !

--
Ignacio Machin,
ignacio.machin AT dot.state.fl.us
Florida Department Of Transportation
Jan 19 '06 #4
Well, you have to know the type first. Once you know the type, you can call
the types Parse() method. You could also implement explicit or implicit
converstion operators from String type.
public class MyType
{
public string Name;
public int Age;

public MyType()
{
}

public static MyType Parse(string value)
{
string[] sa = value.Split(new string[] {"," },
StringSplitOptions.RemoveEmptyEntries);
MyType mt = new MyType();
mt.Name = sa[0];
mt.Age = int.Parse(sa[1]);
return mt;
}
public override string ToString()
{
return Name + "," + Age.ToString();
}
public static explicit operator MyType(string value)
{
return MyType.Parse(value);
}

}

private void button1_Click(object sender, EventArgs e)
{
MyType mt = (MyType)"wjs, 30";
Console.WriteLine(mt)

}
--
William Stacey [MVP]

"John Cotsell" <jo**********@formicary.net> wrote in message
news:u5**************@TK2MSFTNGP14.phx.gbl...
| Basically I have a class called Dog and when using the objectdatasource I
| can use a string parameter to pass into the selectmethod. so I pass in a
| string = 'Dog' now once in the selectmethod I need to convert that string
| into and object of type Dog...I hope this makes sense. Do anyone know how
to
| do this???? i guess I need a similar method to Eval in javascript.
|
| Please need urgent so could replies also go to jo*********@hotmail.com
|
| cheers John
|
|
|
Jan 19 '06 #5

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

4 posts views Thread by Ken Varn | last post: by
7 posts views Thread by Jim Bancroft | last post: by
15 posts views Thread by Yifan | last post: by
5 posts views Thread by manmit.walia | last post: by
9 posts views Thread by Marco Nef | last post: by
reply views Thread by suresh191 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.