By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
445,885 Members | 1,499 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 445,885 IT Pros & Developers. It's quick & easy.

Quick question about Hashtable.....

P: n/a
When I create a hashtable hashing on Object-->Item, can I mix "string" and
"integer" as the key types? I have a single thumbnail cache for a database
with (hashed on key) and a file view (hashed on string). So, for example,
when I want to know if the file "xyz.abc" is in the cache, I can write
myTable.ContainsKey ( "xyz.abc" ) or if a given DB key is in the hash I can
write myTable.ContainsKey ( 123 ). Or do the keys all have to be of the
same type?

Also, any disadvantages of using this kind of method?
Nov 20 '05 #1
Share this Question
Share on Google+
8 Replies


P: n/a
Ok, I satisfied myself that its ok to have strings and integers in the same
hash (not sure how this affects things like performance however):

Dim theintKey As Integer = 1
Dim thestrKey As String = "123"
Dim theString As String = "Value"
Dim theValue As Integer = 123
Dim theHash As New Hashtable

theHash.Add(thestrKey, theString)
theHash.Add(theintKey, theValue)

Debug.WriteLine(CType(theHash(theintKey), Integer))
Debug.WriteLine(CType(theHash(thestrKey), String))

"Robin Tucker" <id*************************@reallyidont.com> wrote in
message news:c4*******************@news.demon.co.uk...
When I create a hashtable hashing on Object-->Item, can I mix "string" and
"integer" as the key types? I have a single thumbnail cache for a database with (hashed on key) and a file view (hashed on string). So, for example,
when I want to know if the file "xyz.abc" is in the cache, I can write
myTable.ContainsKey ( "xyz.abc" ) or if a given DB key is in the hash I can write myTable.ContainsKey ( 123 ). Or do the keys all have to be of the
same type?

Also, any disadvantages of using this kind of method?

Nov 20 '05 #2

P: n/a
Ok, I satisfied myself that its ok to have strings and integers in the same
hash (not sure how this affects things like performance however):

Dim theintKey As Integer = 1
Dim thestrKey As String = "123"
Dim theString As String = "Value"
Dim theValue As Integer = 123
Dim theHash As New Hashtable

theHash.Add(thestrKey, theString)
theHash.Add(theintKey, theValue)

Debug.WriteLine(CType(theHash(theintKey), Integer))
Debug.WriteLine(CType(theHash(thestrKey), String))

"Robin Tucker" <id*************************@reallyidont.com> wrote in
message news:c4*******************@news.demon.co.uk...
When I create a hashtable hashing on Object-->Item, can I mix "string" and
"integer" as the key types? I have a single thumbnail cache for a database with (hashed on key) and a file view (hashed on string). So, for example,
when I want to know if the file "xyz.abc" is in the cache, I can write
myTable.ContainsKey ( "xyz.abc" ) or if a given DB key is in the hash I can write myTable.ContainsKey ( 123 ). Or do the keys all have to be of the
same type?

Also, any disadvantages of using this kind of method?

Nov 20 '05 #3

P: n/a
Robin,
Although you can use both Integer & String as keys in the same HashTable is
it the "correct" thing to do?

Also are you storing two disparate values by two disparate keys? It appears
that you have intKey storing strValue, while strKey is storing intValue!

I would find it more "correct" to have two HashTables, one keyed by Integer
& one keyed by String, where both store the same value, or each store their
own value. These two HashTables would be encapsulated within a single class.

Something like:

Public Class ThumbnailCache

Private m_ids As HashTable
Private m_names As HashTable

Public Sub Add(id As Integer, name As String, value As Object)
m_ids.Add(id, value)
m_names.Add(name, value)
End Sub

Public Function Contains(id As Integer) as Boolean
Return m_ids.Contains(id)
End Function

Public Function Contains(name As String) As Boolean
Return m_names.Contains(name)
End Function

Default Public Readonly Property Item(id As Integer) as Object
Get
Return m_ids(id)
End Get
End Property

Default Public Readonly Property Item(name As String) as Object
Get
Return m_names(name)
End Get
End Property

End Class

If the two types of key are storing their own type of values I would
considering inheriting each from DictionaryBase.

Hope this helps
Jay

"Robin Tucker" <id*************************@reallyidont.com> wrote in
message news:c4*******************@news.demon.co.uk...
Ok, I satisfied myself that its ok to have strings and integers in the same hash (not sure how this affects things like performance however):

