Reputation: 5
I have a class MyClass
which has
public enum Days{Mon, Tue, Wed}
and then a field
public Days dayOfWeek;
From another class in my solution I have a string (myString
) value of either 0
, 1
or 2
. I want to set an instance of MyClass
's (called myClassInstance
) field dayOfWeek
equal to myStringvalue
such that 0
means Mon, 1
means Tue...
I have tried
myClassInstance.dayOfWeek = Convert.ToInt32(myString)
and
myClassInstance.dayOfWeek = (int) myString
but neither work. I'm sure this is straightforward. Why don't these techniques work?
Upvotes: 0
Views: 2556
Reputation: 155925
You just need to cast the int
to the Days
enum after converting it:
myClassInstance.dayOfWeek = (Days)Convert.ToInt32(myString);
You can also use Enum.TryParse
(if you're in .NET 4) or Enum.Parse
. Depending on how much your trust the incoming data, you may also want to call Enum.IsDefined
to make sure that the integer is a valid value of Days
(otherwise, in all of these cases, you'll have an instance of Days
that doesn't correspond to any of your named values).
Days dayOfWeek;
if (!Enum.TryParse(myString, out dayOfWeek)) {
dayOfWeek = Days.Mon; // or some other default, or throw
}
myClassInstance.dayOfWeek = dayOfWeek;
Or
myClassInstance.dayOfWeek = (Days)Enum.Parse(typeof(Days), myString);
In addition, as others have mentioned, you may want to consider using the built-in DayOfWeek
enum instead of your custom version, if it matches what you really want.
Also, as others have mentioned again, even if it doesn't, Day
is a better name based on the .NET naming guidelines, since it isn't a Flags
enum.
Upvotes: 6
Reputation: 241641
Try
string s = "0";
Days day = (Days)Enum.Parse(typeof(Days), s);
or
string s = "0";
Days day;
if(!Enum.TryParse(s, out day)) {
// error handling
}
to gracefully handle the case where s
can't be parsed to an instance of Days
.
This works per the documentation for Enum.Parse
which states
Converts the string representation of the name or numeric value of one or more enumerated constants to an equivalent enumerated object.
Additionally, you can check if the instance of string
actually represents a value defined by the enum via
string s = "3";
bool defined = Enum.IsDefined(typeof(Days), s);
// defined is false
Enum.Parse
will blindly parse s
in this case, even though it doesn't represent a value defined by the enum Days
and the cast from the result of Enum.Parse
to Days
will not fail.
Moreover, there is a built-in enum that represents the days of the week. This enum is System.DayOfWeek
. I would suggest using this.
Finally, if for some reason you can't use System.DayOfWeek
, you should at a minimum rename your enum to Day
instead of Days
(remove the pluralization). Only enums that represent flags should be pluralized. Note that the variable day
above represents a day, and it does not represent days. This is why you should rename the enum to Day
. This is consistent with the naming conventions that most C# and .NET programmers use.
Upvotes: 9
Reputation: 57169
This should work
myClassInstance.dayOfWeek = Enum.Parse(typeof(Days), myString);
Upvotes: 0
Reputation: 2032
What you want to do is cast the string to an int (or tryparse if you want to do it in a nice way)
Then use the Following code to set the enum value:
(Days)Enum.ToObject(typeof(Days), intValue);
Upvotes: 0
Reputation: 17428
You need to cast the int
to Days
:
myClassInstance.dayOfWeek = (Days)Convert.ToInt32(myString);
Upvotes: 0
Reputation: 26211
Try:
Days d = (Days)Enum.Parse(typeof(Days), myString);
A more complete example is here.
Upvotes: 2
Reputation: 40235
Try
myClassInstance.dayOfWeek = (Days)int.Parse(myString);
Do you know there is already an Enum that you can resuse called DayOfWeek
(http://msdn.microsoft.com/en-us/library/system.dayofweek.aspx)
Upvotes: 2