Hi there,
I'd like to submit this program I'm working on to thr forum.
I want to display 6 randomly generated numbers on 6 command buttons as captions at the click of another button. One number will be selected only once.
Here's my code: - Public Class Form1
-
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
-
Dim x As Integer() = New Integer() {x(0), x(1), x(2), x(3), x(4), x(5)}
-
Dim m_blnUsed As Boolean() = _
-
New Boolean(x.GetUpperBound(0)) {}
-
-
Dim objrandom As Random = New Random()
-
Dim intrandom As Integer
-
Do
-
intrandom = objrandom.Next(0, m_blnUsed.Length)
-
Loop Until m_blnUsed(intrandom) = False
-
m_blnUsed(intrandom) = True
-
-
Dim index As Integer
-
For index = 0 To 5
-
x(index) = x(intrandom)
-
Next
-
-
Button3.Text = Str(x(0))
-
Button4.Text = Str(x(1))
-
Button5.Text = Str(x(2))
-
Button6.Text = Str(x(3))
-
Button7.Text = Str(x(4))
-
Button8.Text = Str(x(5))
-
-
End Sub
-
-
End Class
I'm getting an error message
variable 'x' is used before it has been assigned a value. A null reference exception could result at runtime.
at the beginning where I'm declaring and initializing the array of integers x(0)
Can anyone help please.
Thanks
33 2453
forgot to mention I'm using VB2005
Dim x As Integer() = New Integer() {x(0), x(1), x(2), x(3), x(4), x(5)}
Think what you are telling VB to do:
"I want to declare an array of integers called x, and initialize it to the values in X(0), X(1), ..."
So, VB is saying, duh, you don't have anything in x, so I cannot use it to initialize anything, much less itself.
What do you really want to initialize x to?
Also, I think there's a loop missing there. Unless I'm misreading the code, intrandom is only set once, so all six occurences of x (and hence the buttons) will be set to the same number.
Also, I think there's a loop missing there. Unless I'm misreading the code, intrandom is only set once, so all six occurences of x (and hence the buttons) will be set to the same number.
Oh, no! This is the same thread as http://www.thescripts.com/forum/thread636691.html with the same mistakes!
OK. I will write an article on random number generation and post it in our new Articles Tab. I'll post a link here afterwards.
Beat you to it, Sammy. :D Here is the link.
thanks a lot sammyB, killer42 for all your help; i've been going trough all the material you've suggested, even some of it i read before by searching, but still can't get my head round it. nevertheless still trying, here's the modified code - Public Class Form1
-
-
-
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
-
Dim x As Integer() = New Integer(6) {}
-
-
Dim randomgeberator As New Random
-
Dim index As Integer
-
For index = 0 To 5
-
x(index) = randomgeberator.Next(0, 20)
-
Next
-
-
Button3.Text = Str(x(0))
-
-
-
Button4.Text = Str(x(1))
-
Button5.Text = Str(x(2))
-
Button6.Text = Str(x(3))
-
Button7.Text = Str(x(4))
-
Button8.Text = Str(x(5))
-
-
End Sub
-
-
End Class
-
i'm having this at runtime on the immediate window
A first chance exception of type 'System.IndexOutOfRangeException' occurred in 2nd ind asgmt.exe
A first chance exception of type 'System.IndexOutOfRangeException' occurred in 2nd ind asgmt.exe
A first chance exception of type 'System.IndexOutOfRangeException' occurred in 2nd ind asgmt.exe
A first chance exception of type 'System.IndexOutOfRangeException' occurred in 2nd ind asgmt.exe
A first chance exception of type 'System.IndexOutOfRangeException' occurred in 2nd ind asgmt.exe
now i'm being able to select randomly 6 numbers and display them in the buttons, the bit i can't achieve is to select one number once only which i was trying to do with the boolean array but i'm having trouble to set it up
any help will be so much appreciated
thanks
I've been able to achieve what i wanted with the code - Dim x As Integer() = New Integer(10) {}
-
-
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
-
-
Dim m_blnUsed As Boolean() = _
-
New Boolean(x.GetUpperBound(0)) {}
-
-
-
Dim objrandom As Random = New Random()
-
Dim intrandom As Integer
-
Dim index As Integer
-
Do
-
intrandom = objrandom.Next(0, m_blnUsed.Length)
-
-
Loop Until m_blnUsed(intrandom) = False
-
m_blnUsed(intrandom) = True
-
-
-
For index = 0 To 5
-
x(index) = intrandom
-
Next
-
-
Button3.Text = Str(x(0))
-
-
Do
-
intrandom = objrandom.Next(0, m_blnUsed.Length)
-
-
Loop Until m_blnUsed(intrandom) = False
-
m_blnUsed(intrandom) = True
-
-
-
For index = 0 To 5
-
x(index) = intrandom
-
Next
-
Button4.Text = Str(x(1))
-
Do
-
intrandom = objrandom.Next(0, m_blnUsed.Length)
-
-
Loop Until m_blnUsed(intrandom) = False
-
m_blnUsed(intrandom) = True
-
-
-
For index = 0 To 5
-
x(index) = intrandom
-
Next
-
Button5.Text = Str(x(2))
-
Do
-
intrandom = objrandom.Next(0, m_blnUsed.Length)
-
-
Loop Until m_blnUsed(intrandom) = False
-
m_blnUsed(intrandom) = True
-
-
-
For index = 0 To 5
-
x(index) = intrandom
-
Next
-
Button6.Text = Str(x(3))
-
Do
-
intrandom = objrandom.Next(0, m_blnUsed.Length)
-
-
Loop Until m_blnUsed(intrandom) = False
-
m_blnUsed(intrandom) = True
-
-
-
For index = 0 To 5
-
x(index) = intrandom
-
Next
-
Button7.Text = Str(x(4))
-
Do
-
intrandom = objrandom.Next(0, m_blnUsed.Length)
-
-
Loop Until m_blnUsed(intrandom) = False
-
m_blnUsed(intrandom) = True
-
-
-
For index = 0 To 5
-
x(index) = intrandom
-
Next
-
Button8.Text = Str(x(5))
-
-
End Sub
-
-
End Class
i'm having this on immediate window
though at run time i'm still getting A first chance exception of type 'System.IndexOutOfRangeException' occurred in 2nd ind asgmt.exe
A first chance exception of type 'System.IndexOutOfRangeException' occurred in 2nd ind asgmt.exe
A first chance exception of type 'System.IndexOutOfRangeException' occurred in 2nd ind asgmt.exe
A first chance exception of type 'System.IndexOutOfRangeException' occurred in 2nd ind asgmt.exe
A first chance exception of type 'System.IndexOutOfRangeException' occurred in 2nd ind asgmt.exe
A first chance exception of type 'System.IndexOutOfRangeException' occurred in 2nd ind asgmt.exe
A first chance exception of type 'System.IndexOutOfRangeException' occurred in 2nd ind asgmt.exe
A first chance exception of type 'System.IndexOutOfRangeException' occurred in 2nd ind asgmt.exe
A first chance exception of type 'System.IndexOutOfRangeException' occurred in 2nd ind asgmt.exe
A first chance exception of type 'System.IndexOutOfRangeException' occurred in 2nd ind asgmt.exe
A first chance exception of type 'System.IndexOutOfRangeException' occurred in 2nd ind asgmt.exe
A first chance exception of type 'System.IndexOutOfRangeException' occurred in 2nd ind asgmt.exe
i'm more concern about the next stage which i don't have any clue, even how to start
what i want is the first of the buttons containing the selected numbers clicked, the number will be copied in the first of a 6 set of labels and so on.
to be more explicit, the 6 buttons will be clicked one at a time and their content copied in the 6 labels as going; the 1st button to be clicked to the 1st label, so on
i've been thinking (may be wrongly & sorry about that incase) there might be a way to set up an array of captions and assign them to the labels and i've been trying hard, but again i have no clue
any help pleasethis what i've been trying to - doDim y As CaptionButton() = New CaptionButton() {}
-
-
label1.text="y(0)"
label2.text=y(1)and i'm getting the error message
Declaration expected
at both the last 2 lines of the code (where i'm trying to assign the captions to the labels
What about if you just set up a form-level variable to count the number of times buttons have been clicked. Each time a button is clicked (up to 6, or whatever) you add 1 to that counter and use it to decide which label to update.
As for the way you're assigning the values to the buttons, it's good to see that you got it working, but I believe you can use a bit more loop processing to reduce the size of the code considerably. Remember, the basic idea behind a loop is that you should not need to do something more than once. Anything which you are coding repeatedly (for example, your For x = 0 to 5 loop setting up the x array) can usually be done inside a loop instead.
thanks once more again for helping, as i was saying i don't have any clue as to how to start, would it be possible to give an example of code please
thanks
i've been trying to set up a string array and assign the contents to each of the labels as needed with the code - Dim y As String() = New String() {}
-
-
Label1.Text="y(0)"
-
and getting the error message
Declaration expected
with a wigly line on Label1
i've been trying to set up a string array and assign the contents to each of the labels as needed with the code - Dim y As String() = New String() {}
-
-
Label1.Text="y(0)"
-
and getting the error message with a wigly line on Label1
You must have deleted Label1
You must have deleted Label1
unless if i don't get you, but i did not delete Label1 as i'm telling you the wigly line is on Label1, so it has to be there to have the wigly line on it
unless if i don't get you, but i did not delete Label1 as i'm telling you the wigly line is on Label1, so it has to be there to have the wigly line on it
No, it has to not be there to have a wigly line. :D When you hover over the wigly line, what is the error message?
No, it has to not be there to have a wigly line. :D When you hover over the wigly line, what is the error message?
i more confused now as i can see Label1, if i hover over it i get the same error message
Declaration expected
i more confused now as i can see Label1, if i hover over it i get the same error message
WHAT ERROR MESSAGE! :ROFL:
WHAT ERROR MESSAGE! :ROFL:
the error message is
Declaration expected
the error message is
hands up sammyB, i've just realised what i did was starting a new line after ending the precedind sub, so VB was telling me hang on you did not declare the new sub you're starting; a proof of how confused i am with this program, still struggling though with it, gonna be there, never give up
guys, how would i go about choosing between the buttons, for the 1st clicked store its content in the 1st label, the 2d button to the 2nd label, so on.
my problem is th choose between the first button which is clicked , whichever is this first, it can be the button in the line to be clicked first, or the last one in the line, it doesn't matter, the 1st clicked, its value assigned to 1st label, and so on
guys, how would i go about choosing between the buttons, for the 1st clicked store its content in the 1st label, the 2d button to the 2nd label, so on.
my problem is th choose between the first button which is clicked , whichever is this first, it can be the 3rd button in the line to be clicked first, or the last one in the line, it doesn't matter, the 1st clicked, its value assigned to 1st label, and so on
any suggestion, any hint, thanks
i've been able to display the Nos in the labels in order of theclick of the buttons
now i want to sort the Nos and display the sorted values in the buttons (from 2 to 6) in order to arrage them in ascending order
here's my code - Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
-
Dim NoOfElements As Integer, I As Integer, J As Integer, Temp As Integer
-
NoOfElements = 6
-
For I = 1 To (NoOfElements - 1)
-
For J = I To (NoOfElements - 1)
-
If x(I - 1) > x(J) Then
-
Temp = x(I - 1)
-
x(I - 1) = x(J)
-
x(J) = Temp
-
End If
-
Next J
-
Next I
-
Button3.Text = x(0)
-
-
Button4.Text = x(1)
-
-
Button5.Text = x(2)
-
-
Button6.Text = x(3)
-
-
Button7.Text = x(4)
-
-
Button8.Text = x(5)
it is displaying the No in button8 (the last one) in all of the buttons
i've been able to display the Nos in the labels in order of theclick of the buttons
now i want to sort the Nos and display the sorted values in the buttons (from 2 to 6) in order to arrage them in ascending order
here's my code - Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
-
Dim NoOfElements As Integer, I As Integer, J As Integer, Temp As Integer
-
NoOfElements = 6
-
For I = 1 To (NoOfElements - 1)
-
For J = I To (NoOfElements - 1)
-
If x(I - 1) > x(J) Then
-
Temp = x(I - 1)
-
x(I - 1) = x(J)
-
x(J) = Temp
-
End If
-
Next J
-
Next I
-
Button3.Text = x(0)
-
Button4.Text = x(1)
-
Button5.Text = x(2)
-
Button6.Text = x(3)
-
Button7.Text = x(4)
-
Button8.Text = x(5)
it is displaying the No in button8 (the last one) in all of the buttons
Sorry, been busy over the weekend.
It looks as though you've been gradually working your way through the problems, which is great. :)
Are you saying that after you use this sort code, you are getting the same value displayed in all of the labels?
Hm...
Ah! I remember now. I don't think this sort is to blame. If I remember correctly, your code which creates these numbers used the x() array in rather a strange way. I think you will find that if you interrupt your code and examine the array just before the sort, you'll find that they already contain the duplicated number. Assuming that is the case, you need to fix the original load of the values, not the sort.
nearly there!
i know as killer42 made the remark, it's not the best code but as long as it's working i'll try and improve it when finished, not to forget i've been programming only 2 months, enjoying it though.
now the last bit, after sorting the array with Array.Sort, all of the 6 buttons display always 0
here's my full code - Class Form1
-
Dim x As Integer() = New Integer(20) {}
-
-
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
-
-
Dim m_blnUsed As Boolean() = _
-
New Boolean(x.GetUpperBound(0)) {}
-
-
-
Dim objrandom As Random = New Random()
-
Dim intrandom As Integer
-
Dim index As Integer
-
Do
-
intrandom = objrandom.Next(0, m_blnUsed.Length)
-
-
Loop Until m_blnUsed(intrandom) = False
-
m_blnUsed(intrandom) = True
-
-
-
For index = 0 To 5
-
x(index) = intrandom
-
Next
-
-
Button3.Text = Str(x(0))
-
-
Do
-
intrandom = objrandom.Next(0, m_blnUsed.Length)
-
-
Loop Until m_blnUsed(intrandom) = False
-
m_blnUsed(intrandom) = True
-
-
-
For index = 0 To 5
-
x(index) = intrandom
-
Next
-
Button4.Text = Str(x(1))
-
Do
-
intrandom = objrandom.Next(0, m_blnUsed.Length)
-
-
Loop Until m_blnUsed(intrandom) = False
-
m_blnUsed(intrandom) = True
-
-
-
For index = 0 To 5
-
x(index) = intrandom
-
Next
-
Button5.Text = Str(x(2))
-
Do
-
intrandom = objrandom.Next(0, m_blnUsed.Length)
-
-
Loop Until m_blnUsed(intrandom) = False
-
m_blnUsed(intrandom) = True
-
-
-
For index = 0 To 5
-
x(index) = intrandom
-
Next
-
Button6.Text = Str(x(3))
-
Do
-
intrandom = objrandom.Next(0, m_blnUsed.Length)
-
-
Loop Until m_blnUsed(intrandom) = False
-
m_blnUsed(intrandom) = True
-
-
-
For index = 0 To 5
-
x(index) = intrandom
-
Next
-
Button7.Text = Str(x(4))
-
Do
-
intrandom = objrandom.Next(0, m_blnUsed.Length)
-
-
Loop Until m_blnUsed(intrandom) = False
-
m_blnUsed(intrandom) = True
-
-
-
For index = 0 To 5
-
x(index) = intrandom
-
Next
-
Button8.Text = Str(x(5))
-
-
Label2.Text = ""
-
Label3.Text = ""
-
Label4.Text = ""
-
Label5.Text = ""
-
Label6.Text = ""
-
Label7.Text = ""
-
End Sub
-
-
Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click, Button4.Click, Button5.Click, Button6.Click, Button7.Click, Button8.Click
-
Dim index As Integer = sender.ToString.IndexOf(":") + 2
-
Dim btnName As Object = sender.ToString.Substring(index)
-
-
If Label2.Text = "" Then
-
'load the name from btnName into the Text property of the Label
-
Label2.Text = btnName
-
ElseIf Label3.Text = "" Then
-
'load the name from btnName into the Text property of the Label
-
Label3.Text = btnName
-
ElseIf Label4.Text = "" Then
-
'load the name from btnName into the Text property of the Label
-
Label4.Text = btnName
-
ElseIf Label5.Text = "" Then
-
'load the name from btnName into the Text property of the Label
-
Label5.Text = btnName
-
ElseIf Label6.Text = "" Then
-
'load the name from btnName into the Text property of the Label
-
Label6.Text = btnName
-
ElseIf Label7.Text = "" Then
-
'load the name from btnName into the Text property of the Label
-
Label7.Text = btnName
-
End If
-
-
End Sub
-
-
Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
-
-
Array.Sort(x)
-
-
Button3.Text = x(0)
-
-
Button4.Text = x(1)
-
-
Button5.Text = x(2)
-
-
Button6.Text = x(3)
-
-
Button7.Text = x(4)
-
-
Button8.Text = x(5)
-
-
End Sub
-
-
-
End Class
nearly there!
Keep on plugging away - you'll get there.
i know as killer42 made the remark, it's not the best code but as long as it's working i'll try and improve it when finished...
Although it's probably very poor programming practice, I tend to do that, too. Throw together something (a "proof of concept") that works ASAP, then worry about the niceties like documenting, commenting, naming standards, nice structure, user interface, etc etc etc.
...not to forget i've been programming only 2 months, enjoying it though.
That's the spirit! :) You're doing fine.
now the last bit, after sorting the array with Array.Sort, all of the 6 buttons display always 0
Honestly, I would completely disregard the sorting and so on, until you get the initial load of the array worked out. At present, the contents of your array have nothing to do with the contents of your command buttons.
Consider the following, in your code... - For index = 0 To 5
-
x(index) = intrandom
-
Next
-
This piece of code says to place the exact same value (intrandom) in every occurrence of the x() array. If you actually meant to do this (unlikely), there would be no point using an array – you could just use a single variable, x. In any case, next time you do it, you replace the entire array, so there's no record of what you have just placed there.
What you need to do, I think, is to go back and examine the underlying logic to set up the array. I believe it should go more like this... -
For Index = each element of x() array
-
Start loop
-
Generate a random number
-
If it doesn’t match any entry in the array, Then
-
Place the value in x(Index)
-
Exit the loop
-
End If
-
End Loop
-
Next
Then load the values from the array into the buttons. Now they match.
Keep on plugging away - you'll get there.
Although it's probably very poor programming practice, I tend to do that, too. Throw together something (a "proof of concept") that works ASAP, then worry about the niceties like documenting, commenting, naming standards, nice structure, user interface, etc etc etc.
That's the spirit! :) You're doing fine.
Honestly, I would completely disregard the sorting and so on, until you get the initial load of the array worked out. At present, the contents of your array have nothing to do with the contents of your command buttons.
Consider the following, in your code... - For index = 0 To 5
-
x(index) = intrandom
-
Next
-
This piece of code says to place the exact same value (intrandom) in every occurrence of the x() array. If you actually meant to do this (unlikely), there would be no point using an array – you could just use a single variable, x. In any case, next time you do it, you replace the entire array, so there's no record of what you have just placed there.
What you need to do, I think, is to go back and examine the underlying logic to set up the array. I believe it should go more like this... -
For Index = each element of x() array
-
Start loop
-
Generate a random number
-
If it doesn’t match any entry in the array, Then
-
Place the value in x(Index)
-
Exit the loop
-
End If
-
End Loop
-
Next
Then load the values from the array into the buttons. Now they match.
Thanks a lot killer42
This is what I came up with, 2 attempts
The first - Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
-
Dim index As Integer, randomgenerator As New Random, computerchoice As Integer
-
For index = 0 To 5
-
Do While computerchoice = randomgenerator.Next(0, 19)
-
If computerchoice <> x(index) Then
-
x(index) = computerchoice
-
End If
-
Loop
-
Next
-
Button3.Text = Str(x(0))
-
Button4.Text = Str(x(1))
-
Button5.Text = Str(x(2))
-
Button6.Text = Str(x(3))
-
Button7.Text = Str(x(4))
-
Button8.Text = Str(x(5))
which is displaying 0 in all the command buttons
And the second - Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
-
Dim index As Integer, randomgenerator As New Random, computerchoice As Integer
-
For index = 0 To 5
-
computerchoice = randomgenerator.Next(0, 19)
-
If computerchoice <> x(index) Then
-
x(index) = computerchoice
-
End If
-
Next
-
Button3.Text = Str(x(0))
-
Button4.Text = Str(x(1))
-
Button5.Text = Str(x(2))
-
Button6.Text = Str(x(3))
-
Button7.Text = Str(x(4))
-
Button8.Text = Str(x(5))
which is displaying one number more than once, ie 3 displayed in button4 & button6
Both of the two solutions above are displaying always 0 in all the buttons after sorting the array
The first - Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
-
Dim index As Integer, randomgenerator As New Random, computerchoice As Integer
-
For index = 0 To 5
-
Do While computerchoice = randomgenerator.Next(0, 19)
-
If computerchoice <> x(index) Then
-
x(index) = computerchoice
-
End If
-
Loop
-
Next
I'd like you to examine this one, and see whether you can figure out why it doesn't work. As a hint, I've highlighted the line where I think you've mainly got "the wrong end of the stick".
Remember, to successfully debug a program, in many cases the key is to think like a computer. That means you forget for the moment about what you want to achieve at the end, and simply step through precisely what each statement does.
I'd like you to examine this one, and see whether you can figure out why it doesn't work. As a hint, I've highlighted the line where I think you've mainly got "the wrong end of the stick".
Remember, to successfully debug a program, in many cases the key is to think like a computer. That means you forget for the moment about what you want to achieve at the end, and simply step through precisely what each statement does.
i'm running out of ideas, i know i need a condition after the while (what would it be, that i'm not sure), anyway, i've turned around the loop in so many positions (do...., loop until; do while.....loop, for.....next); i even don't get why do we need this loop in a loop (the outer loop for....next)
all i know is i need a push now
thanks
this is one of my try - For index = 0 To 5
-
Do While index <= 5
-
computerchoice = randomgenerator.Next(0, 21)
-
If computerchoice <> x(index) Then
-
x(index) = computerchoice
-
End If
-
Loop
-
Next
I don't remember what I suggested earlier, but you don't necessarily have to have a condition on a loop. (Have a look at the awesome power of nested loops :)). You could do something like... - For Index = x To y
-
Do
-
Generate random number
-
Look for it in the array
-
If not found
-
Add it to the array
-
Exit Do
-
End If
-
Loop
-
Next
:D Now you know how "infinite loops" come to exist. If the code in the middle fails to trigger the Exit Do, then of course this code will run forever. Well, until you interrupt it anyway.
I'll be on lunch in about 4 hours, should be able to help more then.
I don't remember what I suggested earlier, but you don't necessarily have to have a condition on a loop. (Have a look at the awesome power of nested loops :)). You could do something like... - For Index = x To y
-
Do
-
Generate random number
-
Look for it in the array
-
If not found
-
Add it to the array
-
Exit Do
-
End If
-
Loop
-
Next
:D Now you know how "infinite loops" come to exist. If the code in the middle fails to trigger the Exit Do, then of course this code will run forever. Well, until you interrupt it anyway.
I'll be on lunch in about 4 hours, should be able to help more then.
Thanks a lot killer42, i really appreciated it.
Here's my code now, which is not too bad, with help i must acknowledge.
Now the very last bit, i want to check the answers with the if...then...else statement at the end of the program, but i don't know, it its always running the else part, it is in all cases displaying the incorrect message on the red background here's my final code - Public Class Form1
-
Dim x As Integer() = New Integer(5) {}
-
-
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
-
-
Label1.Text = "Select in ascending order by clicking on the numbers"
-
Label1.BackColor = Color.Empty
-
-
Dim m_blnUsed As Boolean() = _
-
New Boolean(20) {}
-
-
-
Dim objrandom As Random = New Random()
-
Dim intrandom As Integer
-
-
Do
-
intrandom = objrandom.Next(0, m_blnUsed.Length)
-
-
Loop Until m_blnUsed(intrandom) = False
-
m_blnUsed(intrandom) = True
-
-
'add the random value into the x array
-
x(0) = intrandom
-
-
-
Button3.Text = Str(x(0))
-
-
Do
-
intrandom = objrandom.Next(0, m_blnUsed.Length)
-
-
Loop Until m_blnUsed(intrandom) = False
-
m_blnUsed(intrandom) = True
-
-
'add the random value into the x array
-
x(1) = intrandom
-
-
Button4.Text = Str(x(1))
-
Do
-
intrandom = objrandom.Next(0, m_blnUsed.Length)
-
-
Loop Until m_blnUsed(intrandom) = False
-
m_blnUsed(intrandom) = True
-
-
'add the random value into the x array
-
x(2) = intrandom
-
-
Button5.Text = Str(x(2))
-
Do
-
intrandom = objrandom.Next(0, m_blnUsed.Length)
-
-
Loop Until m_blnUsed(intrandom) = False
-
m_blnUsed(intrandom) = True
-
-
'add the random value into the x array
-
x(3) = intrandom
-
-
Button6.Text = Str(x(3))
-
Do
-
intrandom = objrandom.Next(0, m_blnUsed.Length)
-
-
Loop Until m_blnUsed(intrandom) = False
-
m_blnUsed(intrandom) = True
-
-
'add the random value into the x array
-
x(4) = intrandom
-
-
Button7.Text = Str(x(4))
-
Do
-
intrandom = objrandom.Next(0, m_blnUsed.Length)
-
-
Loop Until m_blnUsed(intrandom) = False
-
m_blnUsed(intrandom) = True
-
-
'add the random value into the x array
-
x(5) = intrandom
-
-
Button8.Text = Str(x(5))
-
-
Label2.Text = ""
-
Label3.Text = ""
-
Label4.Text = ""
-
Label5.Text = ""
-
Label6.Text = ""
-
Label7.Text = ""
-
End Sub
-
-
Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click, Button4.Click, Button5.Click, Button6.Click, Button7.Click, Button8.Click
-
Dim index As Integer = sender.ToString.IndexOf(":") + 2
-
Dim btnName As Object = sender.ToString.Substring(index)
-
-
If Label2.Text = "" Then
-
'load the name from btnName into the Text property of the Label
-
Label2.Text = btnName
-
ElseIf Label3.Text = "" Then
-
'load the name from btnName into the Text property of the Label
-
Label3.Text = btnName
-
ElseIf Label4.Text = "" Then
-
'load the name from btnName into the Text property of the Label
-
Label4.Text = btnName
-
ElseIf Label5.Text = "" Then
-
'load the name from btnName into the Text property of the Label
-
Label5.Text = btnName
-
ElseIf Label6.Text = "" Then
-
'load the name from btnName into the Text property of the Label
-
Label6.Text = btnName
-
ElseIf Label7.Text = "" Then
-
'load the name from btnName into the Text property of the Label
-
Label7.Text = btnName
-
End If
-
-
End Sub
-
-
Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
-
-
Array.Sort(x)
-
-
Button3.Text = x(0)
-
-
Button4.Text = x(1)
-
-
Button5.Text = x(2)
-
-
Button6.Text = x(3)
-
-
Button7.Text = x(4)
-
-
Button8.Text = x(5)
-
-
If Label2.Text = Button3.Text And Label3.Text = Button4.Text And Label4.Text = Button5.Text And Label5.Text = Button6.Text And Label6.Text = Button7.Text And Label7.Text = Button8.Text Then
-
-
Label1.Text = "Correct, well done!"
-
Label1.BackColor = Color.Green
-
Else
-
Label1.Text = "Sorry, incorrect!"
-
Label1.BackColor = Color.Red
-
End If
-
End Sub
-
-
-
End Class
i want to check the answers with the if...then...else statement at the end of the program, but i don't know, it its always running the else part...
Have you interrupted the code and examined the values at that point, to see what doesn't match?
Have you interrupted the code and examined the values at that point, to see what doesn't match?
as a matter of fact, i don't know how to interrupt the code and always wondered how to do it
as a matter of fact, i don't know how to interrupt the code and always wondered how to do it
Before running the code, click on the grey-bar to the left of your code, beside the line of code you are interested in. Then when you run your code, it will "break" (temporally stop, waiting for you) just before it executes that line. You can examine objects and variables by hovering over them, step to the next statement (F10), step into a subroutine(F11), or resume execution(F5).
Before running the code, click on the grey-bar...
You should learn to use the debugging facilities built into VB - they are essential for debugging, or just making sure you understand, your code.
They allow you to stop the execution, examine and change values, and step through the execution so that you can watch exactly what is executing. Just seeing precisely which statements are executed can often show up bugs. " Oh, I thought that IF would be FALSE...".
Sign in to post your reply or Sign up for a free account.
Similar topics
by: tao_benz |
last post by:
Hi:
My system generates a bunch of integers, about 1000. There is no any
relationship between those integers; the smallest one might only
contain 1 digit and the biggest one might contain 6...
|
by: Jack |
last post by:
I have two files:
sort_comparison.c++
my_sort.h
sort_comparison.c++ calls this code in my_sort.h:
void my_sort::fillArray(int arr,int n)
{
// const int random_number_range=1000000;
|
by: Baz |
last post by:
I want to randomly select some files from a source directory & copy them to
a target folder for testing. How can I select files from the source
directory randomly?
I have thought about getting a...
|
by: RJN |
last post by:
Hi
I have to randomly pickup records from database for some quality check
of data. Say the data base has about 1000 records, I have to randomly
select about 50 records. Can someone suggest me...
|
by: RadiationX |
last post by:
I have a problem that I really don't understand at all. In my previous
post I could get started on my projects I just had a few problems with
syntax errors. This problem is something that I don't...
|
by: wisten |
last post by:
This program is to create a simple application to generate random number between 1 and 50.
additional: when the user click on the Start button, a random number will be displayed every 300ms.Display...
|
by: JuAn2226 |
last post by:
How do I create database which will store the random number that generated before I reset the random number and how do I check that new random number after I reset is exists in database. If exist...
|
by: Suprim |
last post by:
I am using a timer to call a random number range from 1 to 50 and display it in a label. How can i display the number without repeating the number that have been call out?
For example, if number...
|
by: jhernandy |
last post by:
I have built a Table using HTML and would like to have a random number genereator, genereate numbers in each of the 5 boxes on the table with no duplicates between 1-50, I have a problem getting the...
|
by: emmanuelkatto |
last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud.
Please let me know.
Thanks!
Emmanuel
|
by: BarryA |
last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
|
by: Sonnysonu |
last post by:
This is the data of csv file
1 2 3
1 2 3
1 2 3
1 2 3
2 3
2 3
3
the lengths should be different i have to store the data by column-wise with in the specific length.
suppose the i have to...
|
by: Hystou |
last post by:
There are some requirements for setting up RAID:
1. The motherboard and BIOS support RAID configuration.
2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
|
by: marktang |
last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
|
by: Hystou |
last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
|
by: Hystou |
last post by:
Overview:
Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
|
by: tracyyun |
last post by:
Dear forum friends,
With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
| |