I ran into a problem a while back when attempting to convert existing .NET
1.1 based code to .NET 2.0 using Generic collections rather than Hashtable,
ArrayList, etc.
I ran into an issue because the old code allowed me to do what basically was
the following assignment:
class SomeClass
{
private Queue q;
SomeClass(Queue q)
{
this.q = q;
}
}
class SomeOtherClass : SomeClass
{
SomeOtherClass( Queue q) : base(q)
{
}
}
Where, when converted to generics is actually:
class SomeClass
{
private Queue<A> q;
SomeClass(Queue <A> q)
{
this.q = q;
}
}
class SomeOtherClass : SomeClass
{
SomeOtherClass( Queue<SubClasso fA> q) : base(q)
{
}
}
Unfortunately Queeu<SubClasso fA> cannot be passed to a Queue<A>.
Now, I was able to resolve the problem in this case by turning the class
which took this into a generic class itself:
class SomeClass<T> where T : A
and
class SomeOtherClass : SomeClass<SubCl assofA>
However, I've now run into essentially the same situation with another
class. I do not believe I can use the same thing here though. The list
(Dictionary, actually) being passed into the class constructor could contain
various subclasses - not just one. Using the above solution of
class SomeOtherClass : SomeClass<SubCl assofA>
limits me to ONLY using SubClassofA objects. But in this case there are
SEVERAL subclasses of A. I can guarrantee that the Dictionary here is ONLY
used in a "read" sense (the Dictionary is not altered). In fact, the only
time the list is used is to retrieve an element (which it currently expects
to be of type A). Various operations are then performed on that object, but
the list itself is never changed.
Does that "read-only" aspect buy me any alternatives?
As I see it now, my only option is to go back to the original Hashtable
object rather than Dictionary.
--
Adam Clauss
1.1 based code to .NET 2.0 using Generic collections rather than Hashtable,
ArrayList, etc.
I ran into an issue because the old code allowed me to do what basically was
the following assignment:
class SomeClass
{
private Queue q;
SomeClass(Queue q)
{
this.q = q;
}
}
class SomeOtherClass : SomeClass
{
SomeOtherClass( Queue q) : base(q)
{
}
}
Where, when converted to generics is actually:
class SomeClass
{
private Queue<A> q;
SomeClass(Queue <A> q)
{
this.q = q;
}
}
class SomeOtherClass : SomeClass
{
SomeOtherClass( Queue<SubClasso fA> q) : base(q)
{
}
}
Unfortunately Queeu<SubClasso fA> cannot be passed to a Queue<A>.
Now, I was able to resolve the problem in this case by turning the class
which took this into a generic class itself:
class SomeClass<T> where T : A
and
class SomeOtherClass : SomeClass<SubCl assofA>
However, I've now run into essentially the same situation with another
class. I do not believe I can use the same thing here though. The list
(Dictionary, actually) being passed into the class constructor could contain
various subclasses - not just one. Using the above solution of
class SomeOtherClass : SomeClass<SubCl assofA>
limits me to ONLY using SubClassofA objects. But in this case there are
SEVERAL subclasses of A. I can guarrantee that the Dictionary here is ONLY
used in a "read" sense (the Dictionary is not altered). In fact, the only
time the list is used is to retrieve an element (which it currently expects
to be of type A). Various operations are then performed on that object, but
the list itself is never changed.
Does that "read-only" aspect buy me any alternatives?
As I see it now, my only option is to go back to the original Hashtable
object rather than Dictionary.
--
Adam Clauss
Comment