Passing an Array of integer to an Array of object in a function

hipturtle

Well-known member
Joined
Dec 1, 2011
Messages
59
Programming Experience
10+
Hello I am creating a dll with all the functions and subroutines that I use all the time and I decided to create a function that I can pass different types of array to so the function obviously (or not) would have an array of type object. Now I no you can pass any type into an object type but when I pass an integer array into an object array I get an error.

value of type '1-dimensional array of integer' cannot be converted to '1-dimensional array of object' because 'integer' is not a reference type.

Now is there any way I can do what I want to do. I have search the internet for an answer but can't find one, I would appreciate any help given on this subject.

Below is an example of my dilemma.

Shared Function CompareArray(ByVal FirstArray() As Object, ByVal SecondArray() As Object, Optional ByRef ErrorMessage As String = "") As Boolean

Dim
IntArrayOne() As Integer = {1,2,3,4,5}
Dim IntArrayTwo() As Integer = {1,2,3,5,6}
Dim IsSame As Boolean = False

IsSame = CompareArray(IntArrayOne(), IntArrayTwo())

Thanks in advance
 
This would be a much better way to implement that method:
Public Shared Function CompareArray(Of T)(array1 As T(), array2 As T()) As Boolean
    If array1.Length <> array2.Length Then
        Return False
    End If

    For i = 0 To array1.GetUpperBound(0)
        If Not array1(i).Equals(array2(i)) Then
            Return False
        End If
    Next

    Return True
End Function
 
Thankyou very much this was perfect, I have seen this type of coding before but never realy understood it, This goes alot to me understanding it now. :eagerness:
 
Back
Top