Advanced Hashtable ?

  • Thread starter Thread starter Marty
  • Start date Start date
M

Marty

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
 
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
 
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 said:
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

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
 
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
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 said:
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

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
 
Marty said:
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
 
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 said:
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
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 said:
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


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
 
Someone did some "research" on this.....................

http://visualbasic.about.com/od/usingvbnet/l/aa072103a.htm


Marty said:
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
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 said:
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


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
 
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
 
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 said:
Someone did some "research" on this.....................

http://visualbasic.about.com/od/usingvbnet/l/aa072103a.htm


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



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
 
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
 
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
 
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
 
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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top