Find the Value of an Enumeration

Author Message
 Posted 3/8/2012 9:21:21 AM
StrataFrame User

StrataFrame UserStrataFrame UserStrataFrame UserStrataFrame UserStrataFrame UserStrataFrame UserStrataFrame UserStrataFrame UserStrataFrame User

Group: Forum Members
Last Login: 2/2/2015 1:05:23 AM
Posts: 448, Visits: 12,502
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.
Post #31014
Add to Twitter Add to Facebook
 Posted 3/8/2012 9:43:08 AM
StrataFrame User

StrataFrame UserStrataFrame UserStrataFrame UserStrataFrame UserStrataFrame UserStrataFrame UserStrataFrame UserStrataFrame UserStrataFrame User

Group: StrataFrame Users
Last Login: 11/7/2018 2:37:04 AM
Posts: 193, Visits: 9,066
Hi Terry,

look at the EnumDisplayValueAttribute class, in the Microfour.Strataframe.Tools namespace
Post #31015
Add to Twitter Add to Facebook
 Posted 3/8/2012 12:41:29 PM
StrataFrame User

StrataFrame UserStrataFrame UserStrataFrame UserStrataFrame UserStrataFrame UserStrataFrame UserStrataFrame UserStrataFrame UserStrataFrame User

Group: Forum Members
Last Login: 2/2/2015 1:05:23 AM
Posts: 448, Visits: 12,502
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.
Post #31016
Add to Twitter Add to Facebook
 Posted 3/8/2012 2:02:50 PM
StrataFrame VIP

StrataFrame VIPStrataFrame VIPStrataFrame VIPStrataFrame VIPStrataFrame VIPStrataFrame VIPStrataFrame VIPStrataFrame VIPStrataFrame VIP

Group: StrataFrame Users
Last Login: 4/9/2021 12:42:36 PM
Posts: 2,436, Visits: 24,184
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
(Using VB.Net 2019/SQL Server 2017)


Edited: 3/8/2012 2:12:24 PM by Edhy Rijo
Post #31018
Add to Twitter Add to Facebook
 Posted 3/9/2012 12:44:14 AM
StrataFrame User

StrataFrame UserStrataFrame UserStrataFrame UserStrataFrame UserStrataFrame UserStrataFrame UserStrataFrame UserStrataFrame UserStrataFrame User

Group: Forum Members
Last Login: 2/2/2015 1:05:23 AM
Posts: 448, Visits: 12,502
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.
Post #31019
Add to Twitter Add to Facebook
 Posted 3/9/2012 1:53:22 AM
StrataFrame VIP

StrataFrame VIPStrataFrame VIPStrataFrame VIPStrataFrame VIPStrataFrame VIPStrataFrame VIPStrataFrame VIPStrataFrame VIPStrataFrame VIP

Group: StrataFrame Users
Last Login: 4/9/2021 12:42:36 PM
Posts: 2,436, Visits: 24,184
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
(Using VB.Net 2019/SQL Server 2017)


Edited: 3/9/2012 1:53:58 AM by Edhy Rijo
Post #31020
Add to Twitter Add to Facebook
 Posted 3/9/2012 2:48:01 AM
StrataFrame User

StrataFrame UserStrataFrame UserStrataFrame UserStrataFrame UserStrataFrame UserStrataFrame UserStrataFrame UserStrataFrame UserStrataFrame User

Group: Forum Members
Last Login: 2/2/2015 1:05:23 AM
Posts: 448, Visits: 12,502
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.
Post #31021
Add to Twitter Add to Facebook


Similar Topics

Expand / Collapse

Reading This Topic

Expand / Collapse

Site Map - Home - My Account - Forum - About Us - Contact Us - Try It - Buy It

Microsoft, Visual Studio, and the Visual Studio logo are trademarks or registered trademarks of Microsoft Corporation in the United States and/or other countries.