Dim theintKey As Integer = 1
Dim thestrKey As String = "123"
Dim theString As String = "Value"
Dim theValue As Integer = 123
Dim theHash As New Hashtable

theHash.Add(thestrKey, theString)
theHash.Add(theintKey, theValue)

Debug.WriteLine(CType(theHash(theintKey), Integer))
Debug.WriteLine(CType(theHash(thestrKey), String))

"Robin Tucker" <id*************************@reallyidont.com> wrote in
message news:c4*******************@news.demon.co.uk...
When I create a hashtable hashing on Object-->Item, can I mix "string" and "integer" as the key types? I have a single thumbnail cache for a

database
with (hashed on key) and a file view (hashed on string). So, for example, when I want to know if the file "xyz.abc" is in the cache, I can write
myTable.ContainsKey ( "xyz.abc" ) or if a given DB key is in the hash I

can
write myTable.ContainsKey ( 123 ). Or do the keys all have to be of the
same type?

Also, any disadvantages of using this kind of method?


Nov 20 '05 #4

P: n/a
Robin,
Although you can use both Integer & String as keys in the same HashTable is
it the "correct" thing to do?

Also are you storing two disparate values by two disparate keys? It appears
that you have intKey storing strValue, while strKey is storing intValue!

I would find it more "correct" to have two HashTables, one keyed by Integer
& one keyed by String, where both store the same value, or each store their
own value. These two HashTables would be encapsulated within a single class.

Something like:

Public Class ThumbnailCache

Private m_ids As HashTable
Private m_names As HashTable

Public Sub Add(id As Integer, name As String, value As Object)
m_ids.Add(id, value)
m_names.Add(name, value)
End Sub

Public Function Contains(id As Integer) as Boolean
Return m_ids.Contains(id)
End Function

Public Function Contains(name As String) As Boolean
Return m_names.Contains(name)
End Function

Default Public Readonly Property Item(id As Integer) as Object
Get
Return m_ids(id)
End Get
End Property

Default Public Readonly Property Item(name As String) as Object
Get
Return m_names(name)
End Get
End Property

End Class

If the two types of key are storing their own type of values I would
considering inheriting each from DictionaryBase.

Hope this helps
Jay

"Robin Tucker" <id*************************@reallyidont.com> wrote in
message news:c4*******************@news.demon.co.uk...
Ok, I satisfied myself that its ok to have strings and integers in the same hash (not sure how this affects things like performance however):

Dim theintKey As Integer = 1
Dim thestrKey As String = "123"
Dim theString As String = "Value"
Dim theValue As Integer = 123
Dim theHash As New Hashtable

theHash.Add(thestrKey, theString)
theHash.Add(theintKey, theValue)

Debug.WriteLine(CType(theHash(theintKey), Integer))
Debug.WriteLine(CType(theHash(thestrKey), String))

"Robin Tucker" <id*************************@reallyidont.com> wrote in
message news:c4*******************@news.demon.co.uk...
When I create a hashtable hashing on Object-->Item, can I mix "string" and "integer" as the key types? I have a single thumbnail cache for a

database
with (hashed on key) and a file view (hashed on string). So, for example, when I want to know if the file "xyz.abc" is in the cache, I can write
myTable.ContainsKey ( "xyz.abc" ) or if a given DB key is in the hash I

can
write myTable.ContainsKey ( 123 ). Or do the keys all have to be of the
same type?

Also, any disadvantages of using this kind of method?


Nov 20 '05 #5

P: n/a
Its true in my example, but in my program I have a single NodeBinary class;
one view can load them into the cache from disk and the other from the
database. Therefore, I need to hash the NodeBinary objects with either a
string path or an integer key. I got it to work though, but I just felt
that performance wise, its never a good idea to make all of your variables
"object" and let the system work out their types. At least, it isn't so bad
it can't be used.

"Jay B. Harlow [MVP - Outlook]" <Ja************@msn.com> wrote in message
news:ey**************@TK2MSFTNGP10.phx.gbl...
Robin,
Although you can use both Integer & String as keys in the same HashTable is it the "correct" thing to do?

Also are you storing two disparate values by two disparate keys? It appears that you have intKey storing strValue, while strKey is storing intValue!

