Jump to content

Welcome to Geeks to Go - Register now for FREE

Need help with your computer or device? Want to learn new tech skills? You're in the right place!
Geeks to Go is a friendly community of tech experts who can solve any problem you have. Just create a free account and post your question. Our volunteers will reply quickly and guide you through the steps. Don't let tech troubles stop you. Join Geeks to Go now and get the support you need!

How it Works Create Account
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