Jump to content

Welcome to Geeks to Go - Register now for FREE

Geeks To Go is a helpful hub, where thousands of volunteer geeks quickly serve friendly answers and support. Check out the forums and get free advice from the experts. Register now to gain access to all of our features, it's FREE and only takes one minute. Once registered and logged in, you will be able to create topics, post replies to existing threads, give reputation to your fellow members, get your own private messenger, post status updates, manage your profile and so much more.

Create Account How it Works
Photo

Comparing array of strings with another string


  • Please log in to reply

#1
sand

sand

    New Member

  • Member
  • Pip
  • 5 posts
Hi,

I am using the following code for my program.

For k = 0 To singlesArray.Length - 1 
If singlesArray(k).Equals(name) Then 
singlesArray.SetValue(name, k) 
End If 
Next

What I am trying to do is compare each element in the array with the string. If the string is the same it should ignore the duplicate the record. However if it is not a duplicate record, the string should be added into the array. However, I get the foillwoing runtime error message.

"A first chance exception of type 'System.NullReferenceException' occurred in Admin Login.EXE" which points to the second line of code. Does anybody know how to solve this problem? Your help would be very much appreciated. Thanks.
  • 0

Advertisements


#2
stu_design

stu_design

    Member

  • Member
  • PipPipPip
  • 217 posts
whats this all about

Admin Login.EXE


give me exactly what u are trying to do
  • 0

#3
sand

sand

    New Member

  • Topic Starter
  • Member
  • Pip
  • 5 posts
Admin.Exe is my file name.

What i am trying to do is get a name randomly from a table and insert it into an array. There should not be any duplicate records in the array. Below is my code.

   Private Sub SinglesToolStripMenuItem1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles SinglesToolStripMenuItem1.Click
        pnlSingles.Show()
        Dim i As New Integer
        Dim j As New Integer
        Dim k As New Integer
        Dim name As String
        Dim singlesArray() As String = {"a"}      
        Dim Table As DataTable = GetSingles()

        Dim x As New Random

        'get a random row...
        For i = 0 To Table.Rows.Count
            Dim tr As DataRow = Table.Rows(x.Next(0, Table.Rows.Count))
            ReDim Preserve singlesArray(i)
            'display the value of the name column...
            name = tr("Name")
            'singlesArray.SetValue(name, i)

            'If NameNotAlreadyGenerated(singlesArray, name) Then
            'singlesArray.SetValue(name, i)
            'End If
            For k = 0 To singlesArray.Length - 1
                If singlesArray(k).Equals(name) Then
                    singlesArray.SetValue(name, k)
                End If
            Next
            'Debug.WriteLine(singlesArray(i))
        Next
        For j = 0 To singlesArray.Length - 1
            Debug.WriteLine(singlesArray(j))
        Next
    End Sub
Function NameNotAlreadyGenerated(ByVal singlesArray() As String, ByVal Name As String) As Boolean
        If Array.IndexOf(singlesArray, Name) <> 0 Then
            Return True
        Else
            Return False
        End If
    End Function
End Class

However, the array still contains duplicate records. How do I solve it? Can somebody help pls? Thanx
  • 0

#4
stu_design

stu_design

    Member

  • Member
  • PipPipPip
  • 217 posts
why dont you look here

http://www.itnewsgro.../topic2277.aspx

take a look on google on the dictionary thing or take a look at the last post

Stu Design
  • 0

#5
sand

sand

    New Member

  • Topic Starter
  • Member
  • Pip
  • 5 posts
Hi,

I tried the last post. However, where does it add the record in the array? Am confused.
  • 0






Similar Topics

0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users

As Featured On:

Microsoft Yahoo BBC MSN PC Magazine Washington Post HP