Hi,
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:-Variable stSuper hides a variable in an enclosing block
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_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
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 IfIf 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.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?
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




Reply With Quote