Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. Enum / Cast Question

Enum / Cast Question

Scheduled Pinned Locked Moved C#
questionhelp
9 Posts 7 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • K Offline
    K Offline
    Kevin Marois
    wrote on last edited by
    #1

    The docs for Enum says (paraphrasing) "Enumerations have an underlying type used for storage, which by default is int" So I have:

    using System;

    namespace Employee_Example1
    {
    public enum EmployeeType
    {
    FullTime = 0,
    PartTime = 1,
    Contract = 2,
    Other = 3
    }

    public class Employee\_Base
    {
    		
    	public int iEmpId = 0;
    	public int iEmployeeType = EmployeeType.FullTime;
    	public double dSalary = 0.00;
    	public string sLastName = "";
    	public string sFirstName = "";
    		
    	public Employee\_Base()
    	{
    	}
    }
    

    }

    So why am I getting the error "Cannot implicitly convert type 'Employee_Example1.EmployeeType' to 'int'" I have also tried "public enum EmployeeType : int", and it still won't compile. However, this works:

    public int iEmployeeType = (int)EmployeeType.FullTime;

    The question is, if the enum's underlying type is int, why do I need to cast?

    Everything makes sense in someone's mind

    L M T D 4 Replies Last reply
    0
    • K Kevin Marois

      The docs for Enum says (paraphrasing) "Enumerations have an underlying type used for storage, which by default is int" So I have:

      using System;

      namespace Employee_Example1
      {
      public enum EmployeeType
      {
      FullTime = 0,
      PartTime = 1,
      Contract = 2,
      Other = 3
      }

      public class Employee\_Base
      {
      		
      	public int iEmpId = 0;
      	public int iEmployeeType = EmployeeType.FullTime;
      	public double dSalary = 0.00;
      	public string sLastName = "";
      	public string sFirstName = "";
      		
      	public Employee\_Base()
      	{
      	}
      }
      

      }

      So why am I getting the error "Cannot implicitly convert type 'Employee_Example1.EmployeeType' to 'int'" I have also tried "public enum EmployeeType : int", and it still won't compile. However, this works:

      public int iEmployeeType = (int)EmployeeType.FullTime;

      The question is, if the enum's underlying type is int, why do I need to cast?

      Everything makes sense in someone's mind

      L Offline
      L Offline
      Lost User
      wrote on last edited by
      #2

      There's unfortunately no implicit conversion from enum to int, although it should be possible without problems. I guess it's for the sake of type safety, C# is a bit restrictive here.

      1 Reply Last reply
      0
      • K Kevin Marois

        The docs for Enum says (paraphrasing) "Enumerations have an underlying type used for storage, which by default is int" So I have:

        using System;

        namespace Employee_Example1
        {
        public enum EmployeeType
        {
        FullTime = 0,
        PartTime = 1,
        Contract = 2,
        Other = 3
        }

        public class Employee\_Base
        {
        		
        	public int iEmpId = 0;
        	public int iEmployeeType = EmployeeType.FullTime;
        	public double dSalary = 0.00;
        	public string sLastName = "";
        	public string sFirstName = "";
        		
        	public Employee\_Base()
        	{
        	}
        }
        

        }

        So why am I getting the error "Cannot implicitly convert type 'Employee_Example1.EmployeeType' to 'int'" I have also tried "public enum EmployeeType : int", and it still won't compile. However, this works:

        public int iEmployeeType = (int)EmployeeType.FullTime;

        The question is, if the enum's underlying type is int, why do I need to cast?

        Everything makes sense in someone's mind

        M Offline
        M Offline
        Mark Salsbery
        wrote on last edited by
        #3

        From the enum docs[^]: "The underlying type specifies how much storage is allocated for each enumerator. However, an explicit cast is needed to convert from enum type to an integral type."

        Mark Salsbery Microsoft MVP - Visual C++ :java:

        1 Reply Last reply
        0
        • K Kevin Marois

          The docs for Enum says (paraphrasing) "Enumerations have an underlying type used for storage, which by default is int" So I have:

          using System;

          namespace Employee_Example1
          {
          public enum EmployeeType
          {
          FullTime = 0,
          PartTime = 1,
          Contract = 2,
          Other = 3
          }

          public class Employee\_Base
          {
          		
          	public int iEmpId = 0;
          	public int iEmployeeType = EmployeeType.FullTime;
          	public double dSalary = 0.00;
          	public string sLastName = "";
          	public string sFirstName = "";
          		
          	public Employee\_Base()
          	{
          	}
          }
          

          }

          So why am I getting the error "Cannot implicitly convert type 'Employee_Example1.EmployeeType' to 'int'" I have also tried "public enum EmployeeType : int", and it still won't compile. However, this works:

          public int iEmployeeType = (int)EmployeeType.FullTime;

          The question is, if the enum's underlying type is int, why do I need to cast?

          Everything makes sense in someone's mind

          T Offline
          T Offline
          TheFM234
          wrote on last edited by
          #4

          As Greeeg said above, there is no implicit conversion for a enum into an int. All conversions need to be done explicitly. Just a question/though about your base class: why are you storing the enum as an int? I think for program clarity and maintainability, storing EmployeeType as the enum type would be the best way to go (and it will be more clear if your going to have an object out of the base).

          1 Reply Last reply
          0
          • K Kevin Marois

            The docs for Enum says (paraphrasing) "Enumerations have an underlying type used for storage, which by default is int" So I have:

            using System;

            namespace Employee_Example1
            {
            public enum EmployeeType
            {
            FullTime = 0,
            PartTime = 1,
            Contract = 2,
            Other = 3
            }

            public class Employee\_Base
            {
            		
            	public int iEmpId = 0;
            	public int iEmployeeType = EmployeeType.FullTime;
            	public double dSalary = 0.00;
            	public string sLastName = "";
            	public string sFirstName = "";
            		
            	public Employee\_Base()
            	{
            	}
            }
            

            }

            So why am I getting the error "Cannot implicitly convert type 'Employee_Example1.EmployeeType' to 'int'" I have also tried "public enum EmployeeType : int", and it still won't compile. However, this works:

            public int iEmployeeType = (int)EmployeeType.FullTime;

            The question is, if the enum's underlying type is int, why do I need to cast?

            Everything makes sense in someone's mind

            D Offline
            D Offline
            DaveyM69
            wrote on last edited by
            #5

            KMAROIS wrote:

            which by default is int

            It can be other types including strings so explicit casting is the only logical way - implicit would create a whole bunch of problems!

            Dave
            BTW, in software, hope and pray is not a viable strategy. (Luc Pattyn)
            Expect everything to be hard and then enjoy the things that come easy. (code-frog)

            C 1 Reply Last reply
            0
            • D DaveyM69

              KMAROIS wrote:

              which by default is int

              It can be other types including strings so explicit casting is the only logical way - implicit would create a whole bunch of problems!

              Dave
              BTW, in software, hope and pray is not a viable strategy. (Luc Pattyn)
              Expect everything to be hard and then enjoy the things that come easy. (code-frog)

              C Offline
              C Offline
              chaiguy1337
              wrote on last edited by
              #6

              Do you know how to create enums based on strings? That would be very useful to me.

              “Time and space can be a bitch.” –Gushie, Quantum Leap {o,o}.oO( Looking for a great RSS reader? Try FeedBeast! ) |)””’)            Built with home-grown CodeProject components! -”-”-

              N D 2 Replies Last reply
              0
              • C chaiguy1337

                Do you know how to create enums based on strings? That would be very useful to me.

                “Time and space can be a bitch.” –Gushie, Quantum Leap {o,o}.oO( Looking for a great RSS reader? Try FeedBeast! ) |)””’)            Built with home-grown CodeProject components! -”-”-

                N Offline
                N Offline
                N a v a n e e t h
                wrote on last edited by
                #7

                Do you mean putting strings rather than int ? AFAIK, you can use attributes for the enum and specify the string value there, use reflection to read the attribute and it's value. I have seen some good articles here written by PiebelConsult and Scott dorman.

                All C# applications should call Application.Quit(); in the beginning to avoid any .NET problems.- Unclyclopedia How to use google | Ask smart questions

                1 Reply Last reply
                0
                • C chaiguy1337

                  Do you know how to create enums based on strings? That would be very useful to me.

                  “Time and space can be a bitch.” –Gushie, Quantum Leap {o,o}.oO( Looking for a great RSS reader? Try FeedBeast! ) |)””’)            Built with home-grown CodeProject components! -”-”-

                  D Offline
                  D Offline
                  DaveyM69
                  wrote on last edited by
                  #8

                  Navaneeth is correct in his response - I was wrong, you can't use strings directly :-O . I knew I'd used them before but forgot that I'd used attributes to do it. There's many examples around of different methods of achieving a string enum. An interesting one is here[^].

                  Dave
                  BTW, in software, hope and pray is not a viable strategy. (Luc Pattyn)
                  Expect everything to be hard and then enjoy the things that come easy. (code-frog)

                  C 1 Reply Last reply
                  0
                  • D DaveyM69

                    Navaneeth is correct in his response - I was wrong, you can't use strings directly :-O . I knew I'd used them before but forgot that I'd used attributes to do it. There's many examples around of different methods of achieving a string enum. An interesting one is here[^].

                    Dave
                    BTW, in software, hope and pray is not a viable strategy. (Luc Pattyn)
                    Expect everything to be hard and then enjoy the things that come easy. (code-frog)

                    C Offline
                    C Offline
                    chaiguy1337
                    wrote on last edited by
                    #9

                    Lol no worries. The attribute trick might be worth it, although with reflection you can get the name of an enum constant (of course these are subject to C# naming rules). That library looks interesting. Thanks to both of you for the responses.

                    “Time and space can be a bitch.” –Gushie, Quantum Leap {o,o}.oO( Looking for a great RSS reader? Try FeedBeast! ) |)””’)            Built with home-grown CodeProject components! -”-”-

                    1 Reply Last reply
                    0
                    Reply
                    • Reply as topic
                    Log in to reply
                    • Oldest to Newest
                    • Newest to Oldest
                    • Most Votes


                    • Login

                    • Don't have an account? Register

                    • Login or register to search.
                    • First post
                      Last post
                    0
                    • Categories
                    • Recent
                    • Tags
                    • Popular
                    • World
                    • Users
                    • Groups