Advanced Hashtable ?

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Marty

    #1

    Advanced Hashtable ?

    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
  • Robin Tucker

    #2
    Re: Advanced Hashtable ?

    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.Collecti ons

    Public Class ShortStringDict ionary
    Inherits DictionaryBase

    Default Public Property Item(key As [String]) As [String]
    Get
    Return CType(Dictionar y(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.Valu es
    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.Cont ains(key)
    End Function 'Contains

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

    End Class

    "Marty" <xmarty99@hotma il.com> wrote in message
    news:H3jne.2871 1$on1.401@clgrp s13...[color=blue]
    > 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[/color]


    Comment

    • Marty

      #3
      Re: Advanced Hashtable ?

      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:
      [color=blue]
      > 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.Collecti ons
      >
      > Public Class ShortStringDict ionary
      > Inherits DictionaryBase
      >
      > Default Public Property Item(key As [String]) As [String]
      > Get
      > Return CType(Dictionar y(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.Valu es
      > 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.Cont ains(key)
      > End Function 'Contains
      >
      > Public Sub Remove(key As [String])
      > Dictionary.Remo ve(key)
      > End Sub 'Remove
      >
      > End Class
      >
      > "Marty" <xmarty99@hotma il.com> wrote in message
      > news:H3jne.2871 1$on1.401@clgrp s13...
      >[color=green]
      >>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[/color]
      >
      >
      >[/color]

      Comment

      • Marty

        #4
        Re: Advanced Hashtable ?

        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:
        [color=blue]
        > 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:
        >[color=green]
        >> 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.Collecti ons
        >>
        >> Public Class ShortStringDict ionary
        >> Inherits DictionaryBase
        >>
        >> Default Public Property Item(key As [String]) As [String]
        >> Get
        >> Return CType(Dictionar y(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.Valu es
        >> 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.Cont ains(key)
        >> End Function 'Contains
        >>
        >> Public Sub Remove(key As [String])
        >> Dictionary.Remo ve(key)
        >> End Sub 'Remove
        >>
        >> End Class
        >>
        >> "Marty" <xmarty99@hotma il.com> wrote in message
        >> news:H3jne.2871 1$on1.401@clgrp s13...
        >>[color=darkred]
        >>> 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[/color]
        >>
        >>
        >>
        >>[/color][/color]

        Comment

        • Robin Tucker

          #5
          Re: Advanced Hashtable ?

          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" <xmarty99@hotma il.com> wrote in message
          news:kzjne.3841 7$tt5.5550@edtn ps90...[color=blue]
          >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:
          >[color=green]
          >> 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:
          >>[color=darkred]
          >>> 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.Collecti ons
          >>>
          >>> Public Class ShortStringDict ionary
          >>> Inherits DictionaryBase
          >>>
          >>> Default Public Property Item(key As [String]) As [String]
          >>> Get
          >>> Return CType(Dictionar y(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.Valu es
          >>> 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.Cont ains(key)
          >>> End Function 'Contains
          >>>
          >>> Public Sub Remove(key As [String])
          >>> Dictionary.Remo ve(key)
          >>> End Sub 'Remove
          >>>
          >>> End Class
          >>>
          >>> "Marty" <xmarty99@hotma il.com> wrote in message
          >>> news:H3jne.2871 1$on1.401@clgrp s13...
          >>>
          >>>> 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
          >>>
          >>>
          >>>
          >>>[/color][/color][/color]


          Comment

          • Armin Zingler

            #6
            Re: Advanced Hashtable ?

            "Marty" <xmarty99@hotma il.com> schrieb[color=blue]
            > 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.[/color]

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

            Armin

            Comment

            • Robin Tucker

              #7
              Re: Advanced Hashtable ?

              Someone did some "research" on this........... ..........




              "Marty" <xmarty99@hotma il.com> wrote in message
              news:kzjne.3841 7$tt5.5550@edtn ps90...[color=blue]
              >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:
              >[color=green]
              >> 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:
              >>[color=darkred]
              >>> 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.Collecti ons
              >>>
              >>> Public Class ShortStringDict ionary
              >>> Inherits DictionaryBase
              >>>
              >>> Default Public Property Item(key As [String]) As [String]
              >>> Get
              >>> Return CType(Dictionar y(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.Valu es
              >>> 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.Cont ains(key)
              >>> End Function 'Contains
              >>>
              >>> Public Sub Remove(key As [String])
              >>> Dictionary.Remo ve(key)
              >>> End Sub 'Remove
              >>>
              >>> End Class
              >>>
              >>> "Marty" <xmarty99@hotma il.com> wrote in message
              >>> news:H3jne.2871 1$on1.401@clgrp s13...
              >>>
              >>>> 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
              >>>
              >>>
              >>>
              >>>[/color][/color][/color]


              Comment

              • Marty

                #8
                Re: Advanced Hashtable ?

                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:
                [color=blue]
                > "Marty" <xmarty99@hotma il.com> schrieb
                >[color=green]
                >> 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.[/color]
                >
                >
                > It *must* be casted because otherwise the types don't match.
                >
                > Armin[/color]

                Comment

                • Marty

                  #9
                  Re: Advanced Hashtable ?

                  This article is interesting but he stopped where it was beginning to be
                  more interesting, when retrieving values.
                  [color=blue]
                  >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.[/color]

                  yes, I look forward for that kind of functionnality.

                  Have a nice day :)
                  Marty



                  Robin Tucker wrote:
                  [color=blue]
                  > Someone did some "research" on this........... ..........
                  >
                  > http://visualbasic.about.com/od/usin.../aa072103a.htm
                  >
                  >
                  > "Marty" <xmarty99@hotma il.com> wrote in message
                  > news:kzjne.3841 7$tt5.5550@edtn ps90...
                  >[color=green]
                  >>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:
                  >>
                  >>[color=darkred]
                  >>>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.Collecti ons
                  >>>>
                  >>>>Public Class ShortStringDict ionary
                  >>>> Inherits DictionaryBase
                  >>>>
                  >>>> Default Public Property Item(key As [String]) As [String]
                  >>>> Get
                  >>>> Return CType(Dictionar y(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.Valu es
                  >>>> 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.Cont ains(key)
                  >>>> End Function 'Contains
                  >>>>
                  >>>> Public Sub Remove(key As [String])
                  >>>> Dictionary.Remo ve(key)
                  >>>> End Sub 'Remove
                  >>>>
                  >>>>End Class
                  >>>>
                  >>>>"Marty" <xmarty99@hotma il.com> wrote in message
                  >>>>news:H3jne. 28711$on1.401@c lgrps13...
                  >>>>
                  >>>>
                  >>>>>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
                  >>>>
                  >>>>
                  >>>>
                  >>>>[/color][/color]
                  >
                  >[/color]

                  Comment

                  • Cor Ligthert

                    #10
                    Re: Advanced Hashtable ?

                    Marty,
                    [color=blue]
                    > 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?
                    >[/color]
                    Can you explain this a little bit more, because AFAIK you cannot cast a
                    value.

                    Cor


                    Comment

                    • Marty

                      #11
                      Re: Advanced Hashtable ?

                      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(myRetriev edObject,
                      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:
                      [color=blue]
                      > Marty,
                      >
                      >[color=green]
                      >>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?
                      >>[/color]
                      >
                      > Can you explain this a little bit more, because AFAIK you cannot cast a
                      > value.
                      >
                      > Cor
                      >
                      >[/color]

                      Comment

                      • Cor Ligthert

                        #12
                        Re: Advanced Hashtable ?

                        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(myha shtable.Value, theClassIUse)

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

                        Cor


                        Comment

                        • Marty

                          #13
                          Re: Advanced Hashtable ?

                          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:
                          [color=blue]
                          > 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(myha shtable.Value, theClassIUse)
                          >
                          > However you can AFAIK never pass it in a quicker way.
                          >
                          > Cor
                          >
                          >[/color]

                          Comment

                          Working...