How to search through array in Visual Basic?

In my program I have an array that contains the results of the rolls of a die. The user input how many times they wanted it rolled. I need to search through that array to find how many times a certain number shows up (also one that the user chose) to find it’s probability.

So I’d like a loop (or something else, but a loops is the only thing I can think of) to be able to loop through the array and keep track of how many times it shows up. I was going to use UBound and LBound, but because I’m finding the probability in a different procedure it won’t work.

Here’s what I have

Public Class Form1
Dim intTosses As Integer
Dim ResultTosses() As Integer
Dim intFindProb As Integer ‘This is the number that the user wants to see the probability for

Private Sub txtNumTosses_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles txtNumTosses.TextChanged
Dim intcounter As Integer
Dim intResult As Integer
intTosses = Val(txtNumTosses.Text)
ReDim ResultTosses(intTosses)

For intcounter = 1 To intTosses
intResult = Int(6 * Rnd(1) + 1)
ResultTosses(intcounter) = intResult
Next
lblProbability.Text = ResultTosses(intTosses)

End Sub

Private Sub txtNumProbability_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles txtNumProbability.TextChanged
Dim intpcounter As Integer
intFindProb = (Val(txtNumProbability.Text)
‘****Here is where I need to find out how many of intFindProb are in my ResultTosses Array****
If

Next
End Sub
End Class

If you could give a different example or just explain it that would be good, as it is something I need to pass in so I would like to put it together myself (we are allowed to ask for help, but the teacher is always busy), but I’m fairly lost so I’ll take what I can get.

The following code will allow the user to enter the number of rolls for one die that he wants to place in an array; and the two search numbers he desires to see howmany times they appear in the array. The number of occurrances for the two search numbers will appear in labels 1 and 2. You can use these 2 numbers for any purpose.

There is a button, 3 text boxes and 2 labels on my form.

Public Class Form1

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim HowMany As Integer
Dim Array(500) As Integer
Dim MaxIndex As Integer
Dim SearchNum1 As Integer = Val(TextBox2.Text)
Dim SearchNum2 As Integer = Val(TextBox3.Text)
Dim SearchNum1Cnt, SearchNum2Cnt As Integer
If IsNumeric(TextBox1.Text) Then
HowMany = Val(TextBox1.Text)
MaxIndex = HowMany – 1
End If
Dim Rnd As New Random
For i = 0 To MaxIndex
Array(i) = Rnd.Next(1, 7)
Next
For i = 0 To MaxIndex
‘MsgBox(i & " " & Array(i) & " " & SearchNum1)
If Array(i) = SearchNum1 Then
SearchNum1Cnt += 1
End If
If Val(Array(i)) = SearchNum2 Then
SearchNum2Cnt += 1
End If
Next
Label1.Text = SearchNum1Cnt
Label2.Text = SearchNum2Cnt
End Sub

End Class

One Response to “How to search through array in Visual Basic?”

  1. texasmaverick says:

    The following code will allow the user to enter the number of rolls for one die that he wants to place in an array; and the two search numbers he desires to see howmany times they appear in the array. The number of occurrances for the two search numbers will appear in labels 1 and 2. You can use these 2 numbers for any purpose.

    There is a button, 3 text boxes and 2 labels on my form.

    Public Class Form1

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
    Dim HowMany As Integer
    Dim Array(500) As Integer
    Dim MaxIndex As Integer
    Dim SearchNum1 As Integer = Val(TextBox2.Text)
    Dim SearchNum2 As Integer = Val(TextBox3.Text)
    Dim SearchNum1Cnt, SearchNum2Cnt As Integer
    If IsNumeric(TextBox1.Text) Then
    HowMany = Val(TextBox1.Text)
    MaxIndex = HowMany – 1
    End If
    Dim Rnd As New Random
    For i = 0 To MaxIndex
    Array(i) = Rnd.Next(1, 7)
    Next
    For i = 0 To MaxIndex
    ‘MsgBox(i & " " & Array(i) & " " & SearchNum1)
    If Array(i) = SearchNum1 Then
    SearchNum1Cnt += 1
    End If
    If Val(Array(i)) = SearchNum2 Then
    SearchNum2Cnt += 1
    End If
    Next
    Label1.Text = SearchNum1Cnt
    Label2.Text = SearchNum2Cnt
    End Sub

    End Class
    References :

Leave a Reply