C#/2.0: Tricky issue with generics.

Tech-Archive recommends: Repair Windows Errors & Optimize Windows Performance



Ok, working my way through a complex library conversion to .NET 2.0 and C#
2.0 with generics I am totally stuck on one thing -if anyone sees the issue
I would be more than glad.

The situation is that we have a tree of "Node" objects. A Node is a
visualizable item, like a component. THe menues of all or ouf applications,
for example, are node hierarchies that an adapter transposes into a menu
model.

Now, Nodes have an internal DataObject, which can hold the actual data -
often a business object. This was so far a Object, now it will be T.

So, the definition of Node looks like this now:

public class Node<T> :
SmartObject,
INamed
where
T : class
{

The T is supposed to indicate the type of the DataObject, which can be any
class. There is a class NodeList that is basically a glorified IList with a
lot of additional functionality. It looks like this:

public class NodeList<T> :
ObjectBindingList<T>
where
T : Node<Object>
{

The idea is that a NodeList can hold any Node and should be strong-typable
for this. Now, here the issues come. A Node itself has a Collection
"Children". This is similar to the collection if children in the component
hierarchy. Nodes are not restricted to subtypes - and the constructor of the
NodeList can take a Node that is the parent. If there is a parent in a
NodeList, there is a lot of stuff going on when you add/remove thigs. So,
there is aconstructor like this:

public NodeList(Node<Object> Parent)

As you can see, ANY Node can be the parent.

And here the issues start. In the constructor of the Node I create the
ChildNodes:

_ChildNodes = new NodeList<Node<Object>> (this);

And the issue is: this blows. And I have no clue why. The error messages
are:

Error 2 The best overloaded method match for
'Foundation.Nodes.NodeList<Foundation.Nodes.Node<object>>.NodeList(Foundation.Nodes.Node<object>)'
has some invalid arguments
C:\WORK\ThonaConsulting\EntityBroker\03.0\Foundation\Nodes\Node.cs 29 18
Foundation

Error 3 Argument '1': cannot convert from 'Foundation.Nodes.Node<T>' to
'Foundation.Nodes.Node<object>'
C:\WORK\ThonaConsulting\EntityBroker\03.0\Foundation\Nodes\Node.cs 29 46
Foundation
Now, this I simply do not understand. The argument "this" is a Node<T> - and
is not convertible to Node<object>? Why not? T is supposed to be a class,
and object is the base-class of all classes. Also, I cano not even cast
this:

_ChildNodes = new NodeList<Node<Object>> ((Node<T>)this);

produces the exactly SAME error. I simply do not understand why. As I said,
the case should be totally valid in my optinion.

Anyone a clue? This starts to look like a compiler error to me, but I still
fear I just overlook something. I mean, this is created WITHIN A node, so
thi sshould be totally valid, or?

Thomas Tomiczek


.



Relevant Pages

  • Re: C#/2.0: Tricky issue with generics.
    ... constructor of your node class. ... > 2.0 with generics I am totally stuck on one thing -if anyone sees the ... > the NodeList can take a Node that is the parent. ... the case should be totally valid in my optinion. ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: Threading Faux Pa
    ... the Parent class data from the database and loads all the objects. ... a TCPClient that is wrapped in a 'communicator' class. ... public class ObjectModelController ... private void LoadObjectModel() ...
    (microsoft.public.dotnet.languages.csharp)
  • Casting Exception. Simple Code... Why! Why! Why!
    ... objTable = .SelectedOptions (the fifth line from ... I should be able to go from a parent to the child, ... Public ReadOnly Property SelectedOptions() As ParentTable ... Public Class ChildTable ...
    (microsoft.public.dotnet.languages.vb)
  • Re: why is a superclass allowed to access protected methods of a subclass?
    ... when both the Parent and Child have a method ... > package FirstGeneration; ... > public class Parent ... protected void doIt { ...
    (comp.lang.java.programmer)
  • Question on inheritance
    ... public class Child: Parent ... public new void Meth() ... though variable cc was assigned Child object. ...
    (microsoft.public.dotnet.languages.csharp)