?
Solved

Using Illegal Characters in Enumerations

Posted on 2004-04-07
14
Medium Priority
?
256 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
11 Comments
 
LVL 54

Accepted Solution

by:
Ryan Chong earned 500 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
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 

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 54

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

[Webinar] Cloud and Mobile-First Strategy

Maybe you’ve fully adopted the cloud since the beginning. Or maybe you started with on-prem resources but are pursuing a “cloud and mobile first” strategy. Getting to that end state has its challenges. Discover how to build out a 100% cloud and mobile IT strategy in this webinar.

Question has a verified solution.

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

Introduction While answering a recent question (http://www.experts-exchange.com/Q_27402310.html) in the VB classic zone, I wrote some VB code in the (Office) VBA environment, rather than fire up my older PC.  I didn't post completely correct code o…
This article describes some techniques which will make your VBA or Visual Basic Classic code easier to understand and maintain, whether by you, your replacement, or another Experts-Exchange expert.
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…
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…
Suggested Courses

850 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