Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 209
  • Last Modified:

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.
0
whittlephilip
Asked:
whittlephilip
  • 2
  • 2
1 Solution
 
Mike TomlinsonMiddle School Assistant TeacherCommented:
Store the index value at a class level:

Public Class Form1
    Inherits System.Windows.Forms.Form

    Private rndIndex As Integer

    Private Function getRndIndex() As Integer
        ' your code that picks the index
        Return someRandomIndexBasedOnYourDataSet
    End Function

    Private Sub Foo()
        rndIndex = getRndIndex() ' set the class level variable...
    End Sub

    Private Sub FooBar()
        ' do something with rndIndex in here....
        Debug.WriteLine(rndIndex.ToString)
    End Sub

End Class
0
 
whittlephilipAuthor Commented:
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
0
 
Mike TomlinsonMiddle School Assistant TeacherCommented:
>> 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
0
 
whittlephilipAuthor Commented:
Perfect!!! Thank you!
0

Featured Post

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

  • 2
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now