473,385 Members | 1,856 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,385 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 1671
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
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
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...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
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
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...

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.