?
Solved

Override Hashtable Contains method

Posted on 2006-11-04
9
Medium Priority
?
292 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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 2000 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

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

This tutorial demonstrates one way to create an application that runs without any Forms but still has a GUI presence via an Icon in the System Tray. The magic lies in Inheriting from the ApplicationContext Class and passing that to Application.Ru…
Article by: Kraeven
Introduction Remote Share is a simple remote sharing tool, enabling you to see, add and remove remote or local shares. The application is written in VB.NET targeting the .NET framework 2.0. The source code and the compiled programs have been in…
In this video you will find out how to export Office 365 mailboxes using the built in eDiscovery tool. Bear in mind that although this method might be useful in some cases, using PST files as Office 365 backup is troublesome in a long run (more on t…
In this video, Percona Solution Engineer Dimitri Vanoverbeke discusses why you want to use at least three nodes in a database cluster. To discuss how Percona Consulting can help with your design and architecture needs for your database and infras…
Suggested Courses
Course of the Month9 days, 19 hours left to enroll

762 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