Android Question Class variables

mscientist33

Active Member
Licensed User
I have a class called CMDS. Inside that class I have a lot of variables defined ex:

B4X:
Dim  UP  As Byte = 5
Dim  DOWN  As Byte = 6

In my main program I receive network data. One of the byte returned descibes the CMD 5 or 6. It sends the byte 5 or 6. How can I then take that 5 and know it is CMDS.UP ?

I need to be able to take the string "UP" and know that it is 5 as a byte if that makes it any clearer....
 
Last edited:

William Lancee

Well-Known Member
Licensed User
Longtime User
Define your UP variable in Class_Globals as Public UP As Byte.
Then when you receive data you can compare it to CMDS.UP

B.T.W. your thread title is not informative. If my answer is not useful consider changing the title.
 
Upvote 0

Heuristx

Active Member
Licensed User
Longtime User
There are no class variables. They are instance variables/constants. If you don't instantiate the class, you cannot use these.
To have something like class globals, you need to use a code module, not a class module.
 
Upvote 0

mscientist33

Active Member
Licensed User
So, let me screw this all up. My data comes across as a string that says "UP". I take the string or at least I try to like this:

B4X:
dim currentRowData() as string
dim currentCmd as CMD=currentRowData(2) 'UP'

says "types do not match"

I could do a sub that takes the "UP" and does some kind of compare but my CMDS have about 80 cmds in it...

Yes, they are constant class variables. I have the class initialized at the beginning.

In VB, I do it like this:

B4X:
CType(System.Enum.Parse(GetType(CMDS)))
 
Upvote 0

emexes

Expert
Licensed User
Perhaps a Map might be the way to go.

B4X:
Dim AllCommands() As String = Array As String( _
    "", "", _
    "UP", "DOWN", "LEFT", "RIGHT", _
    "LOOK", "LISTEN", "SPEAK" _
)

Dim CommandValues As Map
CommandValues.Initialize

For I = 0 To AllCommands.Length - 1
    If AllCommands(I).Length > 0 Then
        CommandValues.Put(AllCommands(I), I)
        CommandValues.Put(I, AllCommands(I))    'translate in other direction too
    End If
Next

For Each TestString As String In Array As String("UP", "DOWN", "DO", "NOT", "SPEAK")
    Log(TestString & " = " & CommandValues.Get(TestString))
Next

For Each TestNumber As Int In Array As Int(3, 1, 4, 1, 5, 9)
    Log(TestNumber & " = " & CommandValues.Get(TestNumber))
Next

Log Output:
Waiting for debugger to connect...
Program started.
UP = 2
DOWN = 3
DO = null
NOT = null
SPEAK = 8
3 = DOWN
1 = null
4 = LEFT
1 = null
5 = RIGHT
9 = null
 
Upvote 0

LucaMs

Expert
Licensed User
Longtime User
For your example, those should be constants, rather than variables.
In some languages you can group them into a whole (ENUM in VB Net); with B4X the way that seems most comfortable to me is to create a code module:
enmKeys - Code module:
Public Const UP As Byte = 5
Public Const DOWN As Byte = 6
From anywhere:
Select Data
    Case enmKeys.UP
        '...
    Case enmKeys.DOWN
        '...
    '...
End Select

Of course, the code module name can be anything, not necessarily "enmKeys"; the prefix "enm" seems useful to me.
 
Upvote 0
Cookies are required to use this site. You must accept them to continue using the site. Learn more…