-
Aug 26th, 2020, 12:20 PM
#1
Thread Starter
Junior Member
[RESOLVED] If Not list.Contains(variable) not working
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?
-
Aug 26th, 2020, 12:26 PM
#2
Re: If Not list.Contains(variable) not working
You set onList to True whether the list contains the number or not, so it will never loop.
It seems like if you set a breakpoint and just stepped through the code once, you should have seen the error in the logic.
"Anyone can do any amount of work, provided it isn't the work he is supposed to be doing at that moment" Robert Benchley, 1930
-
Aug 26th, 2020, 12:27 PM
#3
Re: If Not list.Contains(variable) not working
You also set nxtNum twice, once before the If and again a couple of lines after it... remove the second line that sets it, and it will probably be fine.
-
Aug 26th, 2020, 12:41 PM
#4
Thread Starter
Junior Member
Re: If Not list.Contains(variable) not working
Originally Posted by passel
You set onList to True whether the list contains the number or not, so it will never loop.
It seems like if you set a breakpoint and just stepped through the code once, you should have seen the error in the logic.
I had tried putting it in so many different lines I'd forgotten where it was when I posted the code
Originally Posted by si_the_geek
You also set nxtNum twice, once before the If and again a couple of lines after it... remove the second line that sets it, and it will probably be fine.
Oh my days, I'm such an idiot! Why didn't I see that
Cheers, you've saved me from throwing my laptop out of the window hahaha
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|