Re: [RESOLVED] Random Number
Also, keep in mind that the first number you provide to Random.Next is inclusive, but the second one is not; so if you want a random number between 1 and 3, you should generate it like this:
Re: [RESOLVED] Random Number
EDIT: Should I have started a new post for this, since I had already marked this one as Resolved? Bummer I can't Unresolve it.
Alright, I finally get everything flowing smoothly and my code is about 2/3 smaller, so I'm feeling good, however slight hiccup. In my old code, I had it so that there was a 3/4th chance of selecting RadioButton0 and a 1/4th chance of selecting RadioButton1. By itself, this code gives a 50% chance:
Code:
Dim stSuper As String = "radiogroup_2" & rand.Next(0, 2).ToString
For Each ele As HtmlElement In WebWindow.WebBrowser1.Document.All
If ele.GetAttribute("id").ToLower = stSuper Then
ele.InvokeMember("click")
End If
Next
So I tried this:
Code:
Dim OneInFour As String = rand.Next(1, 5).ToString
If OneInFour = 1 Then Dim stSuper As String = "radiogroup_20".ToString Else
If OneInFour <> 1 Then Dim stSuper As String = "radiogroup_2" & rand.Next(0, 2).ToString
For Each ele As HtmlElement In WebWindow.WebBrowser1.Document.All
If ele.GetAttribute("id").ToLower = stSuper Then
ele.InvokeMember("click")
End If
Next
Which is giving me the error:
Quote:
Variable stSuper hides a variable in an enclosing block
Any idea's how to resolve this? And one more thing while it's on my mind; if I turn OneInFour into a Sub, will it randomize every time I call it, or would I have basically just Dim a new OneInFour for every iteration I want?
Re: [RESOLVED] Random Number
Hi,
Quote:
Variable stSuper hides a variable in an enclosing block
To specifically answer your question, the reason you are getting this error is due to fact that you are declaring the variable within the If statements and therefore the scope of this variable is only visible within the If blocks and not outside. To solve this you would declare the variable outside of the If block. i.e:-
Code:
Dim OneInFour As String = rand.Next(1, 5).ToString
Dim stSuper As String
If OneInFour = "1" Then
stSuper = "radiogroup_20"
Else
stSuper = "radiogroup_2" & rand.Next(0, 2).ToString
End If
For Each ele As HtmlElement In WebWindow.WebBrowser1.Document.All
If ele.GetAttribute("id").ToLower = stSuper Then
ele.InvokeMember("click")
End If
Next
That said, and if I understand your comments correctly, this is not going to do what you want since you want 1 in 4 chances of picking RadionButton20 and 3 in 4 chances of picking RadioButton21. If I am correct then you would want to say:-
Code:
Dim OneInFour As String = rand.Next(1, 5).ToString
Dim stSuper As String
If OneInFour = "1" Then
stSuper = "radiogroup_20"
Else
stSuper = "radiogroup_21"
End If
Quote:
And one more thing while it's on my mind; if I turn OneInFour into a Sub, will it randomize every time I call it, or would I have basically just Dim a new OneInFour for every iteration I want?
If you put this into a Sub then you would still need to call rand.Next to get any random number in the range that you want.
In addition to this, turn Option Strict On and keep it on. It will help you to eradicate type errors in the future. I have fixed these in the examples shown above.
Hope that helps.
Cheers,
Ian