Hi,
I'm kind of new to VB and I'm trying to make myself a little game to help me get into the swing of VB (I know some java).
Basically what I'm trying to do is this, I have 20 statements in a Select Case (right now they're just labels for my own testing), I have a random number generator that will generate a number between 1 and 20. Since I don't want the same statement to repeat during the individual run of the program I'm trying to generate a random number, then check it in a dupeCheck method, then if it isn't a dupe I'm trying to return it to the select case and put it in the array so that when I choose the next random number if it's the same the dupe check method will catch it and return a False boolean and then the random method will do a recursive call to itself to generate a new random number and the whole thing starts again. I'm populating the numbers into the select case just fine, but VB keeps telling me that my functions aren't returning values on all paths, and stepping through I can see that the recursion isn't happening and that it's just sending the repeat number back, but I can't figure out where my major issue is. I'll show you my current skeleton code for the "GuessOrDie" as well as the first manifestation of the code I wrote for it a couple of days ago (the old code had infinite recursive calls and array out of bound exceptions)
I would really love it if you guys could take a look and let me know what mistakes I'm making, I'm having big problems moving from FOR loops in java to For/Each loops in VB and I suspect that is a big part of my problem. Anyway, thanks for looking and I appreciate any help :)
Current Code:
Public Class GuessOrDie
Dim questHistoryArray(19) As Integer
Dim indexCount As Integer
Dim j As Integer
Public Function Random()
'Generates Random Number
Dim RandomNumber As Integer
Randomize()
RandomNumber = Int(Rnd() * 21) 'up to 20 random numbers (21-1)
Select DupeCheck(RandomNumber)
Case True
Random()
Case False
Return RandomNumber
RandomNumber = questHistoryArray(j)
j += 1
End Select
End Function
Public Function DupeCheck(ByRef theNumber)
If questHistoryArray.Count = 0 Then
Return False
ElseIf questHistoryArray.Length > 0 Then
For Each Me.indexCount In questHistoryArray
If theNumber = questHistoryArray(indexCount) Then
Return True
End If
Next indexCount
Else
Return True
End If
End Function
Private Sub Question()
'Chooses question for display
Dim RandomNumber As Integer = Random()
Select Case RandomNumber
Case 1
Label1.Text = "1"
Case 2
Label2.Text = "2"
Case 3
Label3.Text = "3"
Case 4
Label4.Text = "4"
Case 5
Label5.Text = "5"
Case 6
Label6.Text = "6"
Case 7
Label7.Text = "7"
Case 8
Label8.Text = "8"
Case 9
Label9.Text = "9"
Case 10
Label10.Text = "10"
Case 11
Label11.Text = "11"
Case 12
Label12.Text = "12"
Case 13
Label13.Text = "13"
Case 14
Label14.Text = "14"
Case 15
Label15.Text = "15"
Case 16
Label16.Text = "16"
Case 17
Label17.Text = "17"
Case 18
Label18.Text = "18"
Case 19
Label19.Text = "19"
Case 20
Label20.Text = "20"
End Select
End Sub
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Question()
End Sub
End Class
Here is the first version of the code that I wrote:
Public Class GuessOrDie
Dim questHistoryArray(19) As Integer
Dim i As Integer = 19
Dim j As Integer = 19
Public Sub Random()
'Generates Random Number
Dim RandomNumber As Integer
Randomize()
RandomNumber = Int(Rnd() * 21) 'up to 20 random numbers (21-1)
Dim QuestionNumber As Integer = RandomNumber
If questHistoryArray.Count = 0 Then
RandomNumber = QuestionNumber
questHistoryArray(j) = RandomNumber
Question(RandomNumber)
j -= 1
Else
For indexCount As Integer = 0 To questHistoryArray.Length
If QuestionNumber = questHistoryArray(i) Then
QuestionNumber += 1
Else
RandomNumber = QuestionNumber
questHistoryArray(indexCount) = RandomNumber
j -= 1
Question(RandomNumber)
End If
Next indexCount
End If
End Sub
Private Sub Question(ByVal RandomNumber)
'Chooses question for display
Select Case randomNumber
Case 1
Label1.Text = "1"
Case 2
Label1.Text = "2"
Case 3
Label1.Text = "3"
Case 4
Label1.Text = "4"
Case 5
Label1.Text = "5"
Case 6
Label1.Text = "6"
Case 7
Label1.Text = "7"
Case 8
Label1.Text = "8"
Case 9
Label1.Text = "9"
Case 10
Label1.Text = "10"
Case 11
Label1.Text = "11"
Case 12
Label1.Text = "12"
Case 13
Label1.Text = "13"
Case 14
Label1.Text = "14"
Case 15
Label1.Text = "15"
Case 16
Label1.Text = "16"
Case 17
Label1.Text = "17"
Case 18
Label1.Text = "18"
Case 19
Label1.Text = "19"
Case 20
Label1.Text = "20"
End Select
End Sub
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Random()
End Sub
End Class