Solved

Override Hashtable Contains method

Posted on 2006-11-04
9
285 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
Best Practices: Disaster Recovery Testing

Besides backup, any IT division should have a disaster recovery plan. You will find a few tips below relating to the development of such a plan and to what issues one should pay special attention in the course of backup planning.

 
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

3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Set value of dynamically created checkboxlist 2 35
export data table to excel in vb.net 10 27
Footer for each row on Gridview 2 21
No Data for DropDown List 2 26
This article explains how to create and use a custom WaterMark textbox class.  The custom WaterMark textbox class allows you to set the WaterMark Background Color and WaterMark text at design time.   IMAGE OF WATERMARKS STEPS Create VB …
The ECB site provides FX rates for major currencies since its inception in 1999 in the form of an XML feed. The files have the following format (reducted for brevity) (CODE) There are three files available HERE (http://www.ecb.europa.eu/stats/exch…
Two types of users will appreciate AOMEI Backupper Pro: 1 - Those with PCIe drives (and haven't found cloning software that works on them). 2 - Those who want a fast clone of their boot drive (no re-boots needed) and it can clone your drive wh…
Nobody understands Phishing better than an anti-spam company. That’s why we are providing Phishing Awareness Training to our customers. According to a report by Verizon, only 3% of targeted users report malicious emails to management. With compan…

831 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