I would find it more "correct" to have two HashTables, one keyed by Integer & one keyed by String, where both store the same value, or each store their own value. These two HashTables would be encapsulated within a single class.
Something like:

Public Class ThumbnailCache

Private m_ids As HashTable
Private m_names As HashTable

Public Sub Add(id As Integer, name As String, value As Object)
m_ids.Add(id, value)
m_names.Add(name, value)
End Sub

Public Function Contains(id As Integer) as Boolean
Return m_ids.Contains(id)
End Function

Public Function Contains(name As String) As Boolean
Return m_names.Contains(name)
End Function

Default Public Readonly Property Item(id As Integer) as Object
Get
Return m_ids(id)
End Get
End Property

Default Public Readonly Property Item(name As String) as Object
Get
Return m_names(name)
End Get
End Property

End Class

If the two types of key are storing their own type of values I would
considering inheriting each from DictionaryBase.

Hope this helps
Jay

"Robin Tucker" <id*************************@reallyidont.com> wrote in
message news:c4*******************@news.demon.co.uk...
Ok, I satisfied myself that its ok to have strings and integers in the

same
hash (not sure how this affects things like performance however):

Dim theintKey As Integer = 1
Dim thestrKey As String = "123"
Dim theString As String = "Value"
Dim theValue As Integer = 123
Dim theHash As New Hashtable

theHash.Add(thestrKey, theString)
theHash.Add(theintKey, theValue)

Debug.WriteLine(CType(theHash(theintKey), Integer))
Debug.WriteLine(CType(theHash(thestrKey), String))

"Robin Tucker" <id*************************@reallyidont.com> wrote in
message news:c4*******************@news.demon.co.uk...
When I create a hashtable hashing on Object-->Item, can I mix "string" and "integer" as the key types? I have a single thumbnail cache for a

database
with (hashed on key) and a file view (hashed on string). So, for example, when I want to know if the file "xyz.abc" is in the cache, I can write
myTable.ContainsKey ( "xyz.abc" ) or if a given DB key is in the hash I
can
write myTable.ContainsKey ( 123 ). Or do the keys all have to be of

the same type?

Also, any disadvantages of using this kind of method?



Nov 20 '05 #6

P: n/a
Its true in my example, but in my program I have a single NodeBinary class;
one view can load them into the cache from disk and the other from the
database. Therefore, I need to hash the NodeBinary objects with either a
string path or an integer key. I got it to work though, but I just felt
that performance wise, its never a good idea to make all of your variables
"object" and let the system work out their types. At least, it isn't so bad
it can't be used.

"Jay B. Harlow [MVP - Outlook]" <Ja************@msn.com> wrote in message
news:ey**************@TK2MSFTNGP10.phx.gbl...
Robin,
Although you can use both Integer & String as keys in the same HashTable is it the "correct" thing to do?

Also are you storing two disparate values by two disparate keys? It appears that you have intKey storing strValue, while strKey is storing intValue!

I would find it more "correct" to have two HashTables, one keyed by Integer & one keyed by String, where both store the same value, or each store their own value. These two HashTables would be encapsulated within a single class.
Something like:

Public Class ThumbnailCache

Private m_ids As HashTable
Private m_names As HashTable

Public Sub Add(id As Integer, name As String, value As Object)
m_ids.Add(id, value)
m_names.Add(name, value)
End Sub

Public Function Contains(id As Integer) as Boolean
Return m_ids.Contains(id)
End Function

Public Function Contains(name As String) As Boolean
Return m_names.Contains(name)
End Function

Default Public Readonly Property Item(id As Integer) as Object
Get
Return m_ids(id)
End Get
End Property

Default Public Readonly Property Item(name As String) as Object
Get
Return m_names(name)
End Get
End Property

End Class

If the two types of key are storing their own type of values I would
considering inheriting each from DictionaryBase.

Hope this helps
Jay

"Robin Tucker" <id*************************@reallyidont.com> wrote in
message news:c4*******************@news.demon.co.uk...
Ok, I satisfied myself that its ok to have strings and integers in the

same
hash (not sure how this affects things like performance however):

Dim theintKey As Integer = 1
Dim thestrKey As String = "123"
Dim theString As String = "Value"
Dim theValue As Integer = 123
Dim theHash As New Hashtable

theHash.Add(thestrKey, theString)
theHash.Add(theintKey, theValue)

