Solved

Override Hashtable Contains method

Posted on 2006-11-04
9
286 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
Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
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
 
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 Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Well, all of us have seen the multiple EXCEL.EXE's in task manager that won't die even if you call the .close, .dispose methods. Try this method to kill any excels in memory. You can copy the kill function to create a check function and replace the …
Since .Net 2.0, Visual Basic has made it easy to create a splash screen and set it via the "Splash Screen" drop down in the Project Properties.  A splash screen set in this manner is automatically created, displayed and closed by the framework itsel…
Although Jacob Bernoulli (1654-1705) has been credited as the creator of "Binomial Distribution Table", Gottfried Leibniz (1646-1716) did his dissertation on the subject in 1666; Leibniz you may recall is the co-inventor of "Calculus" and beat Isaac…

808 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