Wednesday, 19 January 2022

Enum Return String Value C#

1/19/2022 - By Pranav Singh 0

In this article i will show you ho you can get string value from an enum instead of getting number using c#.  So first we will create a enum.

  enum ProjectStatusEnum

    {

        Open = 0,

        Started = 1,

        InProgress = 2,

        Close = 3,

        Paused = 4,

        OnHold = 5

    }


Now we will take  a static method and add the below code into it. In this code we are passing the enum value an returning the string as per status.

public static class EnumStatus

    {

        public static string Status(int type)

        {

            string statusnew = "";

            switch (type)

            {

                case (int)ProjectStatusEnum.Open:

                    statusnew = "Open";

                    break;

                case (int)ProjectStatusEnum.Started:

                    statusnew = "Started";

                    break;

                case (int)ProjectStatusEnum.InProgress:

                    statusnew = "In-Progress";

                    break;

                case (int)ProjectStatusEnum.Close:

                    statusnew = "Close";

                    break;

                case (int)ProjectStatusEnum.Paused:

                    statusnew = "Paused";

                    break;

                case (int)ProjectStatusEnum.OnHold:

                    statusnew = "On Hold";

                    break;

                default:

                    statusnew = "";

                    break;

            }

            return statusnew;

        }

    }


Now we will add the below code where we need to display the status in text string as per the enum status.

  string status = EnumStatus.Status((int)ProjectStatusEnum.InProgress);

  Console.WriteLine("Current Status: " + status);


The above code will return the string status as per passed enum. Now run the code to check the output.

Enum Return String Value C#

About the Author

We are the group of people who are expertise in different Microsoft technology like Asp.Net,MVC,C#.Net,VB.Net,Windows Application,WPF,jQuery,Javascript,HTML. This blog is designed to share the knowledge.

Get Updates

Subscribe to our e-mail newsletter to receive updates.

Share This Post

0 comments:

Please let me know your view

Free Ebooks


About Us

We are the group of people who are expertise in different Microsoft technology like Asp.Net,MVC,C#.Net,VB.Net,Windows Application,WPF,jQuery,Javascript,HTML. This blog is designed to share the knowledge.

Contact Us

For writing article in this website please send request by your

GMAIL ID: dotnetpools@gmail.com

Bugs and Suggestions

As we all know that this website is for sharing knowledge and providing proper solution. So while reading the article is you find any bug or if you have any suggestion please mail us at contact@aspdotnet-pools.com.

Partners


Global Classified : Connectseekers.com
© 2014 aspdotnet-pools.com Designed by Bloggertheme9.
back to top