Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. Extending System.Collections.Hashtable

Extending System.Collections.Hashtable

Scheduled Pinned Locked Moved C#
questioncsharpc++
6 Posts 3 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • T Offline
    T Offline
    Thomas Gondorf
    wrote on last edited by
    #1

    Hi, I am new to C# and trying to create an extended Class Library that extends Hashtable. I have created a new class like this: 'public class SerializeableHashtable: System.Collections.Hashtable' In a method called Load i load a Hashtable object from file and try to set it to SerializeableHashtable like this: public System.Collections.Hashtable LoadHashtableForSample() { System.Collections.Hashtable table = new System.Collections.Hashtable(); return table; } public void Load() { this = LoadHashtableForSample(); // fails because this is readonly base = LoadHashtableForSample(); // fails because it is not the right context for base ?!? } Both of the things i try in Load fails. So the big question is: How can i assign the Hashtable object to SerializeableHashtable? (In C++ it works using the this pointer ...) Thanks a lot in advance Thomas

    J 1 Reply Last reply
    0
    • T Thomas Gondorf

      Hi, I am new to C# and trying to create an extended Class Library that extends Hashtable. I have created a new class like this: 'public class SerializeableHashtable: System.Collections.Hashtable' In a method called Load i load a Hashtable object from file and try to set it to SerializeableHashtable like this: public System.Collections.Hashtable LoadHashtableForSample() { System.Collections.Hashtable table = new System.Collections.Hashtable(); return table; } public void Load() { this = LoadHashtableForSample(); // fails because this is readonly base = LoadHashtableForSample(); // fails because it is not the right context for base ?!? } Both of the things i try in Load fails. So the big question is: How can i assign the Hashtable object to SerializeableHashtable? (In C++ it works using the this pointer ...) Thanks a lot in advance Thomas

      J Offline
      J Offline
      jparsons
      wrote on last edited by
      #2

      Thomas Gondorf wrote: Both of the things i try in Load fails. So the big question is: How can i assign the Hashtable object to SerializeableHashtable? (In C++ it works using the this pointer ...) You can't. SerializableHashtable is a child of Hashtable. You cannot assign a parent object to a reference pointing to the child. Also you cannot (without shooting yourself in the foot) do this in C++. Suppose you had a method (FooBar) in SerializableHashtable which doesn't exist in Hashtable. If you were allowed to assign a Hashtable object to a SerializableHashtable reference, what would happen if you called FooBar? It is possible for you to assign a SerializableHashtable object to a Hashtable reference. Jared jparsons@jparsons.org www.prism.gatech.edu/~gte477n

      H 1 Reply Last reply
      0
      • J jparsons

        Thomas Gondorf wrote: Both of the things i try in Load fails. So the big question is: How can i assign the Hashtable object to SerializeableHashtable? (In C++ it works using the this pointer ...) You can't. SerializableHashtable is a child of Hashtable. You cannot assign a parent object to a reference pointing to the child. Also you cannot (without shooting yourself in the foot) do this in C++. Suppose you had a method (FooBar) in SerializableHashtable which doesn't exist in Hashtable. If you were allowed to assign a Hashtable object to a SerializableHashtable reference, what would happen if you called FooBar? It is possible for you to assign a SerializableHashtable object to a Hashtable reference. Jared jparsons@jparsons.org www.prism.gatech.edu/~gte477n

        H Offline
        H Offline
        Heath Stewart
        wrote on last edited by
        #3

        But he can cast it to a SerializableHashtable.

        -----BEGIN GEEK CODE BLOCK----- Version: 3.21 GCS/G/MU d- s: a- C++++ UL@ P++(+++) L+(--) E--- W+++ N++ o+ K? w++++ O- M(+) V? PS-- PE Y++ PGP++ t++@ 5 X+++ R+@ tv+ b(-)>b++ DI++++ D+ G e++>+++ h---* r+++ y+++ -----END GEEK CODE BLOCK-----

        J 1 Reply Last reply
        0
        • H Heath Stewart

          But he can cast it to a SerializableHashtable.

          -----BEGIN GEEK CODE BLOCK----- Version: 3.21 GCS/G/MU d- s: a- C++++ UL@ P++(+++) L+(--) E--- W+++ N++ o+ K? w++++ O- M(+) V? PS-- PE Y++ PGP++ t++@ 5 X+++ R+@ tv+ b(-)>b++ DI++++ D+ G e++>+++ h---* r+++ y+++ -----END GEEK CODE BLOCK-----

          J Offline
          J Offline
          jparsons
          wrote on last edited by
          #4

          Only if the object is a SerializableHashtable. Otherwise he will get an exception thrown Jared jparsons@jparsons.org www.prism.gatech.edu/~gte477n

          H 1 Reply Last reply
          0
          • J jparsons

            Only if the object is a SerializableHashtable. Otherwise he will get an exception thrown Jared jparsons@jparsons.org www.prism.gatech.edu/~gte477n

            H Offline
            H Offline
            Heath Stewart
            wrote on last edited by
            #5

            Yeah, of course, or any other class derived from Hashtable.

            -----BEGIN GEEK CODE BLOCK----- Version: 3.21 GCS/G/MU d- s: a- C++++ UL@ P++(+++) L+(--) E--- W+++ N++ o+ K? w++++ O- M(+) V? PS-- PE Y++ PGP++ t++@ 5 X+++ R+@ tv+ b(-)>b++ DI++++ D+ G e++>+++ h---* r+++ y+++ -----END GEEK CODE BLOCK-----

            T 1 Reply Last reply
            0
            • H Heath Stewart

              Yeah, of course, or any other class derived from Hashtable.

              -----BEGIN GEEK CODE BLOCK----- Version: 3.21 GCS/G/MU d- s: a- C++++ UL@ P++(+++) L+(--) E--- W+++ N++ o+ K? w++++ O- M(+) V? PS-- PE Y++ PGP++ t++@ 5 X+++ R+@ tv+ b(-)>b++ DI++++ D+ G e++>+++ h---* r+++ y+++ -----END GEEK CODE BLOCK-----

              T Offline
              T Offline
              Thomas Gondorf
              wrote on last edited by
              #6

              So, i throw this idea away and store the Hashtable Object in a member of SerializeableHashtable. That will work ... Thanks a lot for your help

              1 Reply Last reply
              0
              Reply
              • Reply as topic
              Log in to reply
              • Oldest to Newest
              • Newest to Oldest
              • Most Votes


              • Login

              • Don't have an account? Register

              • Login or register to search.
              • First post
                Last post
              0
              • Categories
              • Recent
              • Tags
              • Popular
              • World
              • Users
              • Groups