Find the Value of an Enumeration


Author
Message
Terry Bottorff
Terry Bottorff
StrataFrame VIP (1K reputation)StrataFrame VIP (1K reputation)StrataFrame VIP (1K reputation)StrataFrame VIP (1K reputation)StrataFrame VIP (1K reputation)StrataFrame VIP (1K reputation)StrataFrame VIP (1K reputation)StrataFrame VIP (1K reputation)StrataFrame VIP (1K reputation)
Group: Forum Members
Posts: 448, Visits: 12K
I have a very simple enumeration

public enum paytype

cash = 1

check = 2

other = 3

end enum

I fill a combo box on my win form with the enum but Not Bound to a BO.

It shows cash and so forth the way I want but is there a way to get the value of the one selected?

Say person selects check, how do I get 2?

Probably easy but not clicking today....

TIA.
Michel Levy
Michel Levy
Advanced StrataFrame User (575 reputation)Advanced StrataFrame User (575 reputation)Advanced StrataFrame User (575 reputation)Advanced StrataFrame User (575 reputation)Advanced StrataFrame User (575 reputation)Advanced StrataFrame User (575 reputation)Advanced StrataFrame User (575 reputation)Advanced StrataFrame User (575 reputation)Advanced StrataFrame User (575 reputation)
Group: StrataFrame Users
Posts: 193, Visits: 9K
Hi Terry,

look at the EnumDisplayValueAttribute class, in the Microfour.Strataframe.Tools namespace
Terry Bottorff
Terry Bottorff
StrataFrame VIP (1K reputation)StrataFrame VIP (1K reputation)StrataFrame VIP (1K reputation)StrataFrame VIP (1K reputation)StrataFrame VIP (1K reputation)StrataFrame VIP (1K reputation)StrataFrame VIP (1K reputation)StrataFrame VIP (1K reputation)StrataFrame VIP (1K reputation)
Group: Forum Members
Posts: 448, Visits: 12K
Michel thank you for the help.
This is the code I tried:
========


        Dim bEnum As Enumerations.PayTypes = Enumerations.PayTypes.Cash
        MessageBox.Show(MicroFour.StrataFrame.Tools.Common.GetEnumDisplayValue(bEnum))
        MessageBox.Show(MicroFour.StrataFrame.Tools.Common.GetEnumAlternateDisplayValue(bEnum))
===========

And each messagebox showed Cash which I guess makes sense so I am not doing something correct but I am not sure what. What am I overlooking?

TIA.
Edhy Rijo
E
StrataFrame VIP (6K reputation)StrataFrame VIP (6K reputation)StrataFrame VIP (6K reputation)StrataFrame VIP (6K reputation)StrataFrame VIP (6K reputation)StrataFrame VIP (6K reputation)StrataFrame VIP (6K reputation)StrataFrame VIP (6K reputation)StrataFrame VIP (6K reputation)
Group: StrataFrame Users
Posts: 2.4K, Visits: 23K
Hi Terry,

Enumerations are basically Integer type, so all you need to do is to cast the enumeration to Integer, see this pseudo code:


  Private Sub Button1_Click(sender As System.Object, e As System.EventArgsHandles Button1.Click
        Dim bEnum As PayTypes = PayTypes.Cash
         MessageBox.Show("My Enum value = " + CInt(bEnum).ToString)
     End Sub
     Public Enum PayTypes As Integer
         Cash = 1
         Check = 2
         Other = 3
     End Enum


As for the MicroFour.StrataFrame.Tools.Comon.GetEnumDisplayValue(bEnum), it is a very nice method that will display a custom
display value used in the EnumDisplayValue attribute when creating the enum like this:

    Public Enum PayTypes As Integer
        <EnumDisplayValue("Cash Payment")>
        Cash = 1
         <EnumDisplayValue("Check Payment")>
        Check = 2
        <EnumDisplayValue("Other Type of Payment")>
         Other = 3
     End Enum


The EnumDisplayValue attribute will require you to Import the MicroFour.StrataFrame.Tools class and also give control
of being able to display values with spaces in your enumerations, very nice and useful feature.



Edhy Rijo

Edited 12 Years Ago by Edhy Rijo
Terry Bottorff
Terry Bottorff
StrataFrame VIP (1K reputation)StrataFrame VIP (1K reputation)StrataFrame VIP (1K reputation)StrataFrame VIP (1K reputation)StrataFrame VIP (1K reputation)StrataFrame VIP (1K reputation)StrataFrame VIP (1K reputation)StrataFrame VIP (1K reputation)StrataFrame VIP (1K reputation)
Group: Forum Members
Posts: 448, Visits: 12K
Edhy your code worked great. Thank you so much.

But on my form if a person selects Check from the ComboBox how does one figure what value the selected enumeration has? I'm sure there must be a way but I can not seem to get the correct syntax. This will give me the Text but I can not then change it to its corresponding value.

Me.cbxpaytypes.SelectedValue.ToString
TIA.
Edhy Rijo
E
StrataFrame VIP (6K reputation)StrataFrame VIP (6K reputation)StrataFrame VIP (6K reputation)StrataFrame VIP (6K reputation)StrataFrame VIP (6K reputation)StrataFrame VIP (6K reputation)StrataFrame VIP (6K reputation)StrataFrame VIP (6K reputation)StrataFrame VIP (6K reputation)
Group: StrataFrame Users
Posts: 2.4K, Visits: 23K
You are welcome Terry.

But on my form if a person selects Check from the ComboBox how does one figure what value the selected enumeration has?
  • If the combobox is binded to a field via the BindingField and BusinessObject properties, then whenever an item is selected from the combobox, this bo.Field will contain the value of the enumeration, in your case will be the number 2.
  • If the  combobox is not binded, then simply cast its SelectedValue to the enum type and do your validation.  Something like this:

        Dim bEnum As PayTypes = CType(Me.cbxpaytypes.SelectedValue, PayTypes)
         Select Case bEnum
             Case PayTypes.Cash
                 MessageBox.Show("You selected cash")
             Case PayTypes.Check
                 MessageBox.Show(String.Format("You selected {0}", MicroFour.StrataFrame.Tools.Common.GetEnumDisplayValue(bEnum)))
             Case PayTypes.Other
                 MessageBox.Show("You selected Other")
         End Select



Edhy Rijo

Edited 12 Years Ago by Edhy Rijo
Terry Bottorff
Terry Bottorff
StrataFrame VIP (1K reputation)StrataFrame VIP (1K reputation)StrataFrame VIP (1K reputation)StrataFrame VIP (1K reputation)StrataFrame VIP (1K reputation)StrataFrame VIP (1K reputation)StrataFrame VIP (1K reputation)StrataFrame VIP (1K reputation)StrataFrame VIP (1K reputation)
Group: Forum Members
Posts: 448, Visits: 12K
Thanks Edhy that will work.

I tried using Ctype but since I don't exactly have all the experience I need I just could not get the syntax correct. Once I saw it, it made sense but I could not produce it. I think my problem was I did not know what type I was trying to convert to what type.

Again thank you so much for such great help.
GO

Merge Selected

Merge into selected topic...



Merge into merge target...



Merge into a specific topic ID...




Similar Topics

Reading This Topic

Login

Explore
Messages
Mentions
Search