Convert string to Type

J

John Cotsell

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 (e-mail address removed)

cheers John
 
M

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.
 
R

Raskawa

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.Random"))
 
I

Ignacio Machin \( .NET/ C# MVP \)

Hi,



John Cotsell said:
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 (e-mail address removed)

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

William Stacey [MVP]

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]

| 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 (e-mail address removed)
|
| cheers John
|
|
|
 

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

Top