Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

array puzzle

Status
Not open for further replies.

vttech

Technical User
Jan 28, 2006
297
US
I want to check an array to see if a random number that I am generating is already in the array. if the number is it will not add it to the array but if its not in the array it will add it to the array. the array I am talking about is named LottoNumbers and the code is below. currenty if adds all the random numbers to the array.



Code:
 Dim LottoNumbers As Integer()
    Dim intNum As Integer



    Private Sub btnPlay_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnPlay.Click
        Dim number As Integer
        Dim randomGenerator As New Random

        ClearTextBoxes()

        For num As Integer = 1 To 6
            number = randomGenerator.Next(1, 49)



            ReDim Preserve LottoNumbers(intNum)
            LottoNumbers(intNum) = number
            intNum += 1


            If lblNum1.Text = "" Then
                lblNum1.Text = number.ToString
            ElseIf lblNum2.Text = "" Then
                lblNum2.Text = number.ToString
            ElseIf lblNum3.Text = "" Then
                lblNum3.Text = number.ToString
            ElseIf lblNum4.Text = "" Then
                lblNum4.Text = number.ToString
            ElseIf lblNum5.Text = "" Then
                lblNum5.Text = number.ToString
            ElseIf lblNum6.Text = "" Then
                lblNum6.Text = number.ToString
            End If
        Next


    End Sub

    Private Sub ClearTextBoxes()

        Dim ctrls As Control

        For Each ctrls In Me.Controls
            If TypeOf ctrls Is Label Then
                ctrls.Text = ""
            End If
        Next
    End Sub

Newbie in search of knowledge
 
try using a hashtable this will trhow an error if you aad the same object.

and a redim preserve is slow.

Christiaan Baes
Belgium

"My new site" - Me
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top