473,659 Members | 3,117 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Convert string to Type

Basically I have a class called Dog and when using the objectdatasourc e 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*********@hot mail.com

cheers John

Jan 19 '06 #1
4 25339
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**********@f ormicary.net> wrote in message
news:u5******** ******@TK2MSFTN GP14.phx.gbl...
Basically I have a class called Dog and when using the objectdatasourc e 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*********@hot mail.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.Creat eInstance(Type. GetType("System .Random"))

Jan 19 '06 #3
Hi,

"John Cotsell" <jo**********@f ormicary.net> wrote in message
news:u5******** ******@TK2MSFTN GP14.phx.gbl...
Basically I have a class called Dog and when using the objectdatasourc e 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*********@hot mail.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[] {"," },
StringSplitOpti ons.RemoveEmpty Entries);
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(va lue);
}

}

private void button1_Click(o bject sender, EventArgs e)
{
MyType mt = (MyType)"wjs, 30";
Console.WriteLi ne(mt)

}
--
William Stacey [MVP]

"John Cotsell" <jo**********@f ormicary.net> wrote in message
news:u5******** ******@TK2MSFTN GP14.phx.gbl...
| Basically I have a class called Dog and when using the objectdatasourc e 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*********@hot mail.com
|
| cheers John
|
|
|
Jan 19 '06 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
3619
by: Eric Lilja | last post by:
Hello, I've made a templated class Option (a child of the abstract base class OptionBase) that stores an option name (in the form someoption=) and the value belonging to that option. The value is of the type the object is instantiated with. In my test program I have Option<std::string> and Option<long>. Here's the code for OptionBase and Option along with a small helper function. In the code are comments describing my problem, look closely...
4
17675
by: Ken Varn | last post by:
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;
7
30775
by: Jim Bancroft | last post by:
Hi everyone, A basic one here, I think. I haven't found the pattern yet, but sometimes when I cast a variable to another type using the "C" style cast operator the compiler refuses to play along. It says it's an invalid cast. However, if I use the Convert.ToInt32() method (for example) things will work. At least, that's how it appears to me. Could someone explain when to use old-style parenthesized casts vs. the Convert() methods?
15
10821
by: Yifan | last post by:
Hi Does anybody know how to convert System::String* to char*? I searched the System::String class members and did not find any. Thanks Yifan
5
2721
by: manmit.walia | last post by:
Hello All, I am stuck on a conversion problem. I am trying to convert my application which is written in VB.NET to C# because the project I am working on currently is being written in C#. I tried my best to convert it, but somehow the app does not work. I am also not getting any errors when I complie thus, letting me know I am on the write track. Basically what I want to do is edit,add,delete, and update an XML file in a DataGrid. Below...
5
3780
by: Learner | last post by:
Hello, Here is the code snippet I got strucked at. I am unable to convert the below line of code to its equavalent vb.net code. could some one please help me with this? static public List<RoleData> GetRoles() { return GetRoles(null, false); }
7
16672
by: groups | last post by:
This is my first foray into writing a generic method and maybe I've bitten off more than I can chew. My intent is to have a generic method that accepts a value name and that value will be returned from the source. My first attempt was as follows; (please ignore that error handling is not present in this example) public T GetValue<T(string objName) { T results;
1
442
by: FAQ server | last post by:
----------------------------------------------------------------------- FAQ Topic - Why does 1+1 equal 11? or How do I convert a string to a number? ----------------------------------------------------------------------- Javascript variables are loosely typed: the conversion between a string and a number happens automatically. Since plus (+) is also used as in string concatenation, ` '1' + 1 ` is equal to ` '11' `: the String deciding...
9
17533
by: Marco Nef | last post by:
Hi there I'm looking for a template class that converts the template argument to a string, so something like the following should work: Convert<float>::Get() == "float"; Convert<3>::Get() == "3"; Convert<HelloWorld>::Get() == "HelloWorld"; The reason I need this is that in our design every class of a certain
0
10752
Debadatta Mishra
by: Debadatta Mishra | last post by:
Introduction In this article I will provide you an approach to manipulate an image file. This article gives you an insight into some tricks in java so that you can conceal sensitive information inside an image, hide your complete image as text ,search for a particular image inside a directory, minimize the size of the image. However this is not a new concept, there is a concept called Steganography which enables to conceal your secret...
0
8428
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8851
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
8747
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
8528
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8627
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
6179
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5649
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4335
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2752
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.