Using Enums in C#
I have a Windows Application that is accepting a string via command line parameter.
The string is going to be one of the following: 19, 20, 21, 23, 25. This is an ID that is used to build a URL dynamically.
I've used enums in plenty of apps before, but I've never actually built an enum from scratch so (I feel foolish saying this), I don't entirely understand them.
Based on the string that's passed in, I want to set a UI label. The corresponding val开发者_StackOverflow社区ues I want to display are as follows (recall the # value is the string that's passed in):
19 = Facebook
20 = Twitter
21 = YouTube
23 = Flickr
25 = Blogs
So if 23 is passed in, my label would say "Flickr". Is this something I can do with enums?
public Form1(string collectorID)
{
InitializeComponent();
this.CollectorID = collectorID;
labelCollector.Text = // Set label here
WebConnection.Create(CollectorID);
}
Thanks!
Scott
You could do this with enums, but you probably shouldn't. Values of a enum
are meant to be used in code, not displayed to users. What happens if you want to display “Foo-Bar Service”?
Because of this and because it seems you actually just want a mapping between numbers and strings, you should do just that: use Dictionary<int, string>
or a switch
.
Yes you can. Have a look at MSDN: enum
It doesn't sound like something that should be done with an enum, but if thats your solution you can set your enum as:
public enum MyEnum
{
Facebook = 19,
Twitter = 20
.
.
}
Edit:
To get the value name:
string s = Enum.GetName(typeof (MyEnum), 19);
enum Sites
{
Facebook = 19,
Twitter = 20,
YouTube = 21,
Flickr = 23,
Blogs = 25,
}
//
int i = 19;
if (!Enum.IsDefined(typeof(Sites), i))
{
throw new Exception();
}
string str = ((Sites)i).ToString();
Be aware that many programmers (including me) consider using the ToString()
of an enum a little "foolish" (this because you are making everything "immutable unless you modify the program" and "unlocalizable")
I'll add that you could do it with a Dictionary
(a little better, the Dictionary could be loaded dynamically):
Dictionary<int, string> sites = new Dictionary<int, string>()
{
{ 19, "Facebook" },
{ 20, "Twitter" },
{ 21, "YouTube" },
{ 23, "Flickr" },
{ 25, "Blogs" },
};
//
string str2;
if (!sites.TryGetValue(i, out str2))
{
throw new Exception();
}
// str2 contains your site description
public enum MyEnum
{
Facebook = 19,
Twitter = 20,
YouTube = 21,
Flickr = 23,
Blogs = 25,
}
public Form1(MyEnum collectorID)
{
InitializeComponent();
this.CollectorID = collectorID;
labelCollector.Text = collectorId.toString();
WebConnection.Create(CollectorID);
}
Use it in following way:
public enum UrlType
{
Facebook = 19,
Twitter = 20,
YouTube = 21,
Flickr = 23,
Blogs = 25
}
public string ParseUrlType(string type)
{
int urlTypeId = Convert.ToInt32(type);
if (!Enum.IsDefined(typeof(UrlType), urlTypeId))
return null;
UrlType urlType = (UrlType)urlTypeId;
return urlType.ToString();
}
Use Enum.IsDefined to filter out invalid input and store names in a dictionary.
public enum SocialNetwork {
Facebook = 19,
Twitter = 20,
YouTube = 21,
Flickr = 23,
Blogs = 25
}
var names = new Dictionary<SocialNetwork, String>() {
{ SocialNetwork.Facebook, "Facebook"}
...
};
String input = /*Read user input*/;
Int32 socialNetworkInteger;
if (Int32.TryParse(input, out socialNetworkInteger)) {
if (Enum.IsDefined(typeof(SocialNetwork), socialNetworkInteger)) {
String nameToDisplay = names[(SocialNetwork) socialNetworkInteger];
}
}
精彩评论