Link to home
Start Free TrialLog in
Avatar of whittlephilip
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
Avatar of Mike Tomlinson
Mike Tomlinson
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of whittlephilip
whittlephilip

ASKER

Hi Idle Mind --

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.Count - 1
        Dim rand As New Random(CInt(Date.Now.Ticks And Integer.MaxValue))

        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 And Integer.MaxValue))
        Dim drow As Data.DataRow = ds.Tables("TestBank").Rows(rndIndex)
       
        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(rndIndex)
 
        Select Case iIndex
            Case 1
                If chkT1.Checked Then
                    If drow("TruthA1") Then
                        PictureBox1.BackgroundImage = PictureBox10.BackgroundImage
                    Else
                        PictureBox1.BackgroundImage = PictureBox11.BackgroundImage
                    End If
                ElseIf chkT2.Checked Then
                    If drow("TruthA2") Then
                        PictureBox1.BackgroundImage = PictureBox10.BackgroundImage
                    Else
                        PictureBox1.BackgroundImage = PictureBox11.BackgroundImage
                    End If
                ElseIf chkT3.Checked Then
                    If drow("TruthA3") Then
                        PictureBox1.BackgroundImage = PictureBox10.BackgroundImage
                    Else
                        PictureBox1.BackgroundImage = PictureBox11.BackgroundImage
                    End If
                End If
        End Select
    End Sub
>> 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
Perfect!!! Thank you!