none
Enumeration doubt - How to distinguish between two enum members with the same underlying numeric value? RRS feed

  • Question

  • Module Module1 Enum days Monday Tuesday Friday = 0 End Enum

    Sub Main() Console.WriteLine($"{"Value:",-15} {days.Friday,-30}") Console.WriteLine($"{"Numeric Value:",-15} {days.Friday,-30:D}") Console.ReadLine() End Sub End Module

    Question: Why am I getting the output as "Monday" for the statement 
    Console.WriteLine($"{"Value:",-15}  {days.Friday,-30}")

    Is it because both Monday and Friday has the same underlying value and the first one is getting picked? If that is the case, then keeping the underlying value set to 0 for both "Monday" and "Friday",  is there a way to display "Friday" instead of Monday?


    Sougata Ghosh


    • Edited by sougata12 Sunday, December 23, 2018 12:05 PM more specific title
    Sunday, December 23, 2018 9:38 AM

Answers

  • Note this too:

    Console.WriteLine(NameOf(days.Friday)) ' prints "Friday"
    Console.WriteLine(NameOf(days.Monday)) ' prints "Monday"
    

    • Proposed as answer by IronRazerz Sunday, December 23, 2018 10:11 PM
    • Marked as answer by sougata12 Monday, December 24, 2018 1:17 PM
    Sunday, December 23, 2018 2:59 PM

All replies

  •  If you do not assign the values to the enumeration members,  they are automatically assigned an Integer type value starting with the first listed member being assigned a value of 0.  In the enum you have shown,  0 is automatically being assigned to Monday because you have not assigned a value to it,  then you are also telling it that Friday is 0 too.  So,  if you checked the values of each member,  you would find that Monday and Friday both have a value of 0.

     If you want Friday to be 0,  then make it the first in the enum like this...

        Enum days
            Friday   'this is 0
            Saturday 'this is 1
            Sunday   'this is 2
    
            '...rest of days
    
        End Enum   
    
     

     Otherwise you will need to assign each member a value like this...

        Enum days
            Monday = 3
            Tuesday = 4
            Friday = 0
    
            '...other days
    
        End Enum   
    

     

     You can read through the following msdn document to learn more...  Enum Statement (Visual Basic)


    If you say it can`t be done then i`ll try it

    Sunday, December 23, 2018 10:43 AM
  • I know about the integer assignment that you have mentioned and therefore agree to what you say. However my question is different. I rephrase it below:

    Firstly, it is to be noted that VB.net allows two enumerations to be assigned the same integer as in my case where both "Monday" and "Friday" are assigned the value zero. And under such a circumstance the output for the statement  Console.WriteLine($"{"Value:",-15}  {days.Friday,-30}")
    is being shown as Monday and not Friday. My question is: Is there a way to get the output as "Friday"
    instead of "Monday" without changing the integer associations of the enumerations....In short I want to keep
    both Monday and Friday as 0 and then get Friday as the output when access the "Friday" element?..
    Is that possible?


    Sougata Ghosh


    • Edited by sougata12 Sunday, December 23, 2018 11:09 AM
    Sunday, December 23, 2018 11:03 AM
  • I know about the integer assignment that you have mentioned and therefore agree to what you say. However my question is different. I rephrase it below:

    Firstly, it is to be noted that VB.net allows two enumerations to be assigned the same integer as in my case where both "Monday" and "Friday" are assigned the value zero. And under such a circumstance the output for the statement  Console.WriteLine($"{"Value:",-15}  {days.Friday,-30}")
    is being shown as Monday and not Friday. My question is: Is there a way to get the output as "Friday"
    instead of "Monday" without changing the integer associations of the enumerations....In short I want to keep
    both Monday and Friday as 0 and then get Friday as the output when access the "Friday" element?..
    Is that possible?


    Sougata Ghosh



     The first enum member with the value of 0 is going to be shown by default because,  the enum would have no way of knowing which member you wanted it to show.  I suppose there are ways you could make it show Friday but,  how/where in your code is it determined which name you want to show Monday or Friday when you have a value of 0?  Is it always suppose to show Friday no mater if it is the Monday or Friday member?

    If you say it can`t be done then i`ll try it

    Sunday, December 23, 2018 11:30 AM
  • I know about the integer assignment that you have mentioned and therefore agree to what you say. However my question is different. I rephrase it below:

    Firstly, it is to be noted that VB.net allows two enumerations to be assigned the same integer as in my case where both "Monday" and "Friday" are assigned the value zero. And under such a circumstance the output for the statement  Console.WriteLine($"{"Value:",-15}  {days.Friday,-30}")
    is being shown as Monday and not Friday. My question is: Is there a way to get the output as "Friday"
    instead of "Monday" without changing the integer associations of the enumerations....In short I want to keep
    both Monday and Friday as 0 and then get Friday as the output when access the "Friday" element?..
    Is that possible?


    Sougata Ghosh



     The first enum member with the value of 0 is going to be shown by default because,  the enum would have no way of knowing which member you wanted it to show.  I suppose there are ways you could make it show Friday but,  how/where in your code is it determined which name you want to show Monday or Friday when you have a value of 0?  Is it always suppose to show Friday no mater if it is the Monday or Friday member?

    If you say it can`t be done then i`ll try it

    You raised the following two points:

    Point 1: how/where in your code is it determined which name you want to show Monday or Friday when you have a value of 0

    My answer: IF I had written Console.WriteLine($"{"Value:",-15}  {days.Tuesday,-30}") then the output would have been Tuesday. Hence I wrote :
    Console.WriteLine($"{"Value:",-15}  {days.Friday,-30}") I was under the impression it would display Friday. In other words I was of the opinion that since I am mentioning the enumeration member explicitly, the compiler would know which member to point o irrespective of the underlying integer. But seems like it is not the case and hence wanted to know if there is a way around?

    Point 2: Is it always suppose to show Friday no mater if it is the Monday or Friday member?

    My answer: No. What I want is to access the enumeration member as per my choice. And therefore I feel the entire discussion boils down to one question namely: 
    Is there a way to distinguish between two enumeration members with the same underlying numeric value?

    Any help is greatly appreciated.


    Sougata Ghosh


    • Edited by sougata12 Sunday, December 23, 2018 11:55 AM
    Sunday, December 23, 2018 11:45 AM
  • Is there a way to distinguish between two enumeration members with the same underlying numeric value?
     In short then,  no.

    If you say it can`t be done then i`ll try it

    • Marked as answer by sougata12 Sunday, December 23, 2018 12:22 PM
    • Unmarked as answer by sougata12 Monday, December 24, 2018 1:26 PM
    Sunday, December 23, 2018 12:20 PM
  • Is there a way to distinguish between two enumeration members with the same underlying numeric value?

     In short then,  no.

    If you say it can`t be done then i`ll try it


    Thanks. Got my answer.

    Sougata Ghosh

    Sunday, December 23, 2018 12:22 PM
  • Note this too:

    Console.WriteLine(NameOf(days.Friday)) ' prints "Friday"
    Console.WriteLine(NameOf(days.Monday)) ' prints "Monday"
    

    • Proposed as answer by IronRazerz Sunday, December 23, 2018 10:11 PM
    • Marked as answer by sougata12 Monday, December 24, 2018 1:17 PM
    Sunday, December 23, 2018 2:59 PM
  • Note this too:

    Console.WriteLine(NameOf(days.Friday)) ' prints "Friday"
    Console.WriteLine(NameOf(days.Monday)) ' prints "Monday"
    


    Thanks Viorel...Very helpful

    Sougata Ghosh

    Monday, December 24, 2018 1:18 PM