Solved

Using Illegal Characters in Enumerations

Posted on 2004-04-07
14
244 Views
Last Modified: 2010-05-02
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?
0
Comment
Question by:togilvie
  • 3
  • 2
  • 2
  • +3
14 Comments
 
LVL 51

Accepted Solution

by:
Ryan Chong earned 125 total points
ID: 10774946
because the variable declaration cannot be start with Numeric character, so try like:

Public Enum Rank
            valueA
            value2
            value3
            value4
            value5
            value6
            value7
            value8
            value9
            valueT
            valueJ
            valueQ
            valueK
        End Enum

?
0
 
LVL 43

Expert Comment

by:TimCottee
ID: 10774948
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
0
 
LVL 6

Expert Comment

by:mmusante
ID: 10774990
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
                 ...
0
Salesforce Has Never Been Easier

Improve and reinforce salesforce training & adoption using WalkMe's digital adoption platform. Start saving on costly employee training by creating fast intuitive Walk-Thrus for Salesforce. Claim your Free Account Now

 

Author Comment

by:togilvie
ID: 10775006
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?
0
 
LVL 51

Expert Comment

by:Ryan Chong
ID: 10775062
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
0
 
LVL 43

Expert Comment

by:TimCottee
ID: 10775102
togilvie,

What is FCardRank?

Tim.
0
 

Author Comment

by:togilvie
ID: 10775136
It's an instance of the Rank Enumeration.

Private FCardRank As Rank
0
 
LVL 43

Expert Comment

by:TimCottee
ID: 10775154
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.
0
 
LVL 15

Expert Comment

by:SRigney
ID: 10775188
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
0
 
LVL 6

Expert Comment

by:___XXX_X_XXX___
ID: 10775198
togilvie: Just enclose your "number" members in [] pair like this:

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


:)
0
 
LVL 6

Expert Comment

by:mmusante
ID: 10775313
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)
0

Featured Post

PeopleSoft Has Never Been Easier

PeopleSoft Adoption Made Smooth & Simple!

On-The-Job Training Is made Intuitive & Easy With WalkMe's On-Screen Guidance Tool.  Claim Your Free WalkMe Account Now

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

The debugging module of the VB 6 IDE can be accessed by way of the Debug menu item. That menu item can normally be found in the IDE's main menu line as shown in this picture.   There is also a companion Debug Toolbar that looks like the followin…
When designing a form there are several BorderStyles to choose from, all of which can be classified as either 'Fixed' or 'Sizable' and I'd guess that 'Fixed Single' or one of the other fixed types is the most popular choice. I assume it's the most p…
Get people started with the process of using Access VBA to control Outlook using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Microsoft Outlook. Using automation, an Access applic…
Get people started with the process of using Access VBA to control Excel using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Excel. Using automation, an Access application can laun…

730 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question