Solved

Override Hashtable Contains method

Posted on 2006-11-04
9
283 Views
Last Modified: 2008-02-01
Hi,

Could someone please help solve this problem.

I've got a hashtable, filled with custom objects. The objects have a property that's a string. I am desperetly trying to get the hashtables "Contains" method to work for this property.

I've "Implements IComparable(Of Word)"

and

    Public Overloads Function CompareTo(ByVal other As Word) As Integer _
        Implements IComparable(Of Word).CompareTo

        Return Me.Word.CompareTo(other.Word)
    End Function

    Public Overloads Overrides Function Equals(ByVal obj As Object) As Boolean
        Return TypeOf obj Is Word AndAlso CType(obj, Word).Word = Me.Word
    End Function

    Public Overloads Shared Function Equals(ByVal objA As Word, ByVal objB As Word) As Boolean
        Return objA.Word = objB.Word
    End Function

And still it doesn't work - despite two objects having the same string, the Contains returns false.

Help very much appreciated, and urgent.
0
Comment
Question by:Shemmie
  • 6
  • 3
9 Comments
 
LVL 7

Expert Comment

by:Mohamed Zedan
ID: 17875965
why don't you store that string as the key in the hashtable ?? instead of overriding or overloading any function ?
and just call the hashtable.conatins(key) ... if that is not possible please explain more your classes (hashtable ,, obj and what is the type word)
I'm awaiting your response ...
0
 

Author Comment

by:Shemmie
ID: 17876144
Sadly that's not do-able, as I'm trying to create a hashtable of Words - the Word object's got the String, Word, and an inner hashtable -


Public Class Word
    Implements IComparable(Of Word)


    Public connected As Hashtable
    Private worddd As String

    Public Property Word() As String
        Get
            Return worddd
        End Get
        Set(ByVal value As String)
            worddd = value
        End Set
    End Property

    Function Check(ByVal wordd As String, ByVal conWord As String) As Boolean

        Dim val As Integer

        If String.Equals(wordd, Me.Word) = True Then
            ' This Word object = same value as the incoming - check ConWord
            If connected.Contains(conWord) = True Then
                ' Word exists, and conWord exists - increase value
                val = connected(conWord)
                val += 1
                connected(conWord) = val
            Else
                'Word exists, conWord doesn't. Add conWord to hashtable
                connected.Add(conWord, 1)
            End If

            Return True

        Else
            'this Word object != the string suggested
            Return False
        End If

    End Function

    Public Overloads Function CompareTo(ByVal other As Word) As Integer _
        Implements IComparable(Of Word).CompareTo

        Return Me.Word.CompareTo(other.Word)
    End Function

    Public Overloads Overrides Function Equals(ByVal obj As Object) As Boolean
        Return TypeOf obj Is Word AndAlso CType(obj, Word).Word = Me.Word
    End Function

    Public Overloads Shared Function Equals(ByVal objA As Word, ByVal objB As Word) As Boolean
        Return objA.Word = objB.Word
    End Function


    Sub New(ByVal wor As String, ByVal conwor As String)
        connected = New Hashtable()
        Me.Word = wor
        Me.connected.Add(conwor, 1)
    End Sub

End Class


Many thanks
0
 
LVL 7

Expert Comment

by:Mohamed Zedan
ID: 17876152
can you list the code where you compare or call the contains method ?
0
 
LVL 7

Expert Comment

by:Mohamed Zedan
ID: 17876162
It's in the check function sorry I didn't see it the first time ... :)
0
How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

 
LVL 7

Expert Comment

by:Mohamed Zedan
ID: 17876165
or am I wrong ?
0
 

Author Comment

by:Shemmie
ID: 17876376
Well... this is the class for Word... so I create a hashtable outside of this, and am storing Word objects as the key, and recording the number of times the word's used as the value. So when I call the Contains method of that hashtable, it doesn't know what it's using within Word to do the comparison. Hence my implementing IComparable, and overloading CompareTo and Equals, in an attempt to make Word.Word the value that's used to compare one object against another. But despite the above code, the above code would fail:

dim w1 as Word = new Word("paul", "ignoreThisOne")
dim w2 as Word = new Word("paul", "stillIgnoreThisOne")
dim hash  as Hashtable = new Hashtable()
hash.Add(w1, 1)
hash.Add(w2, 1)

if hash.Contains("paul" then...

That would fail. I need it to pass as true.
0
 
LVL 7

Accepted Solution

by:
Mohamed Zedan earned 500 total points
ID: 17876396
I see .... your best bet is implementing your own hash table ....

    Public Class WordsHashTable
        Inherits Hashtable

        Public Overloads Function Contains(ByVal Value As String) As Boolean
            Dim Enumer As IEnumerator = MyBase.Keys.GetEnumerator
            While Enumer.MoveNext
                Dim w As Word = Enumer.Current
                If w.Word = Value Then Return True
            End While
            Return False
        End Function
    End Class

this should do the trick just do this after inserting the code above

dim w1 as Word = new Word("paul", "ignoreThisOne")
dim w2 as Word = new Word("paul", "stillIgnoreThisOne")
dim hash  as WordsHashTable = new WordsHashTable()
hash.Add(w1, 1)
hash.Add(w2, 1)

if hash.Contains("paul") then ....
this should do the trick for you ....

inform me of you progress
0
 

Author Comment

by:Shemmie
ID: 17876531
Urgh, inherit hashtable, and overload. Of course! Many, many thanks!
0
 
LVL 7

Expert Comment

by:Mohamed Zedan
ID: 17876541
you're welcome ... :)
0

Featured Post

Free Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

I think the Typed DataTable and Typed DataSet are very good options when working with data, but I don't like auto-generated code. First, I create an Abstract Class for my DataTables Common Code.  This class Inherits from DataTable. Also, it can …
Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
This video discusses moving either the default database or any database to a new volume.
This video demonstrates how to create an example email signature rule for a department in a company using CodeTwo Exchange Rules. The signature will be inserted beneath users' latest emails in conversations and will be displayed in users' Sent Items…

747 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now