whittlephilip
asked on
How to retrieve value
I have a random function that generates an index value based on my data set. My problem is how to retrieve that index value for use in another segment of code using the same data set on the same form? Again, I only need the index value generated. I'm open to suggestions (trial & error method is killing me). Thanks.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
>> Am I "accidentally" creating another instance of the index?
In your checkme() sub, you don't need to to call getRndIndex() again as this will overwrite the value you previously picked and used:
Public Sub checkme()
' rndIndex = getRndIndex() ' don't need this call...
' code...
End Sub
In your checkme() sub, you don't need to to call getRndIndex() again as this will overwrite the value you previously picked and used:
Public Sub checkme()
' rndIndex = getRndIndex() ' don't need this call...
' code...
End Sub
ASKER
Perfect!!! Thank you!
ASKER
I feel your suggestion is what I'm looking for, but I'm still getting different values for the index. I know it is due to my own ignorance. Here's how I implemented the code starting with my form class ...
Public Class frmMain
Private iIndex As Integer
Private rndIndex As Integer
Then creating the random generation routine based on your suggestion ...
Private Function getRndIndex() As Integer
Dim ds As Data.DataSet = TestbankDataSet
Dim MaxCount As Integer = ds.Tables("TestBank").Rows
Dim rand As New Random(CInt(Date.Now.Ticks
rndIndex = rand.Next(0, MaxCount)
Return rndIndex
End Function
And implementing the function into ...
Private Sub RandomizeData()
rndIndex = getRndIndex()
Dim ds As Data.DataSet = TestbankDataSet
Dim rand As New Random(CInt(Date.Now.Ticks
Dim drow As Data.DataRow = ds.Tables("TestBank").Rows
With ds.Tables("TestBank")
txtQuestion.Text = drow.Item("Question")
txtAnswer1.Text = drow.Item("Answer1")
txtAnswer2.Text = drow.Item("Answer2")
txtAnswer3.Text = drow.Item("Answer3")
txtQID.Text = drow.Item("QuestionID")
End With
End Sub
Then into this routine, which is where I need the same value implemented. Have I overlooked something? Am I "accidentally" creating another instance of the index?
Public Sub checkme()
rndIndex = getRndIndex()
Dim ds As Data.DataSet = TestbankDataSet
Dim drow As Data.DataRow = ds.Tables("TestBank").Rows
Select Case iIndex
Case 1
If chkT1.Checked Then
If drow("TruthA1") Then
PictureBox1.BackgroundImag
Else
PictureBox1.BackgroundImag
End If
ElseIf chkT2.Checked Then
If drow("TruthA2") Then
PictureBox1.BackgroundImag
Else
PictureBox1.BackgroundImag
End If
ElseIf chkT3.Checked Then
If drow("TruthA3") Then
PictureBox1.BackgroundImag
Else
PictureBox1.BackgroundImag
End If
End If
End Select
End Sub