• Status: Solved
• Priority: Medium
• Security: Public
• Views: 460

# I need help comparing two arrays in VB.Net 3.5

Hi Experts,
Hi Experts,
I have a VB.Net application where I need to compare the values of two arrays.  If a match is found it needs to update the status column/dimmension in both arrays to Match.  Say I have the following two arrays:

oldArray(2,1) and newArray(2,1) with the following values:

oldArray(0,0) = Amy Rogers
oldArray(0,1) = Old
oldArray(1,1) = Old
oldArray(2,0) = Richard Smith
oldArray(2,1) = Old

AND

newArray(0,0) = Robert Berger
newArray(0,1) = New
newArray(1,1) = New
newArray(2,0) = Tracey Schultz
newArray(2,1) = New

After the Arrays are compared the arrays will look like this, notice that the status in both arrays has been updated to Match for Sean Adams:

oldArray(0,0) = Amy Rogers
oldArray(0,1) = Old
oldArray(1,1) = Match
oldArray(2,0) = Richard Smith
oldArray(2,1) = Old

AND

newArray(0,0) = Robert Berger
newArray(0,1) = New
newArray(1,1) = Match
newArray(2,0) = Tracey Schultz
newArray(2,1) = New

How can I do this VB.Net?

mrotor
0
mainrotor
• 3
3 Solutions

Commented:
I am not very likeable to arrays (and multidimensionals even worse!) but can suggesta solution using a dictionary (which is generally the same thing, I think ...)
Here we go.

If you only require to update the new array, this will work wonders for you
``````        Dim OldARR As New Dictionary(Of String, String) From {{"Amy Rogers", "OLD"}, {"Sean Adams", "OLD"}, {"Richard Smith", "OLD"}}
Dim NEWARR As New Dictionary(Of String, String) From {{"Robert Berger", "NEW"}, {"Sean Adams", "NEW"}, {"Tracey Schultz", "NEW"}}
Dim Common = OldARR.Keys.Intersect(NEWARR.Keys)
If Common.Count > 0 Then
For Each x In Common
If NEWARR.Keys.Contains(x) Then NEWARR.Item(x) = "Match"
Next
End If
``````
PS. It is .NET4.0 code to intialise the dictionaries, but you can apply .NET3.5 syntax and the result will be the same.
0

Commented:
Here's my offering:
``````Sub MarkMatches(ByRef oldArray(,) As String, ByRef newArray(,) As String)
For i As Integer = 0 To oldArray.Length - 1
If oldArray(i, 0) = newArray(i, 0) Then
oldArray(i, 1) = "Match"
End If
Next
End Sub
``````
0

Commented:
Disregard the above.
0

Commented:
This is what the above should have been:
``````Sub MarkMatches(ByRef oldArray(,) As String, ByRef newArray(,) As String)
For i As Integer = 0 To oldArray.GetUpperBound(0) - 1
If oldArray(i, 0) = newArray(i, 0) Then
oldArray(i, 1) = "Match"
End If
Next
End Sub
``````
0

Commented:
I'd add an extra check to what kaufmed proposed. If oldArray.Length <> newArray.Length then you know the arrays can't possibly match.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.