-
I am making a program, and would like to have something like this in a BUTTON:
in a module i have
Code:
Function Game(Action As String) As Boolean
Select Case Action
Case "Activate"
Case "DeActivate"
Case Else
End Select
End Function
stuff like that
*BUT
when i use this,
the code in the button gets a SPACE between
Game and (Action)
Sooo.. i've tryed many things, and it doesn't work, OR it just keeps adding a space
I want to use this method, because its just simple and if i have to change something, then i just change it in the module and not fool around with ALL The forms etc..
Thanks..
-
why do you have it as Boolean if it has no return value??
but, all you have to do to remove the space is
-
If you change it so that it has no return value, you could just use:
You might find tha whole game a lot easier to program if you use enums instead of using strings, eg:
Code:
'Module
Public Enum Actions
Activate = 1
DeActivate
'OtherActions here
End Enum
Sub Game(Action As Actions)
Select Case Action
Case Activate
'...
Case DeActivate
'...
Case Else
'...
End Select
End Sub
'Code Elsewhere
Call Game(Activate)
Enumeration is a way to make your job easier. That's really all it does. It saves each word in the Enum...End Enum block as an integer. So Activate is actually equal to 1, DeActivate is actually equal to 2. You know how when you have a Boolean and you press equals and a little listbox appears saying True and False (this is called IntelliSense) the same happens, so when you enter:
That little box will appear giving you all the possible values for Action (in this case Activate and DeActivate) so you can just click on one of them, meaning typos are nearly impossible.
(BTW Enum is short for Enumeration which means (basically) Giving each value (like Activate) a number value (1 in this case)
Also, you don't have to type in the value for each one, eg:
Code:
Public Enum MaritalStatus
Single = 1
Married = 2
Divorced = 3
Remarried = 4
Widow = 5
Widower = 6
End Enum
is that same as:
Code:
Public Enum MaritalStatus
Single = 1
Married
Divorced
Remarried
Widow
Widower
End Enum
(VB just counts up one from the previous entry)
You could specify you own value for each one if necessar, though:
Code:
Enum Ages
Baby = 3
Child = 12
Teen = 19
GrownUp = 65
Pensioner = 100
End Enum
But you must remember that 'Baby' is just another way of saying '3'. So if you think you are being smart by doing this:
Code:
Enum Ages
Baby = 3
Child = 12
Teen = 19
GrownUp = 65
Pensioner = 100
End Enum
Sub ShowAgePeriod(Age As Ages)
If Age < Baby Then
Print Baby
ElseIf Age < Child
Print Child
ElseIf Age < Teen
Print Teen
ElseIf Age < GrownUp
Print GrownUp
ElseIf Age < Pensioner
Print Pensioner
Else:
MsgBox("Please enter a valid age"), , "Error"
End If
End Sub
'....In code somewhere
Call ShowAgePeriod(27)
'This will not show 'GrownUp' is will show '65'
I hope you understand Enums, how useful thay can be, and when to use them.
Hope it helps,
Moi