case select

skaryChinezeGuie

Well-known member
Joined
Apr 23, 2006
Messages
94
Programming Experience
Beginner
can anyone explain how case select works?

Public Sub Luck()
Dim randomLuck As New Random
Dim goodLuck As Integer
Dim badLuck As Integer
Dim luck As Integer
luck = randomLuck.Next(1, 2)

End Sub

What i would like to do is

if randomLuck = 1 then select case good luck
else
select case badLuck
end if
:D
 
the best way would be if you enum the all combinations before you go for Select case statement. btw, implementing select case statement is not hard at all ... just more readable than an If statement. In your case:
VB.NET:
[SIZE=2][COLOR=#0000ff]Private [/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]Enum[/COLOR][/SIZE][SIZE=2] stateOfLuck [/SIZE][SIZE=2][COLOR=#0000ff]As [/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]Integer
[/COLOR][/SIZE][SIZE=2]Luck = 1
GoodLuck = 2
BadLuck = 3
Misery = 4
[/SIZE][SIZE=2][COLOR=#0000ff]End [/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]Enum[/COLOR][/SIZE]
then declare the random and the integer that will accept the random generated number


VB.NET:
[SIZE=2][COLOR=#0000ff]Dim[/COLOR][/SIZE][SIZE=2] randomLuck [/SIZE][SIZE=2][COLOR=#0000ff]As [/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]New[/COLOR][/SIZE][SIZE=2] Random
[/SIZE][SIZE=2][COLOR=#0000ff]Dim[/COLOR][/SIZE][SIZE=2] myLuckInt%[/SIZE]

then join this Select case statement to an event (button click in this case)


VB.NET:
[SIZE=2][COLOR=#0000ff]Private [/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]Sub[/COLOR][/SIZE][SIZE=2] Button1_Click([/SIZE][SIZE=2][COLOR=#0000ff]ByVal[/COLOR][/SIZE][SIZE=2] sender [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2] System.Object, [/SIZE][SIZE=2][COLOR=#0000ff]ByVal[/COLOR][/SIZE][SIZE=2] e [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2] System.EventArgs) [/SIZE][SIZE=2][COLOR=#0000ff]Handles[/COLOR][/SIZE][SIZE=2] Button1.Click
myLuckInt = randomLuck.Next(1, 5)
[/SIZE][SIZE=2][COLOR=#0000ff]Select [/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]Case[/COLOR][/SIZE][SIZE=2] [Enum].GetName([/SIZE][SIZE=2][COLOR=#0000ff]GetType[/COLOR][/SIZE][SIZE=2](stateOfLuck), myLuckInt)
[/SIZE][SIZE=2][COLOR=#0000ff] Case[/COLOR][/SIZE][SIZE=2] "Luck"
     MessageBox.Show("today you are only lucky")
[/SIZE][SIZE=2][COLOR=#0000ff] Case[/COLOR][/SIZE][SIZE=2] "GoodLuck"
     MessageBox.Show("today you have good luck")
[/SIZE][SIZE=2][COLOR=#0000ff] Case[/COLOR][/SIZE][SIZE=2] "BadLuck"
     MessageBox.Show("today you have bad luck")
[/SIZE][SIZE=2][COLOR=#0000ff] Case[/COLOR][/SIZE][SIZE=2] "Misery"
     MessageBox.Show("today your luck is miserable")
[/SIZE][SIZE=2][COLOR=#0000ff]End [/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]Select
[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]End [/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]Sub
[/COLOR][/SIZE]
 
Good idea to use Enum, very good coding practise! But you have misunderstood something when you type out the enum members as strings in the select casing. The variable holding the enum value should be strongly typed, instead of Integer. Below is the same code, only actually using the enum as it should be used in code. Note that when you select case an enum, when writing "Case " the editor will show the values (enum member) in box for you to select. Less typing and more readable and reliable code.
VB.NET:
[COLOR=blue]Private Enum[/COLOR] stateOfLuck [COLOR=blue]As Integer[/COLOR]
  Luck = 1
  GoodLuck = 2
  BadLuck = 3
  Misery = 4
[COLOR=blue]End Enum[/COLOR]
 
[COLOR=blue]Dim[/COLOR] randomLuck [COLOR=blue]As New[/COLOR] Random
[COLOR=blue]Dim[/COLOR] myLuck [COLOR=blue]As[/COLOR] stateOfLuck
 
[COLOR=blue]Private Sub[/COLOR] Button1_Click([COLOR=blue]ByVal[/COLOR] sender [COLOR=blue]As[/COLOR] System.Object, [COLOR=blue]ByVal[/COLOR] e [COLOR=blue]As[/COLOR] System.EventArgs) _
[COLOR=blue]Handles[/COLOR] Button1.Click
  myLuck = [COLOR=blue]DirectCast[/COLOR](randomLuck.Next(1, 5), stateOfLuck)
  [COLOR=blue]Select Case[/COLOR] myLuck
  [COLOR=blue]Case[/COLOR] stateOfLuck.Luck
    MessageBox.Show("today you are only lucky")
  [COLOR=blue]Case[/COLOR] stateOfLuck.GoodLuck
    MessageBox.Show("today you have good luck")
  [COLOR=blue]Case[/COLOR] stateOfLuck.BadLuck
    MessageBox.Show("today you have bad luck")
  [COLOR=blue]Case[/COLOR] stateOfLuck.Misery
    MessageBox.Show("today your luck is miserable")
  [COLOR=blue]End Select[/COLOR]
[COLOR=blue]End Sub[/COLOR]
 
cool. 2 things. what's the DirectCast? and what if i want each one of the luck states to have lets say 3 randomly possible events, but i only want one of the events to be chosen? For instance if you had goodLuck you could get GoodLuck1, GoodLuck2, or GoodLuck3. by the way thanks.
 
i tried it and after a little editing this is my only error
myLuck = (randomLuck.Next(1, 5), stateOfLuck) it says ')' expected


main form code

PrivateSub BtnMap_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BtnMap.Click
Dim nFrmMap As Form = New Map
nFrmMap.ShowDialog()
GetNewValues()
LblAcidPrice.Text = acidPrice.ToString
LblCocainePrice.Text = cocainePrice.ToString
LblEcstasyPrice.Text = ecstasyPrice.ToString
LblPCPprice.Text = PCPprice.ToString
LblHeroinPrice.Text = HeroinPrice.ToString
LblWeedPrice.Text = weedPrice.ToString
LblShroomsPrice.Text = shroomsPrice.ToString
LblSpeedPrice.Text = speedPrice.ToString
LblHashPrice.Text = hashPrice.ToString
LblPlace.Text = GameLocation
myLuck = (randomLuck.Next(1, 5), stateOfLuck)
SelectCase myLuck
Case stateOfLuck.GoodLuck
MessageBox.Show("today you have good luck")
Case stateOfLuck.BadLuck
MessageBox.Show("today you have bad luck")
Case stateOfLuck.Cops
MessageBox.Show("today your luck is miserable")
EndSelect
EndSub

publicVariables code

PublicEnum stateOfLuck AsInteger
GoodLuck = 2
BadLuck = 3
Cops = 4
EndEnum

Public randomLuck AsNew Random
Public myLuck As stateOfLuck
 
Last edited:
Well, i am also not sure what John is doing here althrough i am interested to learn something new if there is ...

About the using string and getname method and the way he showed above:

Case stateOfLuck.Luck
or
Case "Luck"

I would say it is just a matter of taste :D
Ooo yes it is ..indeed (i am using both but in this case i just did use the one which i like more)

Regards ;)

SkaryChinese, did you try to run the project i left above?
Check it out ... it will do just what you expect to do :)
 
Oh how i hate when someone do not follow what i am doing for him/her ignoring my posts ... it is driving me crazy. Yes certainly it does (is saturday and you have express answer(s) ... i guess it deserve at least one thanks. Just for illustration open this and tell me how much people you can see among active) .
So, skaryChinezeGuie if you want any further help of me then read what i am telling you. Of course read the other posts but care to check all posts ...isn't it sort of normal.
Btw, if you want to follow the changed/adapted or if you want improved version of my code then use this

myLuck = CType(randomLuck.Next(1, 5), stateOfLuck)

 
yeah i looked at it and put part of it in my app but it didn't do anything. I put a breakpoint at the button click event and stepped through it, but it went through all the cases and no messageboxes were displayed.

PrivateSub BtnMap_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BtnMap.Click
Dim nFrmMap As Form = New Map
nFrmMap.ShowDialog()
GetNewValues()
LblAcidPrice.Text = acidPrice.ToString
LblCocainePrice.Text = cocainePrice.ToString
LblEcstasyPrice.Text = ecstasyPrice.ToString
LblPCPprice.Text = PCPprice.ToString
LblHeroinPrice.Text = HeroinPrice.ToString
LblWeedPrice.Text = weedPrice.ToString
LblShroomsPrice.Text = shroomsPrice.ToString
LblSpeedPrice.Text = speedPrice.ToString
LblHashPrice.Text = hashPrice.ToString
LblPlace.Text = GameLocation
myLuck = randomLuck.Next(1, 5)
SelectCase [Enum].GetName(GetType(stateOfLuck), myLuck)
Case stateOfLuck.GoodLuck
MessageBox.Show("today you have good luck")
Case stateOfLuck.BadLuck
MessageBox.Show("today you have bad luck")
Case stateOfLuck.Cops
MessageBox.Show("Run bitch! It's the Fuzz!")
EndSelect
EndSub

Public Enum stateOfLuck As Integer
GoodLuck = 2
BadLuck = 3
Cops = 4
End Enum
Public randomLuck As New Random
Public myLuck As stateOfLuck
:D
 
skaryChinezeGuie said:
yeah i looked at it and put part of it in my app but it didn't do anything. I put a breakpoint at the button click event and stepped through it, but it went through all the cases and no messageboxes were displayed.

PrivateSub BtnMap_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BtnMap.Click
Dim nFrmMap As Form = New Map
nFrmMap.ShowDialog()
GetNewValues()
LblAcidPrice.Text = acidPrice.ToString
LblCocainePrice.Text = cocainePrice.ToString
LblEcstasyPrice.Text = ecstasyPrice.ToString
LblPCPprice.Text = PCPprice.ToString
LblHeroinPrice.Text = HeroinPrice.ToString
LblWeedPrice.Text = weedPrice.ToString
LblShroomsPrice.Text = shroomsPrice.ToString
LblSpeedPrice.Text = speedPrice.ToString
LblHashPrice.Text = hashPrice.ToString
LblPlace.Text = GameLocation
myLuck = randomLuck.Next(1, 5)
SelectCase [Enum].GetName(GetType(stateOfLuck), myLuck)
Case stateOfLuck.GoodLuck
MessageBox.Show("today you have good luck")
Case stateOfLuck.BadLuck
MessageBox.Show("today you have bad luck")
Case stateOfLuck.Cops
MessageBox.Show("Run bitch! It's the Fuzz!")
EndSelect
EndSub

PublicEnum stateOfLuck AsInteger
GoodLuck = 2
BadLuck = 3
Cops = 4
EndEnum
Public randomLuck AsNew Random
Public myLuck As stateOfLuck
:D

It seems like you don't get the concept of select case statement

1st of notice that your enumeration starts with value 2
means random suppose to ranges from 2 to 5 producing three values (from 2 to 5).
2nd off, if you want to use [Enum].getname way you suppose to use string after Case keyword.

OMG i am so tired ... i am going to get some aspirin.
Later :(
 
you're right i don't get it. that's why i asked if anyone could explain it. I got this to work using if statements but that work was lost when i reformatted.

Public Enum stateOfLuck As Integer
GoodLuck = 1
BadLuck = 2
Cops = 3
End Enum
Public randomLuck As New Random
Public myLuck As stateOfLuck


Private Sub BtnMap_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BtnMap.Click
Dim nFrmMap As Form = New Map
nFrmMap.ShowDialog()
GetNewValues()
LblAcidPrice.Text = acidPrice.ToString
LblCocainePrice.Text = cocainePrice.ToString
LblEcstasyPrice.Text = ecstasyPrice.ToString
LblPCPprice.Text = PCPprice.ToString
LblHeroinPrice.Text = HeroinPrice.ToString
LblWeedPrice.Text = weedPrice.ToString
LblShroomsPrice.Text = shroomsPrice.ToString
LblSpeedPrice.Text = speedPrice.ToString
LblHashPrice.Text = hashPrice.ToString
LblPlace.Text = GameLocation
myLuck = randomLuck.Next(1, 3)
Select Case [Enum].GetName(GetType(stateOfLuck), myLuck)
Case stateOfLuck.GoodLuck
MessageBox.Show("today you have good luck")
Case stateOfLuck.BadLuck
MessageBox.Show("today you have bad luck")
Case stateOfLuck.Cops
MessageBox.Show("Run bitch! It's the Fuzz!")
End Select
End Sub

when i step through it it looks like the value of myLuck is 0 then it changes to badLuck. it steps over the cases and then the main form comes back up. and i couldn't understand your "2nd off" statement.
 
right, i was gonna do that, but since i knew case select was another way to do it i thought i would go ahead and learn it. If you don't wanna get into it i understand. thanks for the help so far.
 
Back
Top