Click here to Skip to main content
15,895,011 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have a class reference:
DeviceType deviceType

and a string type variable
string device_Type

How do i store the value from deviceType to this class type reference deviceType?


here is the code:
C#
public enum DeviceType
   {
       Unknown,
       Mesa = 1,
       Pulnix,
       Vitrionics,
       DriveSafe
   }


and
i have :
string deviceType;
now whatever value i am getting I need to store in this DeviceType variable.
i mean


DeviceType dev_Type;
dev_Type=<here i="" need="" to="" get="" devicetype="" which="" is="" string="" type="" data=""> ??
Posted
Updated 5-Aug-14 2:49am
v2
Comments
johannesnestler 5-Aug-14 8:46am    
It's quite hard to understand what you want and would could be your problem - I assume you are a beginner and this ia a "conceptual" question. So try to tell what you want to do in the end, because the question doesn't make any sense to me- It's hard to guess WHAT you don't understand. (if you have problems with basics better (re-)work through any c# tutorial). So what is a "class type reference" and a "string type value"? What I think you have is a "string representation" (XML?) of your DeviceType - so you may ask for how to "parse" it or something like that?
Anshumaan Chaturvedi 5-Aug-14 8:50am    
please see the updated question
johannesnestler 5-Aug-14 9:08am    
posted solution - please accept if it is what you want.

Hi again Anshumaan,

So now I understand your Problem. The question is how to covert between an enumeration value and it's string representation (Name). You are lucky - that's quite easy in .net - ther is a Parse function implemented on the Enum class. Use it like this:

C#
string strDeviceType = "Pulnix";
DeviceType devicetype = (DeviceType) Enum.Parse(typeof(DeviceType), strDeviceType);


Cool, I think this is what you asked for, isn't it?

Kind regards
Johannes
 
Share this answer
 
Comments
Anshumaan Chaturvedi 5-Aug-14 9:30am    
Thanks you Sir. Yes I am a beginner. 3 months only into .NET. Long way to go.
Cheers!!
thanks again for your help and sharing the knowledge
johannesnestler 5-Aug-14 9:33am    
you are welcome - glad we solved it! Keep on learning and that way get's shorter and shorter ;-)
In your example code, DeviceType is not a class, it is an enum, so the variable deviceType is a enum as well (and isn't a reference at all, it's a ValueType)

You cannot add, store or otherwise include a string in an enum: it is a integer based variable type and cannot contain anythign else.

You could create a containing class which holds them both:
C#
public class MyContainer
   {
   public DeviceType DeviceType { get; set; }
   public string Text { get; set; }
   }
 
Share this answer
 
Comments
Anshumaan Chaturvedi 5-Aug-14 9:02am    
some implementation example links ?
OriginalGriff 5-Aug-14 9:39am    
Sorry? Links to what?
The example is complete in itself...

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900