?
Solved

Using Illegal Characters in Enumerations

Posted on 2004-04-07
14
Medium Priority
?
250 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 2
  • 2
  • +3
14 Comments
 
LVL 53

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
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 

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 53

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

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

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…
Introduction While answering a recent question about filtering a custom class collection, I realized that this could be accomplished with very little code by using the ScriptControl (SC) library.  This article will introduce you to the SC library a…
As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
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
Course of the Month9 days, 4 hours left to enroll

764 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