StrataFrame Forum

Find the Value of an Enumeration

http://forum.strataframe.net/Topic31014.aspx

By Terry Bottorff - 3/8/2012

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.
By Michel Levy - 3/8/2012

Hi Terry,

look at the EnumDisplayValueAttribute class, in the Microfour.Strataframe.Tools namespace
By Terry Bottorff - 3/8/2012

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.
By Edhy Rijo - 3/8/2012

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.

By Terry Bottorff - 3/9/2012

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.
By Edhy Rijo - 3/9/2012

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

By Terry Bottorff - 3/9/2012

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.