How to use ArrayList.Contains method?

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
 
P

Peter Huang

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.
 
H

Herfried K. Wagner [MVP]

* "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'.
 

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