Solved

I need help comparing two arrays in VB.Net 3.5

Posted on 2011-05-02
434 Views
Last Modified: 2012-08-14
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,0) = Sean Adams
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,0) = Sean Adams
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,0) = Sean Adams
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,0) = Sean Adams
newArray(1,1) = Match
newArray(2,0) = Tracey Schultz
newArray(2,1) = New

How can I do this VB.Net?

Thanks in advance,
mrotor
0
Question by:mainrotor
5 Comments

LVL 17

Accepted Solution

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

LVL 74

Expert Comment

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

LVL 74

Expert Comment

Disregard the above.
0

LVL 74

Assisted Solution

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

LVL 24

Assisted Solution

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

Write Comment

Please enter a first name

Please enter a last name

We will never share this with anyone.

Featured Post

In my previous article (http://www.experts-exchange.com/Programming/Languages/.NET/.NET_Framework_3.x/A_4362-Serialization-in-NET-1.html) we saw the basics of serialization and how types/objects can be serialized to Binary format. In this blog we wi…
For those of you who don't follow the news, or just happen to live under rocks, Microsoft Research released a beta SDK (http://www.microsoft.com/en-us/download/details.aspx?id=27876) for the Xbox 360 Kinect. If you don't know what a Kinect is (http:…
Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…
Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.

759 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!