Array compare

G

Guest

What is the best method to compare 2 array to knw if each element is equal?
Now, i have to compare the datarow.itemarray of 2 datarows wich is equals in
structure.
I tried to write this routine: (OPTION STRICT MUST BE ON!!)
Private Function ISArrayEquals(ByVal A As Array, ByVal B As Array) As
Boolean
For index As Integer = 0 To A.Length - 1
If A.GetValue(index) <> B.GetValue(index) Then '<----Here is the error!
Return False
End If
Next
End Function
But in the commented line i have to convert in the right type... but i don't
know HOW.
Obviously i assume the arrays are identical for type and dimension, i only
want to check values.

Anybody can help me in this routine or a better method ?
Thank you.
 
P

PvdG42

Riccardo said:
What is the best method to compare 2 array to knw if each element is
equal?
Now, i have to compare the datarow.itemarray of 2 datarows wich is equals
in
structure.
I tried to write this routine: (OPTION STRICT MUST BE ON!!)
Private Function ISArrayEquals(ByVal A As Array, ByVal B As Array) As
Boolean
For index As Integer = 0 To A.Length - 1
If A.GetValue(index) <> B.GetValue(index) Then '<----Here is the error!
Return False
End If
Next
End Function
But in the commented line i have to convert in the right type... but i
don't
know HOW.
Obviously i assume the arrays are identical for type and dimension, i only
want to check values.

Anybody can help me in this routine or a better method ?
Thank you.

Care to quote the error message you are getting from the compiler?
The issue that jumps out at me is your failure to return anything if you get
through the loop without finding any mismatches.
 
G

Guest

You say that you assume the arrays are the same size, but are you sure? If B
is smaller than A, you will get a runtime error. It is a trivial check
before the loop to add:

if (A.Length <> B.Length) then return false
 
G

Guest

Sorry, i wrote a bad and incomplete question!
The two arrays are identical. For your understanding, i have to call this
function passing two array from identical datarows:
ISArrayEquals(drLocal.ItemArray, drReturn.ItemArray)
This arrays contains different type of items but at the same position.
When i try to compare the values, i would have to convert (casting) the
values into the same type.
If i set Option Strict to OFF, i can do this:
If A(index) <> B(index) Then....
But with Option Strict ON i can't.
Then, i have to do this:
If CType(A.GetValue(index), WhatType?) <> CType(B.GetValue(index),
WhatType?) then...

Here is my question: How can i determine "WhatType?" (the type of the VALUE
of the item) ?
I hope that my question is clearer. Excuse my bad English!
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top