473,398 Members | 2,212 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,398 software developers and data experts.

Quick question about Hashtable.....

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
8 1672
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
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
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
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
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
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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: joenchinghkg | last post by:
I am going to create an method to insert Items inside the hash table, However, i don't really know what I should code inside the method. Should I use push or pop because it is based on a list...
3
by: MFRASER | last post by:
Ok I have a collection that inherits from the CollectionBase. Problem is that I want a quick way to access the objects either by ID or Name. I was thinking of creating two hashtables that are...
5
by: francois | last post by:
First of all I would to to apologize for resending this post again but I feel like my last post as been spoiled Here I go for my problem: Hi, I have a webservice that I am using and I would...
33
by: Ken | last post by:
I have a C# Program where multiple threads will operate on a same Hashtable. This Hashtable is synchronized by using Hashtable.Synchronized(myHashtable) method, so no further Lock statements are...
4
by: Robin Tucker | last post by:
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...
7
by: netloss | last post by:
I have a question so basic that I suspect everyone takes it for granted, as i cannot google the answer. Anyone here can probably answer it! I want to create a bunch of variable at runtime, but I...
10
by: chrisben | last post by:
Hi, Here is the scenario. I have a list of IDs and there are multiple threads trying to add/remove/read from this list. I can do in C# 1. create Hashtable hList = Hashtable.Synchronized(new...
2
by: PAzevedo | last post by:
I have this Hashtable of Hashtables, and I'm accessing this object from multiple threads, now the Hashtable object is thread safe for reading, but not for writing, so I lock the object every time I...
28
by: Tony Johansson | last post by:
Hello! I can't figure out what point it is to use GetHashCode. I know that this GetHashCode is used for obtaining a unique integer value. Can somebody give me an example that prove the...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.