Click here to Skip to main content
15,898,987 members
Articles / Programming Languages / C#
Tip/Trick

Enum with String and Number Request. Extra DayOfWeek

Rate me:
Please Sign up or sign in to vote.
3.40/5 (3 votes)
17 Dec 2014CPOL 15.9K   4   3
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.

C#
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.

C#
// 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:

C#
//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.

C#
//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.

C#
using System.Globalization;

Naturally, you can replace the used number with a variable.

GOOD LUCK !!!

License

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


Written By
Software Developer
Netherlands Netherlands
20 years IT, Sinds 2010 development.
C#, asp.net, Blazor, MVC, html, css, VB.net, SQL, javascript, jquery, xml, linq.

Comments and Discussions

 
GeneralMy vote of 1 Pin
Tomas Takac18-Dec-14 23:31
Tomas Takac18-Dec-14 23:31 
GeneralRe: My vote of 1 Pin
Dinand.dotnet30-Dec-14 3:19
professionalDinand.dotnet30-Dec-14 3:19 
Hello Thomas,
Thanx for your comments.
The point of the article is to bring this to the attention. A number of things are quite unknown by some developers. Naturely is notting new.

Thanx for the tips, I've changed the hotspots.
Waiting for the day, I'll solve your problem.
Regards Dinand

Generalu Pin
Prakash Kumar 2717-Dec-14 22:54
Prakash Kumar 2717-Dec-14 22:54 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.