Debug.WriteLine(CType(theHash(theintKey), Integer))
Debug.WriteLine(CType(theHash(thestrKey), String))

"Robin Tucker" <id*************************@reallyidont.com> wrote in
message news:c4*******************@news.demon.co.uk...
When I create a hashtable hashing on Object-->Item, can I mix "string" and "integer" as the key types? I have a single thumbnail cache for a

database
with (hashed on key) and a file view (hashed on string). So, for example, when I want to know if the file "xyz.abc" is in the cache, I can write
myTable.ContainsKey ( "xyz.abc" ) or if a given DB key is in the hash I
can
write myTable.ContainsKey ( 123 ). Or do the keys all have to be of

the same type?

Also, any disadvantages of using this kind of method?



Nov 20 '05 #7

P: n/a
Robin,
Just because you got storing both kinds of keys to work in a single
HashTable I really would not rely on it.
What happens when you attempt to iterate over all the objects in the
HashTable (HashTable.GetEnumerator)? You see duplicates.

What happens when you attempt to iterate over all the keys in the HashTable
(HashTable.Keys)? You see two distinct types (some string & some integer).

What happens when you attempt to iterate over all the values in the
HashTable (HashTable.Values)? You see duplicates.
Remember that a HashTable uses both Object.GetHashCode & Object.Equals, you
may have an Integer.GetHashCode that matchs the String.GetHashCode that may
actual cause problems for Integer.Equals(String), which luckily is defined
not to cause an exception.

Again I would recommend two HashTables as it is the "Correct" thing to do.

Hope this helps
Jay

"Robin Tucker" <id*************************@reallyidont.com> wrote in
message news:c4*******************@news.demon.co.uk...
Its true in my example, but in my program I have a single NodeBinary class; one view can load them into the cache from disk and the other from the
database. Therefore, I need to hash the NodeBinary objects with either a
string path or an integer key. I got it to work though, but I just felt
that performance wise, its never a good idea to make all of your variables
"object" and let the system work out their types. At least, it isn't so bad it can't be used.

"Jay B. Harlow [MVP - Outlook]" <Ja************@msn.com> wrote in message
news:ey**************@TK2MSFTNGP10.phx.gbl...
Robin,
Although you can use both Integer & String as keys in the same HashTable is
it the "correct" thing to do?

Also are you storing two disparate values by two disparate keys? It

appears
that you have intKey storing strValue, while strKey is storing intValue!

I would find it more "correct" to have two HashTables, one keyed by

Integer
& one keyed by String, where both store the same value, or each store

their
own value. These two HashTables would be encapsulated within a single

class.

Something like:

Public Class ThumbnailCache

Private m_ids As HashTable
Private m_names As HashTable

Public Sub Add(id As Integer, name As String, value As Object)
m_ids.Add(id, value)
m_names.Add(name, value)
End Sub

Public Function Contains(id As Integer) as Boolean
Return m_ids.Contains(id)
End Function

Public Function Contains(name As String) As Boolean
Return m_names.Contains(name)
End Function

Default Public Readonly Property Item(id As Integer) as Object
Get
Return m_ids(id)
End Get
End Property

Default Public Readonly Property Item(name As String) as Object
Get
Return m_names(name)
End Get
End Property

End Class

If the two types of key are storing their own type of values I would
considering inheriting each from DictionaryBase.

Hope this helps
Jay

"Robin Tucker" <id*************************@reallyidont.com> wrote in
message news:c4*******************@news.demon.co.uk...
Ok, I satisfied myself that its ok to have strings and integers in the

same
hash (not sure how this affects things like performance however):

Dim theintKey As Integer = 1
Dim thestrKey As String = "123"
Dim theString As String = "Value"
Dim theValue As Integer = 123
Dim theHash As New Hashtable

theHash.Add(thestrKey, theString)
theHash.Add(theintKey, theValue)

Debug.WriteLine(CType(theHash(theintKey), Integer))
Debug.WriteLine(CType(theHash(thestrKey), String))

"Robin Tucker" <id*************************@reallyidont.com> wrote in
message news:c4*******************@news.demon.co.uk...
> When I create a hashtable hashing on Object-->Item, can I mix "string"
and
> "integer" as the key types? I have a single thumbnail cache for a
database
> with (hashed on key) and a file view (hashed on string). So, for

