How to use ArrayList.Contains method?

  • Thread starter Thread starter Dean Slindee
  • Start date Start date
D

Dean Slindee

I have loaded two forms into an ArrayList with the following statement:

arrForm.Add(Me) 'in each form's Load method.

Now I want to reference a form in the ArrayList. However, I must not be
using the .Contains Method properly, because the If clause is never true.

Dim frm As frmMain

If arrForm.Contains(frm) Then

Call FormDataFieldsClear(Me)

End If

Thanks,

Dean Slindee
 
Hi Dean,

You may need to use the instance of the arraylist element.

Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Load
Dim arrForm As New ArrayList
arrForm.Add(Me)
Dim frm As Form1
frm = Me
Console.WriteLine(arrForm.Contains(frm))'Return true
End Sub

ArrayList.Contains Method
This method determines equality by calling Object.Equals.
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/cpref/html/
frlrfSystemCollectionsArrayListClassContainsTopic.asp

Object.Equals Method
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/cpref/html/
frlrfsystemobjectclassequalstopic.asp

Regards,
Peter Huang
Microsoft Online Partner Support
Get Secure! www.microsoft.com/security
This posting is provided "as is" with no warranties and confers no rights.
 
* "Dean Slindee said:
I have loaded two forms into an ArrayList with the following statement:

arrForm.Add(Me) 'in each form's Load method.

Now I want to reference a form in the ArrayList. However, I must not be
using the .Contains Method properly, because the If clause is never true.

Dim frm As frmMain

If arrForm.Contains(frm) Then

Call FormDataFieldsClear(Me)

End If

The 'Contains' method will check reference equality when used with
a reference type. The code above doesn't make sense because the
variable 'frm' points to 'Nothing', and 'Me' points to something 'IsNot
Nothing'.
 
Back
Top