Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / Languages / VB.NET / October 2004

Tip: Looking for answers? Try searching our database.

Collection & IndexOf

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Bernard Bour?e - 31 Oct 2004 12:17 GMT
I have a class named "Variable" with 2 properties "Name" and "Value" and
the following class to handle a collection of Variable:

Public Class ColVariables
Inherits CollectionBase
Default Public Property Item(ByVal index As Integer) As Variable
   Get
       Return CType(List(index), Variable)
   End Get
   Set(ByVal Value As Variable)
       List(index) = Value
   End Set
End Property
Public Function Add(ByVal value As Variable) As Integer
   Return List.Add(value)
End Function 'Add
Public Function IndexOf(ByVal value As Variable) As Integer
   Return List.IndexOf(value)
End Function 'IndexOf
...........................

My collection is build with:

Dim ColVar as New ColVariables
ColVar.Add(New Variable(Name, Value)

I'm trying to retrieve one item of the collection but the following code
always return -1

Dim var As New Variable(Name, Value)
Dim idx As Integer
idx = ColVar.IndexOf(var)
Idx is always -1

What is wrong ?

Signature

Bernard Bour?e
bernard@bouree.net

Jay B. Harlow [MVP - Outlook] - 31 Oct 2004 16:35 GMT
Bernard,
ArrayList.IndexOf (CollectionBase.List.IndexOf) uses Object.Equals to
compare two objects, so to get ColVariables.IndexOf to work you need to
override Object.Equals in your Variable class:

Something like:

   Public Class Variable

       Private ReadOnly m_name As String
       Private ReadOnly m_value As Object

       Public Sub New(ByVal name As String, ByVal value As Object)
           m_name = name
           m_value = value
       End Sub

       Public Overloads Function Equals(ByVal value As Variable) As Boolean
           Return m_name = value.m_name AndAlso
m_value.Equals(value.m_value)
       End Function

       Public Overloads Overrides Function Equals(ByVal obj As Object) As
Boolean
           If TypeOf obj Is Variable Then
               Return Equals(DirectCast(obj, Variable))
           Else
               Return False
           End If
       End Function

   End Class

You may want to have VariableCollection (your ColVariables) inherit from
DictionaryBase or NameObjectCollectionBase instead of CollectionBase, as it
appears you have a collection of named objects.

Hope this helps
Jay

>I have a class named "Variable" with 2 properties "Name" and "Value" and
> the following class to handle a collection of Variable:
[quoted text clipped - 31 lines]
>
> What is wrong ?
Bernard Bour?e - 31 Oct 2004 17:32 GMT
Jay,

Thanks a lot !!

Signature

Bernard Bour?e
bernard@bouree.net

> Bernard,
> ArrayList.IndexOf (CollectionBase.List.IndexOf) uses Object.Equals to
[quoted text clipped - 71 lines]
> >
> > What is wrong ?

Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.