none
Showing combined enum results as string RRS feed

  • Question

  • Hi

    Hopefully this will make sense, as couldn't get wording right for title!

    I have an Enum, and I set a string property to one or more of the enum values

    I then want to be able to show it on screen, but showing the Enum name to string not just the enum value

    The below code explains my question better - as you can see if I just store color.red then when I show mycolors.tostring it correctly shows "Red"

    But if I store multiple values from my enum, then .tostring shows the number of all values added up e.g. 13, when I want it to show each entries string value e.g. "Red, Blue, Yellow"

    Not sure where I am going wrong, or how best to achieve my requirement

    Thanks

    Public Class Form1
        
        Enum Colors
            Red = 1
            Green = 2
            Blue = 4
            Yellow = 8
        End Enum 
        
        
        Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
            Dim myColors As Colors = Colors.Red 
            msgbox("myColors = " & myColors.tostring)
        End Sub
    
        Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
            Dim myColors As Colors = Colors.Red Or Colors.Blue Or Colors.Yellow
            msgbox("myColors = " & myColors.tostring)
        End Sub
    
    End Class
    
    
    
    


    Darren Rose

    Tuesday, September 3, 2019 2:20 PM

Answers

  • Well I did already know what they were, hence why I said it - it was a fact not a slur on your help which I always appreciate

    That does show output I want but takes away from whole point of using Enum as values I am showing above are only a small subset of what is really going in the Enum

    Anyway not to worry I have found a solution which I will cover below for others if they have same question:-

    Public Class Form1
    
        Enum Colors
            Red = 1
            Green = 2
            Blue = 4
            Yellow = 8
        End Enum
    
        Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
            Dim myColors1 As Colors = Colors.Red
           Dim myColors2 As Colors = Colors.Red Or Colors.Blue Or Colors.Yellow
            MsgBox("myColors1 = " & myColors1.ToString & vbCrLf & "myColors2 = " & mycolors2.tostring)
        End Sub
    
    End Class


    Produces this output

    Adding <Flags> above my Enum e.g.

    Public Class Form1
    
        <Flags>
        Enum Colors
            Red = 1
            Green = 2
            Blue = 4
            Yellow = 8
        End Enum
    
        Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
            Dim myColors1 As Colors = Colors.Red
           Dim myColors2 As Colors = Colors.Red Or Colors.Blue Or Colors.Yellow
            MsgBox("myColors1 = " & myColors1.ToString & vbCrLf & "myColors2 = " & mycolors2.tostring)
        End Sub
    
    End Class

    Gives my desired output of 


    Darren Rose



    • Proposed as answer by leshay Tuesday, September 3, 2019 4:27 PM
    • Edited by wingers Tuesday, September 3, 2019 4:27 PM
    • Marked as answer by wingers Tuesday, September 3, 2019 4:27 PM
    Tuesday, September 3, 2019 4:03 PM

