"Barry Mossman" <NO****@NoSpam.com> a écrit dans le message de news:
en**************@TK2MSFTNGP04.phx.gbl...
| My motivation was to have a type safe collection. There will only be one
of
| type of child in the collection.
Well, in that case, all you really need is to use List<T>. This is then
typesafe to whatever type you bind it.
e.g.
{
List<MyClass> myList = new List<MyClass>;
...
}
This provides a typesafe list that can only accept/return items of MyClass
or its derivatives as MyClass instances.
Now if you want to add extra functionality to the basic List<> class then
you can either create a wrapper class that holds a List<> or derive from it,
stating the type to which you want to bind it.
public class Group : List<MyClass>
{
...
}
Now you can do this :
{
Group g = new Group();
MyClass x = new MyClass();
g.Add(x); // typesafe
MyClass y = g[0]; //typesafe
...
}
However, if you don't want to add more functionality to the list class, this
is really redundant and you might as well declare a List<MyClass> whenever
you need one.
| I still haven't found a way to pass my generic class's this as per my
| original question. In the meantime I am building a parameter array to pass
| across the information that I need. I am still curious about how a generic
| class is able to pass a reference of itself.
If you want to pass such a list to another method, then all you have to do
is to declare a parameter of the List<MyClass> type.
public void OnValidateChanged(List<MyClass> group)
{
..
}
....or if you derive a Group class ...
public void OnValidateChanged(Group group)
{
..
}
Any better ?
Joanna
--
Joanna Carter [TeamB]
Consultant Software Engineer