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.
var stringValue = ((Roles)2);
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:
var DayName = ((DayOfWeek)1);
var intDay = (int)DayOfWeek.Monday;
But there is more with this function...
Culture is also possible.
var stringValue = System.Globalization.DateTimeFormatInfo.CurrentInfo.GetDayName((DayOfWeek)2);
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 !!!
20 years IT, Sinds 2010 development.
C#, asp.net, Blazor, MVC, html, css, VB.net, SQL, javascript, jquery, xml, linq.