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

Advanced Hashtable ?

P: n/a
Hi,

Is there a way when using a hashtable in VB.NET to add value of a
specific type and retrieve that same value without having to cast it to
the original type?

Thanks
Marty
Nov 21 '05 #1
Share this Question
Share on Google+
12 Replies


P: n/a
Yes, you can derive from Dictionary and the override the methods you want to
use, performing the cast. This is from MSDN, and shows an example of using
a string type instead of generic object.
Imports System
Imports System.Collections

Public Class ShortStringDictionary
Inherits DictionaryBase

Default Public Property Item(key As [String]) As [String]
Get
Return CType(Dictionary(key), [String])
End Get
Set
Dictionary(key) = value
End Set
End Property

Public ReadOnly Property Keys() As ICollection
Get
Return Dictionary.Keys
End Get
End Property

Public ReadOnly Property Values() As ICollection
Get
Return Dictionary.Values
End Get
End Property

Public Sub Add(key As [String], value As [String])
Dictionary.Add(key, value)
End Sub 'Add

Public Function Contains(key As [String]) As Boolean
Return Dictionary.Contains(key)
End Function 'Contains

Public Sub Remove(key As [String])
Dictionary.Remove(key)
End Sub 'Remove

End Class

"Marty" <xm******@hotmail.com> wrote in message
news:H3jne.28711$on1.401@clgrps13...
Hi,

Is there a way when using a hashtable in VB.NET to add value of a specific
type and retrieve that same value without having to cast it to the
original type?

Thanks
Marty

Nov 21 '05 #2

P: n/a
Hi Robin,

Thanks for the information. Does MSDN has details about the performance
of using this algorithm? Because it still has to do a cast before
returning the value.

Thank you,
Marty

Robin Tucker wrote:
Yes, you can derive from Dictionary and the override the methods you want to
use, performing the cast. This is from MSDN, and shows an example of using
a string type instead of generic object.
Imports System
Imports System.Collections

Public Class ShortStringDictionary
Inherits DictionaryBase

Default Public Property Item(key As [String]) As [String]
Get
Return CType(Dictionary(key), [String])
End Get
Set
Dictionary(key) = value
End Set
End Property

Public ReadOnly Property Keys() As ICollection
Get
Return Dictionary.Keys
End Get
End Property

Public ReadOnly Property Values() As ICollection
Get
Return Dictionary.Values
End Get
End Property

Public Sub Add(key As [String], value As [String])
Dictionary.Add(key, value)
End Sub 'Add

Public Function Contains(key As [String]) As Boolean
Return Dictionary.Contains(key)
End Function 'Contains

Public Sub Remove(key As [String])
Dictionary.Remove(key)
End Sub 'Remove

End Class

"Marty" <xm******@hotmail.com> wrote in message
news:H3jne.28711$on1.401@clgrps13...
Hi,

Is there a way when using a hashtable in VB.NET to add value of a specific
type and retrieve that same value without having to cast it to the
original type?

Thanks
Marty


Nov 21 '05 #3

P: n/a
I just found the article, but there is nothing about performance
increment...

A technical question, why are they using bracket "[" and "]" to specify
types?

Thanks :)
Marty

Marty wrote:
Hi Robin,

Thanks for the information. Does MSDN has details about the performance
of using this algorithm? Because it still has to do a cast before
returning the value.

Thank you,
Marty

Robin Tucker wrote:
Yes, you can derive from Dictionary and the override the methods you
want to use, performing the cast. This is from MSDN, and shows an
example of using a string type instead of generic object.
Imports System
Imports System.Collections

Public Class ShortStringDictionary
Inherits DictionaryBase

Default Public Property Item(key As [String]) As [String]
Get
Return CType(Dictionary(key), [String])
End Get
Set
Dictionary(key) = value
End Set
End Property

Public ReadOnly Property Keys() As ICollection
Get
Return Dictionary.Keys
End Get
End Property

Public ReadOnly Property Values() As ICollection
Get
Return Dictionary.Values
End Get
End Property

Public Sub Add(key As [String], value As [String])
Dictionary.Add(key, value)
End Sub 'Add

Public Function Contains(key As [String]) As Boolean
Return Dictionary.Contains(key)
End Function 'Contains

Public Sub Remove(key As [String])
Dictionary.Remove(key)
End Sub 'Remove

End Class

"Marty" <xm******@hotmail.com> wrote in message
news:H3jne.28711$on1.401@clgrps13...
Hi,

