Solved

Using Illegal Characters in Enumerations

Posted on 2004-04-07
14
232 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 49

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
 

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 49

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
IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 
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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Suggested Solutions

I’ve seen a number of people looking for examples of how to access web services from VB6.  I’ve been using a test harness I built in VB6 (using many resources I found online) that I use for small projects to work out how to communicate with web serv…
Enums (shorthand for ‘enumerations’) are not often used by programmers but they can be quite valuable when they are.  What are they? An Enum is just a type of variable like a string or an Integer, but in this case one that you create that contains…
Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…

758 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

Need Help in Real-Time?

Connect with top rated Experts

22 Experts available now in Live!

Get 1:1 Help Now