VB.net Issue with a string comparison failing

Mythikos Source

Below is the snippit of code that is having the trouble.

Private Const DOB_VALUE As Integer = 0
Private Const ADDRESS_VALUE As Integer = 1
Private Const NAME_VALUE As Integer = 2

For Each oRecord As KeyValuePair(Of Integer, String) In OriginalFileInfo
        For Each nRecord As KeyValuePair(Of Integer, String) In WorkingFileInfo
            Dim OriginalComparisonStringSubstrings As String() = oRecord.Value.ToString.ToLower.Split(":")
            Dim WorkingComparisonStringSubstrings As String() = nRecord.Value.ToString.ToLower.Split(":")

            ' Are dates of birth the same?
            If OriginalComparisonStringSubstrings(DOB_VALUE) Like WorkingComparisonStringSubstrings(DOB_VALUE) Then
                ' Are the address the same?
                If OriginalComparisonStringSubstrings(ADDRESS_VALUE) Like WorkingComparisonStringSubstrings(ADDRESS_VALUE) Then
                    ' Dob and address are the same, means we have a valid match. Lets check if the names match
                    If OriginalComparisonStringSubstrings(NAME_VALUE) Like WorkingComparisonStringSubstrings(NAME_VALUE) Then
                        ' dob, address and name matches
                    Else
                        ' Dob and address matches, name does not
                    End If
                End If
            End If
        Next
    Next

The issue is, when I get to the address value comparison, it is always failing. I have had my watch window active and the values are identical yet they do not compare. You can see my Watch window output below.

-       WorkingComparisonStringSubstrings   {Length=3}  String()
    (0) "4323"  String
    (1) "123 somewhere lane"    String
    (2) "j ii"  String
-       OriginalComparisonStringSubstrings  {Length=3}  String()
    (0) "4323"  String
    (1) "123 somewhere lane"    String
    (2) "j ii j. .johnson"  String
    OriginalComparisonStringSubstrings(DOB_VALUE) Like   WorkingComparisonStringSubstrings(DOB_VALUE)   True    Boolean
    OriginalComparisonStringSubstrings(ADDRESS_VALUE) Like WorkingComparisonStringSubstrings(ADDRESS_VALUE) False   Boolean
    OriginalComparisonStringSubstrings(NAME_VALUE) Like WorkingComparisonStringSubstrings(NAME_VALUE)   False   Boolean

The comparison for the two addresses, which would be ("123 somewhere lane" Like "123 somewhere lane") should be true but is returning false. My question is why are these values failing in comparison when they are so apparently equal? In my code I am using "Like" comparisons but I have also tried String.Compare, StrComp, .Equals, =, and every other variation of comparison. In addition, the values are always a string from the time they are inserted into the FileInfo variable to the time they are split and compared.

Anyone have an idea of why they wont compare?

vb.netstringvisual-studiostring-comparison

Answers

answered 2 years ago Krev32 #1

I cannot comment yet based on no rating, but shouldnt the dicionary be a list of strings? Such as:

 Dim OriginalFileInfo As New Dictionary(Of Integer, List(Of String))

Dim OriginalComparisonStringSubstrings As List(Of String) = oRecord.Value.ToString.ToLower.Split(":")

The when you loop through the dictionary,

If OriginalComparisonStringSubstrings(DOB_VALUE) = WorkingComparisonStringSubstrings(DOB_VALUE) Then
'You found a match. 
end if

answered 2 years ago Mythikos #2

My colleague pointed out the issue. In the previous version of the program, we were dynamically changing the value of ADDRESS_VALUE. In this most recent version is when we changed the the values to a constant integer.

It appears in this version, before we made ADDRESS_VALUE constant, it was still being assigned a new value, so it was actually comparing the NAME_VALUE rather than comparing the ADDRESS_VALUE which would in fact fail.

Thank you everyone for your comments. They were really helpful.

comments powered by Disqus