Hangover
Hangover

Reputation: 51

How to use arr.Contains with Object ArrayList

i have this Visual Basic .NET ArrayList

Dim first As New ArrayList()

            first.Add({100, 200})
            first.Add({500, 250})
            first.Add({700, 200})

My question is, how i can get True from this code...

first.Contains({500, 250})

Always return me False... what is the correct syntax?

Upvotes: 0

Views: 67

Answers (1)

Mirco Ellmann
Mirco Ellmann

Reputation: 968

Contains proves if the object you pass in is already in the ArrayList. It does not compare your values.

Sample:

Imports System

Public Class Sample

    Sub Method()
        Dim Obj1 As New Object()
        Dim Obj2 As New Object()
        Console.WriteLine(Obj1.Equals(Obj2)) '===> false
        Obj2 = Obj1
        Console.WriteLine(Obj1.Equals(Obj2)) '===> true
    End Sub 'Method 
End Class 'Sample

Here Obj1 und Obj2 are both of type Object but they are not equal even if their internal object state might be the same.

You might write your own custom class implementing IComparable to achieve want you want.

Upvotes: 3

Related Questions