example,
> when I want to know if the file "xyz.abc" is in the cache, I can
write > myTable.ContainsKey ( "xyz.abc" ) or if a given DB key is in the

hash I can
> write myTable.ContainsKey ( 123 ). Or do the keys all have to be of the > same type?
>
> Also, any disadvantages of using this kind of method?
>
>



Nov 20 '05 #8

P: n/a
Robin,
Just because you got storing both kinds of keys to work in a single
HashTable I really would not rely on it.
What happens when you attempt to iterate over all the objects in the
HashTable (HashTable.GetEnumerator)? You see duplicates.

What happens when you attempt to iterate over all the keys in the HashTable
(HashTable.Keys)? You see two distinct types (some string & some integer).

What happens when you attempt to iterate over all the values in the
HashTable (HashTable.Values)? You see duplicates.
Remember that a HashTable uses both Object.GetHashCode & Object.Equals, you
may have an Integer.GetHashCode that matchs the String.GetHashCode that may
actual cause problems for Integer.Equals(String), which luckily is defined
not to cause an exception.

Again I would recommend two HashTables as it is the "Correct" thing to do.

Hope this helps
Jay

"Robin Tucker" <id*************************@reallyidont.com> wrote in
message news:c4*******************@news.demon.co.uk...
Its true in my example, but in my program I have a single NodeBinary class; one view can load them into the cache from disk and the other from the
database. Therefore, I need to hash the NodeBinary objects with either a
string path or an integer key. I got it to work though, but I just felt
that performance wise, its never a good idea to make all of your variables
"object" and let the system work out their types. At least, it isn't so bad it can't be used.

"Jay B. Harlow [MVP - Outlook]" <Ja************@msn.com> wrote in message
news:ey**************@TK2MSFTNGP10.phx.gbl...
Robin,
Although you can use both Integer & String as keys in the same HashTable is
it the "correct" thing to do?

Also are you storing two disparate values by two disparate keys? It

appears
that you have intKey storing strValue, while strKey is storing intValue!

I would find it more "correct" to have two HashTables, one keyed by

Integer
& one keyed by String, where both store the same value, or each store

their
own value. These two HashTables would be encapsulated within a single

class.

Something like:

Public Class ThumbnailCache

Private m_ids As HashTable
Private m_names As HashTable

Public Sub Add(id As Integer, name As String, value As Object)
m_ids.Add(id, value)
m_names.Add(name, value)
End Sub

Public Function Contains(id As Integer) as Boolean
Return m_ids.Contains(id)
End Function

Public Function Contains(name As String) As Boolean
Return m_names.Contains(name)
End Function

Default Public Readonly Property Item(id As Integer) as Object
Get
Return m_ids(id)
End Get
End Property

Default Public Readonly Property Item(name As String) as Object
Get
Return m_names(name)
End Get
End Property

End Class

If the two types of key are storing their own type of values I would
considering inheriting each from DictionaryBase.

Hope this helps
Jay

"Robin Tucker" <id*************************@reallyidont.com> wrote in
message news:c4*******************@news.demon.co.uk...
Ok, I satisfied myself that its ok to have strings and integers in the

same
hash (not sure how this affects things like performance however):

Dim theintKey As Integer = 1
Dim thestrKey As String = "123"
Dim theString As String = "Value"
Dim theValue As Integer = 123
Dim theHash As New Hashtable

theHash.Add(thestrKey, theString)
theHash.Add(theintKey, theValue)

Debug.WriteLine(CType(theHash(theintKey), Integer))
Debug.WriteLine(CType(theHash(thestrKey), String))

"Robin Tucker" <id*************************@reallyidont.com> wrote in
message news:c4*******************@news.demon.co.uk...
> When I create a hashtable hashing on Object-->Item, can I mix "string"
and
> "integer" as the key types? I have a single thumbnail cache for a
database
> with (hashed on key) and a file view (hashed on string). So, for

example,
> when I want to know if the file "xyz.abc" is in the cache, I can
write > myTable.ContainsKey ( "xyz.abc" ) or if a given DB key is in the

hash I can
> write myTable.ContainsKey ( 123 ). Or do the keys all have to be of the > same type?
>
> Also, any disadvantages of using this kind of method?
>
>



Nov 20 '05 #9

This discussion thread is closed

Replies have been disabled for this discussion.