Is there a way when using a hashtable in VB.NET to add value of a
specific type and retrieve that same value without having to cast it
to the original type?

Thanks
Marty


Nov 21 '05 #4

P: n/a
I wouldn't worry about hashtable performance. imho it's pretty quick (my
software regularly maintains a hash of integer keys to database "record"
instances of well over 100,000 without any trouble). In any case, you will
have to perform the cast at some point at present. I guess when .NET 2.0
and Visual Studio 2005 come along with generics, you will be able to use
templates to generate a class for a specific type that won't have this
overhead.
As for the [], I have no idea :/

"Marty" <xm******@hotmail.com> wrote in message
news:kzjne.38417$tt5.5550@edtnps90...
I just found the article, but there is nothing about performance
increment...

A technical question, why are they using bracket "[" and "]" to specify
types?

Thanks :)
Marty

Marty wrote:
Hi Robin,

Thanks for the information. Does MSDN has details about the performance
of using this algorithm? Because it still has to do a cast before
returning the value.

Thank you,
Marty

Robin Tucker wrote:
Yes, you can derive from Dictionary and the override the methods you
want to use, performing the cast. This is from MSDN, and shows an
example of using a string type instead of generic object.
Imports System
Imports System.Collections

Public Class ShortStringDictionary
Inherits DictionaryBase

Default Public Property Item(key As [String]) As [String]
Get
Return CType(Dictionary(key), [String])
End Get
Set
Dictionary(key) = value
End Set
End Property

Public ReadOnly Property Keys() As ICollection
Get
Return Dictionary.Keys
End Get
End Property

Public ReadOnly Property Values() As ICollection
Get
Return Dictionary.Values
End Get
End Property

Public Sub Add(key As [String], value As [String])
Dictionary.Add(key, value)
End Sub 'Add

Public Function Contains(key As [String]) As Boolean
Return Dictionary.Contains(key)
End Function 'Contains

Public Sub Remove(key As [String])
Dictionary.Remove(key)
End Sub 'Remove

End Class

"Marty" <xm******@hotmail.com> wrote in message
news:H3jne.28711$on1.401@clgrps13...

Hi,

Is there a way when using a hashtable in VB.NET to add value of a
specific type and retrieve that same value without having to cast it to
the original type?

Thanks
Marty

Nov 21 '05 #5

P: n/a
"Marty" <xm******@hotmail.com> schrieb
Thanks for the information. Does MSDN has details about the
performance of using this algorithm? Because it still has to do a
cast before returning the value.


It *must* be casted because otherwise the types don't match.

Armin
Nov 21 '05 #6

P: n/a
Someone did some "research" on this.....................

http://visualbasic.about.com/od/usin.../aa072103a.htm
"Marty" <xm******@hotmail.com> wrote in message
news:kzjne.38417$tt5.5550@edtnps90...
I just found the article, but there is nothing about performance
increment...

A technical question, why are they using bracket "[" and "]" to specify
types?

Thanks :)
Marty

Marty wrote:
Hi Robin,

Thanks for the information. Does MSDN has details about the performance
of using this algorithm? Because it still has to do a cast before
returning the value.

Thank you,
Marty

Robin Tucker wrote:
Yes, you can derive from Dictionary and the override the methods you
want to use, performing the cast. This is from MSDN, and shows an
example of using a string type instead of generic object.
Imports System
Imports System.Collections

Public Class ShortStringDictionary
Inherits DictionaryBase

Default Public Property Item(key As [String]) As [String]
Get
Return CType(Dictionary(key), [String])
End Get
Set
Dictionary(key) = value
End Set
End Property

Public ReadOnly Property Keys() As ICollection
Get
Return Dictionary.Keys
End Get
End Property

Public ReadOnly Property Values() As ICollection
Get
Return Dictionary.Values
End Get
End Property

Public Sub Add(key As [String], value As [String])
Dictionary.Add(key, value)
End Sub 'Add

Public Function Contains(key As [String]) As Boolean
Return Dictionary.Contains(key)
End Function 'Contains

Public Sub Remove(key As [String])
Dictionary.Remove(key)
End Sub 'Remove

End Class

"Marty" <xm******@hotmail.com> wrote in message
news:H3jne.28711$on1.401@clgrps13...

Hi,

Is there a way when using a hashtable in VB.NET to add value of a
specific type and retrieve that same value without having to cast it to
the original type?

Thanks
Marty

Nov 21 '05 #7

