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

For loops are actualy pretty easy to use.

Example:

For Each object In Collection

Next

For Each Control in Me.Controls
'Loops through every control on the form.'
Next



For i = 0 to Me.Controls.Count

Next

'Would be the same as coding'
for(i=0;i=Me.Controls.Count;i++)
{

}

Hope this helps.

Using your advice I changed it to this, and I can see it going throug and increasing the index now, but I can't get it to stop at the end of the array, it counts out to the 20th index and then throws an array out of bounds exception, I've tried with .count and .length as well as putting me. in front of the indexCount variable (but vb gives me a squiggle and tells me not to do that)

Any advice on getting it to stop at the end of the index, if I can get that to happen this would be working and solved.

Public Function DupeCheck(ByRef theNumber)

        Dim indexCount As Integer


        If questHistoryArray.Length > 0 Then

            For indexCount = 0 To questHistoryArray.Length
                If theNumber = questHistoryArray(indexCount) Then
                    Return True

                End If

            Next
        Else
            Return False
        End If
        Return True

    End Function

You will want to subtract one from QuestHistroyArray length

You will always want to subtract 1 from an array.
The last item will be a terminator character, so you will want the n - 1 index.

Thank you so much, - 1 from the array worked, I can't believe I missed that.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.