Enum with String and Number Request. Extra DayOfWeek






3.40/5 (3 votes)
Easy trick to get your int or string value from your enum. Extra DayOfweek
Introduction
Simple working with an enum
to get a value int
or string out
.
Using the Code
This is an easy example, but works perfectly.
Place the following enum
in a reachable class file.
Public enum Roles
{
Admin = 1,
SuperUser = 2,
Staff = 3
}
Then move along in the code behind (ASP) or controller (MVC).
The stringValue
comes from the enum
by entering the int
between the second pair of brackets.
The intValue
needs the int
type in brackets before the enum
call.
// The answer will be "SuperUser"
var stringValue = ((Roles)2);
// The answer will be 3
var intValue = (int) Roles.Staff;
GOOD LUCK !!!
DayOfWeek
Likely unknown is the DayOfWeek
enum.
Looking at the way of request on the tip above the DayOfWeek
is also working this way and more.
So:
//The answer will be "Monday"
var DayName = ((DayOfWeek)1);
//The answer will be 1
var intDay = (int)DayOfWeek.Monday;
But there is more with this function...
Culture is also possible.
//The answer will be "Tuesday" in the computer preferred language
var stringValue = System.Globalization.DateTimeFormatInfo.CurrentInfo.GetDayName((DayOfWeek)2);
//The Answer will be "We" in your computer preferred language
var stringValue =
System.Globalization.DateTimeFormatInfo.CurrentInfo.GetAbbreviatedDayName((DayOfWeek)3);
I have given you the whole using
, but you can shorten it by setting the using on top with the rest.
using System.Globalization;
Naturally, you can replace the used number with a variable.
GOOD LUCK !!!