Extending System.Collections.Hashtable
-
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
-
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
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
-
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
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-----
-
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-----
Only if the object is a SerializableHashtable. Otherwise he will get an exception thrown Jared jparsons@jparsons.org www.prism.gatech.edu/~gte477n
-
Only if the object is a SerializableHashtable. Otherwise he will get an exception thrown Jared jparsons@jparsons.org www.prism.gatech.edu/~gte477n
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-----
-
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-----
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