Quantcast
Channel: VBForums
Viewing all articles
Browse latest Browse all 15722

[RESOLVED] If Not list.Contains(variable) not working

$
0
0
Hi,

I am making a really simple bingo caller. The object of it is that it displays a random number from 1 to 90 every time a label is clicked (just because I don't like buttons). It should never repeat the same number.

How I've done it before in WinForms is by creating a list of integer, and using a Do Until Loop to check to make sure it generates a unique number. It has always worked when doing it in WinForms, but for some reason it is still giving out duplicates in WPF.

Code:

Class MainWindow
    Dim nxtNum As Integer
    Dim rnd As New Random()
    Dim selNum As New List(Of Integer)
    Dim onList As Boolean
    Dim gameOver As Boolean
    Private Sub lblExit_MouseDown(sender As Object, e As MouseButtonEventArgs)
        Close()
    End Sub
    Private Sub lblExit_MouseEnter(sender As Object, e As MouseEventArgs)
        lblExit.Foreground = Brushes.Red
        lblExit.Background = Brushes.Black
    End Sub
    Private Sub lblExit_MouseLeave(sender As Object, e As MouseEventArgs)
        lblExit.Foreground = Brushes.Black
        lblExit.Background = Brushes.Red
    End Sub
    Private Sub lblNumCall_MouseDown(sender As Object, e As MouseEventArgs)
        onList = False
        If selNum.Count < 90 Then
            Do Until onList = True
                nxtNum = rnd.Next(1, 91)
                If Not selNum.Contains(nxtNum) Then
                    selNum.Add(nxtNum)
                    nxtNum = rnd.Next(1, 91)
                    lblNumCall.Content = nxtNum

                    Dim labelSelect As Control = CType(Grid1.FindName("lblSelect" & nxtNum), Label)

                    If labelSelect.Foreground Is Brushes.YellowGreen Then
                        labelSelect.Foreground = Brushes.Black
                    Else
                        labelSelect.Foreground = Brushes.Red
                    End If

                    txtNumbersPicked.Text = txtNumbersPicked.Text & nxtNum & ","
                End If
                onList = True
            Loop
        Else
            gameOver = True
        End If

        If gameOver = True Then Game_Over()
    End Sub

    Private Sub Game_Over()
        MessageBox.Show("That's All folks!")
        selNum.Clear()
    End Sub

    Private Sub Window_Loaded(sender As Object, e As RoutedEventArgs)
        selNum.Clear()
        onList = False
    End Sub
End Class

I have a Boolean variable that is set to false as soon as the label is clicked, then an IF statement that runs only if the list is < 90.

Inside the IF statement there is a Do Until Loop that runs until the boolean variable becomes true. It picks the number between 1 to 90, checks that it isn't already in the list. If it is it *should* pick another number, but it doesn't.

I've tried searching Google, but all the responses are either WinForms vb.net or WPF C#

Should I be handling the "IF Not selNum.Contains(nxtNum)" statement differently?

Viewing all articles
Browse latest Browse all 15722

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>