How to print an Enum Value based on an integer user input?












0















I've a got an Enum that looks like so:



 public enum Month 
{
January, February, March,
April, May, June, July, August,
Septemper, October, November, December
};


What I need to really do is ask and read from the user a number also like so:



Console.WriteLine("Enter the number of the 
month")
int monthValue=int.parse(Console.ReadLine())


Lastly I wanna take the monthValue and print the equivalent Enum.
(e.g april for monthvalue 4)










share|improve this question























  • just cast it. (Month)monthValue

    – Selman Genç
    Nov 21 '18 at 14:15






  • 3





    Possible duplicate of Cast int to enum in C#

    – Foo
    Nov 21 '18 at 14:19











  • I removed my answer due to duplication

    – Prasad Telkikar
    Nov 21 '18 at 14:23






  • 1





    It's quite useful for this type of enum to tell it to start from 1 - then you don't have to worry about forgetting to subtract the value to get the right result - public enum Month { January = 1, February, [etc],

    – stuartd
    Nov 21 '18 at 14:36
















0















I've a got an Enum that looks like so:



 public enum Month 
{
January, February, March,
April, May, June, July, August,
Septemper, October, November, December
};


What I need to really do is ask and read from the user a number also like so:



Console.WriteLine("Enter the number of the 
month")
int monthValue=int.parse(Console.ReadLine())


Lastly I wanna take the monthValue and print the equivalent Enum.
(e.g april for monthvalue 4)










share|improve this question























  • just cast it. (Month)monthValue

    – Selman Genç
    Nov 21 '18 at 14:15






  • 3





    Possible duplicate of Cast int to enum in C#

    – Foo
    Nov 21 '18 at 14:19











  • I removed my answer due to duplication

    – Prasad Telkikar
    Nov 21 '18 at 14:23






  • 1





    It's quite useful for this type of enum to tell it to start from 1 - then you don't have to worry about forgetting to subtract the value to get the right result - public enum Month { January = 1, February, [etc],

    – stuartd
    Nov 21 '18 at 14:36














0












0








0








I've a got an Enum that looks like so:



 public enum Month 
{
January, February, March,
April, May, June, July, August,
Septemper, October, November, December
};


What I need to really do is ask and read from the user a number also like so:



Console.WriteLine("Enter the number of the 
month")
int monthValue=int.parse(Console.ReadLine())


Lastly I wanna take the monthValue and print the equivalent Enum.
(e.g april for monthvalue 4)










share|improve this question














I've a got an Enum that looks like so:



 public enum Month 
{
January, February, March,
April, May, June, July, August,
Septemper, October, November, December
};


What I need to really do is ask and read from the user a number also like so:



Console.WriteLine("Enter the number of the 
month")
int monthValue=int.parse(Console.ReadLine())


Lastly I wanna take the monthValue and print the equivalent Enum.
(e.g april for monthvalue 4)







c# enums






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Nov 21 '18 at 14:14









Abdul SHAbdul SH

84




84













  • just cast it. (Month)monthValue

    – Selman Genç
    Nov 21 '18 at 14:15






  • 3





    Possible duplicate of Cast int to enum in C#

    – Foo
    Nov 21 '18 at 14:19











  • I removed my answer due to duplication

    – Prasad Telkikar
    Nov 21 '18 at 14:23






  • 1





    It's quite useful for this type of enum to tell it to start from 1 - then you don't have to worry about forgetting to subtract the value to get the right result - public enum Month { January = 1, February, [etc],

    – stuartd
    Nov 21 '18 at 14:36



















  • just cast it. (Month)monthValue

    – Selman Genç
    Nov 21 '18 at 14:15






  • 3





    Possible duplicate of Cast int to enum in C#

    – Foo
    Nov 21 '18 at 14:19











  • I removed my answer due to duplication

    – Prasad Telkikar
    Nov 21 '18 at 14:23






  • 1





    It's quite useful for this type of enum to tell it to start from 1 - then you don't have to worry about forgetting to subtract the value to get the right result - public enum Month { January = 1, February, [etc],

    – stuartd
    Nov 21 '18 at 14:36

















just cast it. (Month)monthValue

– Selman Genç
Nov 21 '18 at 14:15





just cast it. (Month)monthValue

– Selman Genç
Nov 21 '18 at 14:15




3




3





Possible duplicate of Cast int to enum in C#

– Foo
Nov 21 '18 at 14:19





Possible duplicate of Cast int to enum in C#

– Foo
Nov 21 '18 at 14:19













I removed my answer due to duplication

– Prasad Telkikar
Nov 21 '18 at 14:23





I removed my answer due to duplication

– Prasad Telkikar
Nov 21 '18 at 14:23




1




1





It's quite useful for this type of enum to tell it to start from 1 - then you don't have to worry about forgetting to subtract the value to get the right result - public enum Month { January = 1, February, [etc],

– stuartd
Nov 21 '18 at 14:36





It's quite useful for this type of enum to tell it to start from 1 - then you don't have to worry about forgetting to subtract the value to get the right result - public enum Month { January = 1, February, [etc],

– stuartd
Nov 21 '18 at 14:36












2 Answers
2






active

oldest

votes


















1














You can simply cast the value to the enum. Don't forget to set initial value for January or take into account that by default enum starts from 0;



Console application will be next:



class Program
{

public enum Month
{
January, February, March,
April, May, June, July, August,
Septemper, October, November, December
};

static void Main(string args)
{
Console.WriteLine("Enter the number of the month");

int monthValue = 0;
int.TryParse(Console.ReadLine(), out monthValue);
Console.WriteLine((Month)monthValue - 1);
Console.ReadKey();
}
}


in case you don't need temporary variable you could also really convert it to enum. But not forget to set default enum value



    public enum Month
{
January = 1, February, March,
April, May, June, July, August,
Septemper, October, November, December
};

static void Main(string args)
{
Console.WriteLine("Enter the number of the month");
var input = Enum.Parse(typeof(Month), Console.ReadLine());
Console.WriteLine(input);
Console.ReadKey();
}





share|improve this answer





















  • 1





    Instead of casting the integer to an enum, you can use Enum.Parse docs.microsoft.com/en-us/dotnet/api/…

    – Isitar
    Nov 21 '18 at 14:24



















1














The following code prints the name of the month to the console. It uses the static Enum.GetName()-method for that.



string monthName = Enum.GetName(typeof(Month), monthValue - 1);
Console.WriteLine(monthName);





share|improve this answer

























    Your Answer






    StackExchange.ifUsing("editor", function () {
    StackExchange.using("externalEditor", function () {
    StackExchange.using("snippets", function () {
    StackExchange.snippets.init();
    });
    });
    }, "code-snippets");

    StackExchange.ready(function() {
    var channelOptions = {
    tags: "".split(" "),
    id: "1"
    };
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function() {
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled) {
    StackExchange.using("snippets", function() {
    createEditor();
    });
    }
    else {
    createEditor();
    }
    });

    function createEditor() {
    StackExchange.prepareEditor({
    heartbeatType: 'answer',
    autoActivateHeartbeat: false,
    convertImagesToLinks: true,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: 10,
    bindNavPrevention: true,
    postfix: "",
    imageUploader: {
    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
    allowUrls: true
    },
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    });


    }
    });














    draft saved

    draft discarded


















    StackExchange.ready(
    function () {
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53414024%2fhow-to-print-an-enum-value-based-on-an-integer-user-input%23new-answer', 'question_page');
    }
    );

    Post as a guest















    Required, but never shown

























    2 Answers
    2






    active

    oldest

    votes








    2 Answers
    2






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes









    1














    You can simply cast the value to the enum. Don't forget to set initial value for January or take into account that by default enum starts from 0;



    Console application will be next:



    class Program
    {

    public enum Month
    {
    January, February, March,
    April, May, June, July, August,
    Septemper, October, November, December
    };

    static void Main(string args)
    {
    Console.WriteLine("Enter the number of the month");

    int monthValue = 0;
    int.TryParse(Console.ReadLine(), out monthValue);
    Console.WriteLine((Month)monthValue - 1);
    Console.ReadKey();
    }
    }


    in case you don't need temporary variable you could also really convert it to enum. But not forget to set default enum value



        public enum Month
    {
    January = 1, February, March,
    April, May, June, July, August,
    Septemper, October, November, December
    };

    static void Main(string args)
    {
    Console.WriteLine("Enter the number of the month");
    var input = Enum.Parse(typeof(Month), Console.ReadLine());
    Console.WriteLine(input);
    Console.ReadKey();
    }





    share|improve this answer





















    • 1





      Instead of casting the integer to an enum, you can use Enum.Parse docs.microsoft.com/en-us/dotnet/api/…

      – Isitar
      Nov 21 '18 at 14:24
















    1














    You can simply cast the value to the enum. Don't forget to set initial value for January or take into account that by default enum starts from 0;



    Console application will be next:



    class Program
    {

    public enum Month
    {
    January, February, March,
    April, May, June, July, August,
    Septemper, October, November, December
    };

    static void Main(string args)
    {
    Console.WriteLine("Enter the number of the month");

    int monthValue = 0;
    int.TryParse(Console.ReadLine(), out monthValue);
    Console.WriteLine((Month)monthValue - 1);
    Console.ReadKey();
    }
    }


    in case you don't need temporary variable you could also really convert it to enum. But not forget to set default enum value



        public enum Month
    {
    January = 1, February, March,
    April, May, June, July, August,
    Septemper, October, November, December
    };

    static void Main(string args)
    {
    Console.WriteLine("Enter the number of the month");
    var input = Enum.Parse(typeof(Month), Console.ReadLine());
    Console.WriteLine(input);
    Console.ReadKey();
    }





    share|improve this answer





















    • 1





      Instead of casting the integer to an enum, you can use Enum.Parse docs.microsoft.com/en-us/dotnet/api/…

      – Isitar
      Nov 21 '18 at 14:24














    1












    1








    1







    You can simply cast the value to the enum. Don't forget to set initial value for January or take into account that by default enum starts from 0;



    Console application will be next:



    class Program
    {

    public enum Month
    {
    January, February, March,
    April, May, June, July, August,
    Septemper, October, November, December
    };

    static void Main(string args)
    {
    Console.WriteLine("Enter the number of the month");

    int monthValue = 0;
    int.TryParse(Console.ReadLine(), out monthValue);
    Console.WriteLine((Month)monthValue - 1);
    Console.ReadKey();
    }
    }


    in case you don't need temporary variable you could also really convert it to enum. But not forget to set default enum value



        public enum Month
    {
    January = 1, February, March,
    April, May, June, July, August,
    Septemper, October, November, December
    };

    static void Main(string args)
    {
    Console.WriteLine("Enter the number of the month");
    var input = Enum.Parse(typeof(Month), Console.ReadLine());
    Console.WriteLine(input);
    Console.ReadKey();
    }





    share|improve this answer















    You can simply cast the value to the enum. Don't forget to set initial value for January or take into account that by default enum starts from 0;



    Console application will be next:



    class Program
    {

    public enum Month
    {
    January, February, March,
    April, May, June, July, August,
    Septemper, October, November, December
    };

    static void Main(string args)
    {
    Console.WriteLine("Enter the number of the month");

    int monthValue = 0;
    int.TryParse(Console.ReadLine(), out monthValue);
    Console.WriteLine((Month)monthValue - 1);
    Console.ReadKey();
    }
    }


    in case you don't need temporary variable you could also really convert it to enum. But not forget to set default enum value



        public enum Month
    {
    January = 1, February, March,
    April, May, June, July, August,
    Septemper, October, November, December
    };

    static void Main(string args)
    {
    Console.WriteLine("Enter the number of the month");
    var input = Enum.Parse(typeof(Month), Console.ReadLine());
    Console.WriteLine(input);
    Console.ReadKey();
    }






    share|improve this answer














    share|improve this answer



    share|improve this answer








    edited Nov 21 '18 at 14:28

























    answered Nov 21 '18 at 14:22









    IlliaIllia

    814




    814








    • 1





      Instead of casting the integer to an enum, you can use Enum.Parse docs.microsoft.com/en-us/dotnet/api/…

      – Isitar
      Nov 21 '18 at 14:24














    • 1





      Instead of casting the integer to an enum, you can use Enum.Parse docs.microsoft.com/en-us/dotnet/api/…

      – Isitar
      Nov 21 '18 at 14:24








    1




    1





    Instead of casting the integer to an enum, you can use Enum.Parse docs.microsoft.com/en-us/dotnet/api/…

    – Isitar
    Nov 21 '18 at 14:24





    Instead of casting the integer to an enum, you can use Enum.Parse docs.microsoft.com/en-us/dotnet/api/…

    – Isitar
    Nov 21 '18 at 14:24













    1














    The following code prints the name of the month to the console. It uses the static Enum.GetName()-method for that.



    string monthName = Enum.GetName(typeof(Month), monthValue - 1);
    Console.WriteLine(monthName);





    share|improve this answer






























      1














      The following code prints the name of the month to the console. It uses the static Enum.GetName()-method for that.



      string monthName = Enum.GetName(typeof(Month), monthValue - 1);
      Console.WriteLine(monthName);





      share|improve this answer




























        1












        1








        1







        The following code prints the name of the month to the console. It uses the static Enum.GetName()-method for that.



        string monthName = Enum.GetName(typeof(Month), monthValue - 1);
        Console.WriteLine(monthName);





        share|improve this answer















        The following code prints the name of the month to the console. It uses the static Enum.GetName()-method for that.



        string monthName = Enum.GetName(typeof(Month), monthValue - 1);
        Console.WriteLine(monthName);






        share|improve this answer














        share|improve this answer



        share|improve this answer








        edited Nov 21 '18 at 14:37

























        answered Nov 21 '18 at 14:30









        Stefan IllnerStefan Illner

        165211




        165211






























            draft saved

            draft discarded




















































            Thanks for contributing an answer to Stack Overflow!


            • Please be sure to answer the question. Provide details and share your research!

            But avoid



            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.


            To learn more, see our tips on writing great answers.




            draft saved


            draft discarded














            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53414024%2fhow-to-print-an-enum-value-based-on-an-integer-user-input%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown





















































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown

































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown







            Popular posts from this blog

            "Incorrect syntax near the keyword 'ON'. (on update cascade, on delete cascade,)

            Alcedinidae

            RAC Tourist Trophy