P: n/a
Thanks Armin, but I know that. My concern is that in my code I have to
access repeteadly a hashtable value, and every time to cast to the type
of my object. Casting takes time and I was looking for a way to bypass
this step by having directly the specific cast in my hashtable.

Regards,
Marty

Armin Zingler wrote:
"Marty" <xm******@hotmail.com> schrieb
Thanks for the information. Does MSDN has details about the
performance of using this algorithm? Because it still has to do a
cast before returning the value.

It *must* be casted because otherwise the types don't match.

Armin

Nov 21 '05 #8

P: n/a
This article is interesting but he stopped where it was beginning to be
more interesting, when retrieving values.
I guess when .NET 2.0
and Visual Studio 2005 come along with generics, you will be able to
use templates to generate a class for a specific type that won't have
this overhead.
yes, I look forward for that kind of functionnality.

Have a nice day :)
Marty

Robin Tucker wrote:
Someone did some "research" on this.....................

http://visualbasic.about.com/od/usin.../aa072103a.htm
"Marty" <xm******@hotmail.com> wrote in message
news:kzjne.38417$tt5.5550@edtnps90...
I just found the article, but there is nothing about performance
increment...

A technical question, why are they using bracket "[" and "]" to specify
types?

Thanks :)
Marty

Marty wrote:

Hi Robin,

Thanks for the information. Does MSDN has details about the performance
of using this algorithm? Because it still has to do a cast before
returning the value.

Thank you,
Marty

Robin Tucker wrote:
Yes, you can derive from Dictionary and the override the methods you
want to use, performing the cast. This is from MSDN, and shows an
example of using a string type instead of generic object.
Imports System
Imports System.Collections

Public Class ShortStringDictionary
Inherits DictionaryBase

Default Public Property Item(key As [String]) As [String]
Get
Return CType(Dictionary(key), [String])
End Get
Set
Dictionary(key) = value
End Set
End Property

Public ReadOnly Property Keys() As ICollection
Get
Return Dictionary.Keys
End Get
End Property

Public ReadOnly Property Values() As ICollection
Get
Return Dictionary.Values
End Get
End Property

Public Sub Add(key As [String], value As [String])
Dictionary.Add(key, value)
End Sub 'Add

Public Function Contains(key As [String]) As Boolean
Return Dictionary.Contains(key)
End Function 'Contains

Public Sub Remove(key As [String])
Dictionary.Remove(key)
End Sub 'Remove

End Class

"Marty" <xm******@hotmail.com> wrote in message
news:H3jne.28711$on1.401@clgrps13...
>Hi,
>
>Is there a way when using a hashtable in VB.NET to add value of a
>specific type and retrieve that same value without having to cast it to
>the original type?
>
>Thanks
>Marty


Nov 21 '05 #9

P: n/a
Marty,
Is there a way when using a hashtable in VB.NET to add value of a specific
type and retrieve that same value without having to cast it to the
original type?

Can you explain this a little bit more, because AFAIK you cannot cast a
value.

Cor
Nov 21 '05 #10

P: n/a
Hi Cor,

the Hashtable receive in input a key and value pair of type "Object".
If your value is an instance of your own class type, then when you
retrieve it, the hashtable return it as an object of type "Object".

If you want to use it, you will cast it with ctype(myRetrievedObject,
gettype(myClass)), isn't it?

If you know a way to avoid the cast, then I would be happy. I'm doing a
couple of test to find out where is my bottleneck.

Marty
Cor Ligthert wrote:
Marty,

Is there a way when using a hashtable in VB.NET to add value of a specific
type and retrieve that same value without having to cast it to the
original type?


Can you explain this a little bit more, because AFAIK you cannot cast a
value.

Cor

Nov 21 '05 #11

P: n/a
Marty,

Do you mean that you are putting more types of objects from different
classes in your hashtable.

Otherwise this is a normal command
dim myObject as theClassIUse = DirectCast(myhashtable.Value, theClassIUse)

However you can AFAIK never pass it in a quicker way.

Cor
Nov 21 '05 #12

P: n/a
Hi Cor,

yes, I put only one type of object in the hashtable.

I did a couple of tests and for the need I have, it is not the
bottleneck I thaught is was initially.

Marty

Cor Ligthert wrote:
Marty,

Do you mean that you are putting more types of objects from different
classes in your hashtable.

Otherwise this is a normal command
dim myObject as theClassIUse = DirectCast(myhashtable.Value, theClassIUse)

However you can AFAIK never pass it in a quicker way.

Cor

Nov 21 '05 #13

This discussion thread is closed

Replies have been disabled for this discussion.