Proper way to use .contains and .find methods with List(of object)
-
Could someone explain how to properly use the .contains and .find methods on generic list? For example, I created a list(of reportparameter) below and fill it with some dummy test values. I can't get either the .contains or find methods to work.
Dim params As New List(Of Microsoft.Reporting.WinForms.ReportParameter)
params.Add(New Microsoft.Reporting.WinForms.ReportParameter("Param1", "ParamValue1", False))
params.Add(New Microsoft.Reporting.WinForms.ReportParameter("Param2", "ParamValue2", False))
params.Add(New Microsoft.Reporting.WinForms.ReportParameter("Param3", "ParamValue3", False))'Compiles, but never returns true. Is it correct to create a new param to execute .contains?
If params.Contains(New Microsoft.Reporting.WinForms.ReportParameter("Param1")) Then
Debug.Print("")
End If'This doesn't compile, not sure of the correct way to perform a find
If params.Find(New Microsoft.Reporting.WinForms.ReportParameter("Param1")) Then
Debug.Print("")
End IfSorry if this is a dumb question - having a blonde moment today.
"There's no such thing as a stupid question, only stupid people." - Mr. Garrison
-
Could someone explain how to properly use the .contains and .find methods on generic list? For example, I created a list(of reportparameter) below and fill it with some dummy test values. I can't get either the .contains or find methods to work.
Dim params As New List(Of Microsoft.Reporting.WinForms.ReportParameter)
params.Add(New Microsoft.Reporting.WinForms.ReportParameter("Param1", "ParamValue1", False))
params.Add(New Microsoft.Reporting.WinForms.ReportParameter("Param2", "ParamValue2", False))
params.Add(New Microsoft.Reporting.WinForms.ReportParameter("Param3", "ParamValue3", False))'Compiles, but never returns true. Is it correct to create a new param to execute .contains?
If params.Contains(New Microsoft.Reporting.WinForms.ReportParameter("Param1")) Then
Debug.Print("")
End If'This doesn't compile, not sure of the correct way to perform a find
If params.Find(New Microsoft.Reporting.WinForms.ReportParameter("Param1")) Then
Debug.Print("")
End IfSorry if this is a dumb question - having a blonde moment today.
"There's no such thing as a stupid question, only stupid people." - Mr. Garrison
Your problem is that you have to use the same object reference, but in your example you are creating two different objects. The way you are doing it can only work if the Equal method is so defined that it compares the components the class with one another. Best Regards, -MRB
-
Could someone explain how to properly use the .contains and .find methods on generic list? For example, I created a list(of reportparameter) below and fill it with some dummy test values. I can't get either the .contains or find methods to work.
Dim params As New List(Of Microsoft.Reporting.WinForms.ReportParameter)
params.Add(New Microsoft.Reporting.WinForms.ReportParameter("Param1", "ParamValue1", False))
params.Add(New Microsoft.Reporting.WinForms.ReportParameter("Param2", "ParamValue2", False))
params.Add(New Microsoft.Reporting.WinForms.ReportParameter("Param3", "ParamValue3", False))'Compiles, but never returns true. Is it correct to create a new param to execute .contains?
If params.Contains(New Microsoft.Reporting.WinForms.ReportParameter("Param1")) Then
Debug.Print("")
End If'This doesn't compile, not sure of the correct way to perform a find
If params.Find(New Microsoft.Reporting.WinForms.ReportParameter("Param1")) Then
Debug.Print("")
End IfSorry if this is a dumb question - having a blonde moment today.
"There's no such thing as a stupid question, only stupid people." - Mr. Garrison
I don't think ReportParameter is IEquatable, so you must use your own predicate to find the item (see below code). Also, even if it were IEquatable, your code uses two different constructors (a 3-parameter constructor and a 1-parameter constructor), which would set different variables in the object, so they wouldn't be equal anyway.
Dim params As New List(Of ReportParameter)
params.Add(New ReportParameter("Param1", "Value1", False))
Dim foundParam As ReportParameter =
params.Find(New Predicate(Of ReportParameter)(Function(rp As ReportParameter) rp.Name = "Param1"))
If foundParam Is Nothing Then
Debug.Print("Doesn't contain.")
Else
Debug.Print("Does contain.")
End If[
S<T>::f(U) // Out of line.
](http://msdn.microsoft.com/en-us/library/8yk3t00s(v=vs.71).aspx)
modified on Friday, May 6, 2011 11:30 AM
-
I don't think ReportParameter is IEquatable, so you must use your own predicate to find the item (see below code). Also, even if it were IEquatable, your code uses two different constructors (a 3-parameter constructor and a 1-parameter constructor), which would set different variables in the object, so they wouldn't be equal anyway.
Dim params As New List(Of ReportParameter)
params.Add(New ReportParameter("Param1", "Value1", False))
Dim foundParam As ReportParameter =
params.Find(New Predicate(Of ReportParameter)(Function(rp As ReportParameter) rp.Name = "Param1"))
If foundParam Is Nothing Then
Debug.Print("Doesn't contain.")
Else
Debug.Print("Does contain.")
End If[
S<T>::f(U) // Out of line.
](http://msdn.microsoft.com/en-us/library/8yk3t00s(v=vs.71).aspx)
modified on Friday, May 6, 2011 11:30 AM
-
I don't think ReportParameter is IEquatable, so you must use your own predicate to find the item (see below code). Also, even if it were IEquatable, your code uses two different constructors (a 3-parameter constructor and a 1-parameter constructor), which would set different variables in the object, so they wouldn't be equal anyway.
Dim params As New List(Of ReportParameter)
params.Add(New ReportParameter("Param1", "Value1", False))
Dim foundParam As ReportParameter =
params.Find(New Predicate(Of ReportParameter)(Function(rp As ReportParameter) rp.Name = "Param1"))
If foundParam Is Nothing Then
Debug.Print("Doesn't contain.")
Else
Debug.Print("Does contain.")
End If[
S<T>::f(U) // Out of line.
](http://msdn.microsoft.com/en-us/library/8yk3t00s(v=vs.71).aspx)
modified on Friday, May 6, 2011 11:30 AM