Generics and collections
am 04.04.2008 15:32:48 von Martin RobinsThis is a multi-part message in MIME format.
------=_NextPart_000_0006_01C89660.C1AA00F0
Content-Type: text/plain;
charset="iso-8859-1"
Content-Transfer-Encoding: quoted-printable
I do not know if what I am trying to do is impossible, however since I =
cannot work out how to do it I thought I would post it here in the hope =
of some assistance.
Consider the following code ...
using System;
public class Parameter
private T value;
public Parameter() {
}
public Parameter(T value)
: this() {
this.value =3D value;
}
public T Value {
get { return this.value; }
set { this.value =3D value; }
}
}
public class BooleanParameter : Parameter
}
public class ByteParameter : Parameter
}
public class WordParameter : Parameter
}
As you can see, this is a simple example where I am creating generic =
types that will all expose a 'Value' property, but each of the 'Value' =
properties will have a different data type.
If I want to create collections for these types, again this is fairly =
basic ...
using System.Collections.ObjectModel;
public class BooleanParameterCollection : =
Collection
}
public class ByteParameterCollection : Collection
}
public class WordParameterCollection : Collection
}
However, what I actually want is to create a collection of Parameter> =
objects; that is, a single typed collection that can contain =
BooleanParameter, ByteParameter or WordParameter objects intermingled =
with each other and then determine which of the objects I am working =
with at run time.
I have tried various combinations of interfaces and base classes, but =
all to no avail. Is this possible, if so, can anybody help me see the =
light?
Thanks in advance.
Martin.
------=_NextPart_000_0006_01C89660.C1AA00F0
Content-Type: text/html;
charset="iso-8859-1"
Content-Transfer-Encoding: quoted-printable
charset=3Diso-8859-1">
is=20
impossible, however since I cannot work out how to do it I thought I =
would post=20
it here in the hope of some assistance.
....
size=3D1><T> {
private T value;
public Parameter() {
public Parameter(T =
value)
: this() {
size=3D1>
this.value =
value;
public T Value {
get { return =
color=3D#0000ff>this.value; =
}
set { this.value =
=3D
color=3D#0000ff>value; =
}
color=3D#0000ff>class ByteParameter =
:
color=3D#2b91af>Parameter<
color=3D#2b91af>Byte
size=3D1>> {
color=3D#0000ff>class WordParameter =
:
color=3D#2b91af>Parameter<
color=3D#2b91af>UInt16
size=3D1>> {
example where I am=20
creating generic types that will all expose a 'Value' property, but each =
of the=20
'Value' properties will have a different data type.
these types,=20
again this is fairly basic ...
System.Collections.ObjectModel;
color=3D#0000ff>class
color=3D#2b91af>BooleanParameterCollection=20
: Collection<
color=3D#2b91af>BooleanParameter
size=3D2>>=20
{
color=3D#0000ff>class
color=3D#2b91af>ByteParameterCollection :=20
Collection<
color=3D#2b91af>ByteParameter> =
{
color=3D#0000ff>class
color=3D#2b91af>WordParameterCollection :=20
Collection<
color=3D#2b91af>WordParameter> =
{
create a=20
collection of Parameter
size=3D2><?> objects; that is, a single typed collection that can=20
contain BooleanParameter,
color=3D#2b91af>ByteParameter or
color=3D#2b91af>WordParameter objects intermingled with each =
other and then=20
determine which of the objects I am working with at run=20
time.
interfaces and=20
base classes, but all to no avail. Is this possible, if so, can anybody =
help me=20
see the light?
------=_NextPart_000_0006_01C89660.C1AA00F0--