Re: Arraylist searching question

From: C Downey (colleenDowney65_at_homtail.com)
Date: 05/26/04


Date: Wed, 26 May 2004 09:35:18 -0700

That worked great!

Sorry - one more hopefully quick question. How could I override the sort
method so I can sort the "count" property?

I dont really understand why I would override the GetHashCode function
either - can you give me a quick explanation?

Thank you very much, you have been a great help!

Colleen

"Alex Papadimoulis" <alexp@papadimoulis.com> wrote in message
news:%23KpOCJzQEHA.3140@TK2MSFTNGP11.phx.gbl...
> Colleen,
>
> Oops! I forgot the "Overloads" keyword. Here's what your class should
have:
>
> Public Overloads Overrides Function Equals(ByVal obj As Object) As Boolean
> If TypeOf obj Is ecMatchingOcc Then _
> Return Equals(DirectCast(obj, ecMatchingOcc))
> End Function
> Public Overloads Function Equals(ByVal obj As ecMatchingOcc) As Boolean
> Return obj.OccID = _occID
> End Function
>
> Now this will work for ya :-)
>
> Note, another function you may want to override is the GetHashCode
function.
> This is a good idea when overloading the equals function. Your GetHashCode
> should probably return your OccID. This is pretty important for HashTables
> and other things that use it.
>
> -- Alex Papadimoulis
>
>
>
> "C Downey" <colleenDowney65@homtail.com> wrote in message
> news:ebivnzyQEHA.396@TK2MSFTNGP12.phx.gbl...
> > Hi Alex -
> >
> > My exact code in my class is below. I still get the error message
> "function
> > Equals shadows an overloadable member declared in the base class
> 'object'".
> > The error is given to me on the first "Equals" function. I have looked
> all
> > over the web but have found no explanation for this. If this works, I
> would
> > also like to override the sort method - this should be similar?
> >
> > Thanks!
> >
> >
> > ' this is the code within my "simple" class...it only has 2 properties
> > Public Overrides Function Equals(ByVal obj As Object) As Boolean
> > If TypeOf obj Is ecMatchingOcc Then
> > Return Equals(DirectCast(obj, ecMatchingOcc))
> > Else
> > Return False
> > End If
> > End Function
> > Public Overloads Function Equals(ByVal obj As ecMatchingOcc) As
> > Boolean
> > Return obj.OccID = _occID
> > End Function
> >
> > "Alex Papadimoulis" <alexp@papadimoulis.com> wrote in message
> > news:ufJAQMyQEHA.3616@TK2MSFTNGP11.phx.gbl...
> > > Colleen,
> > >
> > > The Arraylist will not call your Equals function, since it is not
> > > Object.Equals. What you should do is ...
> > >
> > > Public Overrides Function Equals(ByVal obj As object) As Boolean
> > > If TypeOf obj Is ecMatchingOcc Then Return Equals(
> > > DirectCast(obj,ecMatchingOcc) )
> > > Return False
> > > End Function
> > > Public Overloads Function Equals(ByVal obj As ecMatchingOcc) As
Boolean
> > > Return obj.OccID = _occID ' doesnt seem to be executed when i put
a
> > > End Function
> > >
> > > .. that way, you are ovveriding the base method (which accepts an
Object
> > as
> > > a paramter), and overloading it with your own, strongly typed method.
> > >
> > > -- Alex Papadimoulis
> > >
> > > "C Downey" <colleenDowney65@homtail.com> wrote in message
> > > news:%23SS8tBqQEHA.3052@TK2MSFTNGP12.phx.gbl...
> > > > Thanks for your reply! It makes sense and is very clever however I
am
> > > > having troubles implementing it. The code runs but the overloaded
> > method
> > > > equals always seems to return false.
> > > > When I put a breakpoint in the overloaded "Equals" the code never
> seems
> > to
> > > > get there.
> > > > Any ideas?
> > > >
> > > >
> > > > My code is :
> > > >
> > > >
> > > > While dr.Read
> > > > ecMO = New ecMatchingOcc
> > > > ecMO.OccID = dr(0)
> > > > ecMO.Matches = dr(1)
> > > > If al.Contains(ecMO) Then
> > > > idx = al.IndexOf(ecMO)
> > > > al(idx).Matches += 1
> > > > Else
> > > > al.Add(ecMO)
> > > > End If
> > > > End While
> > > >
> > > > ' -- ============= simple class
> > > > Public Class ecMatchingOcc ' simple class to store matching
> Occupational
> > > > profiles
> > > > Private _occID As Int32
> > > > Private _matches As Int32
> > > >
> > > > Public Overloads Function Equals(ByVal obj As ecMatchingOcc) As
> Boolean
> > > > Return obj.OccID = _occID ' doesnt seem to be executed when i
put
> a
> > > > breakpoint here
> > > > End Function
> > > >
> > > > Public Property OccID() As Int32
> > > > Get
> > > > Return _occID
> > > > End Get
> > > > Set(ByVal Value As Int32)
> > > > _occID = Value
> > > > End Set
> > > > End Property
> > > >
> > > > Public Property Matches() As Int32
> > > > Get
> > > > Return _matches
> > > > End Get
> > > > Set(ByVal Value As Int32)
> > > > _matches = Value
> > > > End Set
> > > > End Property
> > > > End Class
> > > >
> > > > "Alex Papadimoulis" <alexp@papadimoulis.com> wrote in message
> > > > news:OckC6ppQEHA.2468@tk2msftngp13.phx.gbl...
> > > > > Hi Colleen,
> > > > >
> > > > > The ArrayList has a Contains( item ) method that returns a boolean
> > > > > indicating whether the item is in the list. this method uses
> > > Object.Equals
> > > > > comparison, so if you want your object to be found, it will need
to
> > > > override
> > > > > that function. Here's an example:
> > > > >
> > > > > Class simpleObj
> > > > > public ID as integer;
> > > > > public Count as integer;
> > > > > public overrides function Equals( obj as simpleObj) as boolean
> > > > > return obj.ID = ID
> > > > > end function
> > > > > End Class
> > > > >
> > > > > dim myList as New ArrayList
> > > > > myList.Add(simpleObj1)
> > > > > if myList.Contains(simpleObj2) then
> > > > > idx = myList.IndexOf(simpleObj2)
> > > > > myList(idx).Count += 1
> > > > > else
> > > > > myList.Add(simpleObject2)
> > > > > end if
> > > > >
> > > > > Good Luck!
> > > > >
> > > > > -- Alex Papadimoulis
> > > > > "C Downey" <colleenDowney65@homtail.com> wrote in message
> > > > > news:u7hsh4oQEHA.1392@TK2MSFTNGP09.phx.gbl...
> > > > > > Hello:
> > > > > >
> > > > > > I have an arraylist storing some very basic objects.
> > > > > >
> > > > > > The object is very basic, it has 2 properties : ID, and COUNT
> > > > > >
> > > > > > Before I add an object to the arraylist, I want to check if an
> > object
> > > > with
> > > > > > that same ID already exists in the arraylist.
> > > > > >
> > > > > > If it does, I would like to increase the count of the matching
> > object
> > > > > inside
> > > > > > of the arraylist by 1, otherwise I want to add the new object.
> > > > > >
> > > > > > How would I go about finding out if that object is already added
> in
> > my
> > > > > > arraylist? And how would I increase the count?
> > > > > >
> > > > > > Any advice and sample code would be greatly apreciated!
> > > > > >
> > > > > >
> > > > >
> > > > >
> > > >
> > > >
> > >
> > >
> >
> >
>
>



Relevant Pages

  • Re: Arraylist searching question
    ... Public Overloads Function CompareTo(ByVal obj As Object) As Integer ... How could I override the sort ... >> Public Overloads Function EqualsAs Boolean ...
    (microsoft.public.dotnet.framework.aspnet)
  • Re: DFSORT FILSZ=U question. (fwd)
    ... OPTION USEWKDD can be used to temporarily override DYNAUTO=IGNWKDD, ... thereby allowing JCL SORTWKdd data sets to be used rather than ... What happened is that we had a sort job go down with INSUFFICIENT SORT ...
    (bit.listserv.ibm-main)
  • Re: Arriving to max field size
    ... not convert boolean to string". ... > in the override KeyPress procedure. ... > DBGrid1: TDBGrid; ...
    (borland.public.delphi.database.ado)
  • Re: How can I make a property not show in the properties window?
    ... If you want to "override" the *attributes* of a not overridable property. ... >> Public Shadows Property AutoScroll() As Boolean ... >> Public Overrides Property AutoScroll() As Boolean ...
    (microsoft.public.dotnet.languages.vb)
  • Re: Change logon style
    ... but some sort of local policy that I can override if necessary? ... A registry hack? ...
    (microsoft.public.windowsxp.general)