All replies

  • Hi

    Give this a try. Here I have used a ListBox to display the data, but you could just as easily use the collection in any other way.

    ListBox1.DataSource = System.Enum.GetNames(GetType(Colors))


    Regards Les, Livingston, Scotland



    • Edited by leshay Tuesday, September 3, 2019 2:53 PM
    Tuesday, September 3, 2019 2:52 PM
  • Hi Les

    That just shows me a list of the four colors in my Enum? I already know what they are as listed in the code

    What I meant was as per example code

    Clicking button 1 shows - "myColors = Red"

    Clicking button 2 shows - "myColors = 13"

    I want button 2 to show all colours added to myColors e.g. "Red, Blue, Yellow"


    Darren Rose

    Tuesday, September 3, 2019 2:57 PM
  • Hi Les

    That just shows me a list of the four colors in my Enum? I already know what they are as listed in the code

    What I meant was as per example code

    Clicking button 1 shows - "myColors = Red"

    Clicking button 2 shows - "myColors = 13"

    I want button 2 to show all colours added to myColors e.g. "Red, Blue, Yellow"


    Darren Rose

    Hi

    Very confusing. you 'want Button2 to show all the colors'.

    Isn't that what I gave you?

    You could be more specific.

    Here is a variation that matches exactly what you posted as your desired result. In this case in a Label since you have not specified what you want to use.

    There is nothing to be gained by you snubbing an offering when you do not explain what you want sufficiently.

    Label1.Text = Nothing For Each c As String In System.Enum.GetNames(GetType(Colors)) Label1.Text &= c & " " Next

    ' and to include the commas
    Label1.Text = Trim(Label1.Text).Substring(0, Label1.Text.Length - 1)

    or, in a MessageBox

    Dim s As String = Nothing
    For Each c As String In System.Enum.GetNames(GetType(Colors))
    	s &= c & ", "
    Next
    		MessageBox.Show(s.Substring(0, s.Length - 2))
    


    Regards Les, Livingston, Scotland




    • Edited by leshay Tuesday, September 3, 2019 3:40 PM
    Tuesday, September 3, 2019 3:24 PM
  • Hi Les

    I did not snub anyone, I just explained that what your offered didn't answer my question which I believe I had clearly explained in the first place so tried to explain again.

    I am not trying to get a list of all FOUR colours in my ENUM - I already know what they are.

    In my example code (button1) you can see when I assign COLORS.RED to my variable myColors and then output to screen (it doesn't matter where I am outputting it at this stage) e.g msgbox(myColors.tostring) it shows Red, because that was value I assigned.

    Now in button2 I assign more than one value (COLORS.RED, COLORS.BLUE, COLORS.YELLOW) from my Enum to myColors, so when I output this using msgbox(myColors.tostring) I would expect it to show "Red, Blue, Yellow", but it doesn't it shows the numeric value 13 instead.

    Clearly that is different as button1 shows the string value of the result e.g. Red rather than 1, but button 2 shows 13 rather than Red, Blue, Yellow

    Sorry again if you thought I had snubbed you, but having just read my initial post and my reply to you again I can't see how anyone could think that


    Darren Rose


    • Edited by wingers Tuesday, September 3, 2019 3:34 PM
    Tuesday, September 3, 2019 3:32 PM
  • Hi

    Being told 'I already know what they are as listed in the code' was diminishing my efforts to help.

    Anyway, here is another try:

    Dim myColors As String = Colors.Red.ToString & ", " & Colors.Blue.ToString & ", " & Colors.Yellow.ToString
    MessageBox.Show(myColors)
    


    Regards Les, Livingston, Scotland

    Tuesday, September 3, 2019 3:53 PM
  • Well I did already know what they were, hence why I said it - it was a fact not a slur on your help which I always appreciate

    That does show output I want but takes away from whole point of using Enum as values I am showing above are only a small subset of what is really going in the Enum

    Anyway not to worry I have found a solution which I will cover below for others if they have same question:-

    Public Class Form1
    
        Enum Colors
            Red = 1
            Green = 2
            Blue = 4
            Yellow = 8
        End Enum
    
        Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
            Dim myColors1 As Colors = Colors.Red
           Dim myColors2 As Colors = Colors.Red Or Colors.Blue Or Colors.Yellow
            MsgBox("myColors1 = " & myColors1.ToString & vbCrLf & "myColors2 = " & mycolors2.tostring)
        End Sub
    
    End Class


    Produces this output

    Adding <Flags> above my Enum e.g.

    Public Class Form1
    
        <Flags>
        Enum Colors
            Red = 1
            Green = 2
            Blue = 4
            Yellow = 8
        End Enum
    
        Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
            Dim myColors1 As Colors = Colors.Red
           Dim myColors2 As Colors = Colors.Red Or Colors.Blue Or Colors.Yellow
            MsgBox("myColors1 = " & myColors1.ToString & vbCrLf & "myColors2 = " & mycolors2.tostring)
        End Sub
    
    End Class

    Gives my desired output of 


    Darren Rose



    • Proposed as answer by leshay Tuesday, September 3, 2019 4:27 PM
    • Edited by wingers Tuesday, September 3, 2019 4:27 PM
    • Marked as answer by wingers Tuesday, September 3, 2019 4:27 PM
    Tuesday, September 3, 2019 4:03 PM