Solved

Override Hashtable Contains method

Posted on 2006-11-04
9
284 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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

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…
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
This Micro Tutorial will teach you how to censor certain areas of your screen. The example in this video will show a little boy's face being blurred. This will be demonstrated using Adobe Premiere Pro CS6.
Internet Business Fax to Email Made Easy - With eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, fr…

896 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

14 Experts available now in Live!

Get 1:1 Help Now