Link to home
Start Free TrialLog in
Avatar of togilvie
togilvie

asked on

Using Illegal Characters in Enumerations

I'm trying to create an enumeration containing the rank for a Playing card (Ace-King). Here's what I'm trying:

Public Enum Rank
            A
            2
            3
            4
            5
            6
            7
            8
            9
            T
            J
            Q
            K
        End Enum

I am getting errors when trying to use the integer values (2, 3, 4, etc). Is there a way to define an enumeration while keeping these "illegal" characters? I don't need to manipulate the value as an Integer - just intend to use it as a string. I tried defining As Byte, but that didn't work. Should I be using a different collections type?
ASKER CERTIFIED SOLUTION
Avatar of Ryan Chong
Ryan Chong
Flag of Singapore image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Hi togilvie,

You have to start with an alpha character for enums. As for using a collection or even a dictionary object, these are possible. It all depends on how you are going to use the elements/values.

Tim Cottee
Brainbench MVP for Visual Basic
http://www.brainbench.com
Avatar of mmusante
mmusante

Enums must use a valid identifiers, they cannot start with a number to avoid anbiguity in expressions with numbers change your enum to someting like this ...

Public Enum Rank
            rk_A
            rk_2
            rk_3
            rk_4
            rk_5
            rk_6
            rk_7
            rk_8
            rk_9
            rk_T
            rk_J
            rk_Q
            rk_K
 End Enum

Enums are always (long)integers if you do not assign the value they are automatically assigned starting by 0 so

Public Enum Rank
            rk_A
            rk_2
              ...

is the same of

Public Enum Rank
            rk_A = 0
            rk_2 = 1
                 ...
Avatar of togilvie

ASKER

I need to use it in two ways:
1) Use the Rank and Suit to determine the index # of a card from 1=52. So, for example if Ace =1 and Clubs = 1, the Ace of Clubs has value 1. I use the following formula:

Dim CardIndex As Integer = CType(Me.FCardRank, Integer) * 4 + FCardSuit

2) Print a combined card value as a two character combo. Two of clubs is 2c, Jack of diamonds is Jd. This is used for input to another program, so it's important that I maintain that format. I'm creating a card value using the following:

Return (FCardRank & FCardSuit)

It doesn't seem like the Value prefix will work for me, because the second app won't work, right?
Something like:

Public Enum Rank
            valueA As Integer = 1
            value2 As Integer = 2
            value3 As Integer = 3
            value4 As Integer = 4
            value5 As Integer = 5
            value6 As Integer = 6
            value7 As Integer = 7
            value8 As Integer = 8
            value9 As Integer = 9
            valueT As Integer = 10
            valueJ As Integer = 11
            valueQ As Integer = 12
            valueK As Integer = 13
        End Enum

? But not very sure if the enum declaration is same at .net
togilvie,

What is FCardRank?

Tim.
It's an instance of the Rank Enumeration.

Private FCardRank As Rank
togilvie,

Ahh, I was hoping that perhaps this was driven by a combobox or something similar where you could have the text and the value as different things.

Perhaps you could give us a bit more of an idea of your existing code/application and what it is trying to do so that we can think around the issue a little.

Tim.
Not sure what you want to use it for, but you could use a string of the values.  Then the offset within the string determines the ranking.

Dim Cards As String
Cards = "A23456789TJQK"

Dim Player1Card As String
Dim Player2Card As String

If InStr(1, Cards, Player1Card) > InStr(1, Cards, Player2Card) Then
   ' Player 1 has the bigger card
Else If InStr(1, Cards, Player1Card) = InStr(1, Cards, Player2Card) Then
   ' Cards are equal.
Else
   ' Player 2 has the bigger card.
End If
togilvie: Just enclose your "number" members in [] pair like this:

Public enum MyEnum
         A
         [2]
         [3]
         [4]
         [5]
End Enum


:)
Try this:

Public Enum Rank
            rk_A
            rk_2
            rk_3
            rk_4
            rk_5
            rk_6
            rk_7
            rk_8
            rk_9
            rk_T
            rk_J
            rk_Q
            rk_K
 End Enum

dim aCRanks as Variant
...
private sub Form_Load
...
aCRanks = array("A","2","3","4","5","6","7","8","9","T","J","Q","K")
...
end sub

so you will able to get the rank string in this way:
strRank = aCRanks(rk_J)