Specifying an interface-implemenation as parameter

A

Alexander Mueller

Hi

it sounds simple, but i want to do the following


I have two interfaces, IBrowseable and INode.
A member-function of IBrowseable, 'getChildren', returns
List<INode>-collections.

Now i want classes that implement IBrowseable
to return different kinds of lists, so they can return
a List of any class that implements INode - as long as
they hold a collection of this INode-type.

Now, how do i specify the type of list i want to be returned?
If i write:

List<INode> getChildren (INode nodetype);

I need an *object* of INode to be passed in, but i simply want
to denote a certain INode-implementation shall be returned

If i write:

List<INode> getChildren (Type nodetype);

that would be okay, but it accepts any types, so it is not exactly
okay. I know i could use an enum for all accepted types, but i think
there must be a built-in-way to simply denote the specific INode-
implementation is queried, without needing an instance of it.

Any hints?


Mfg,
Alex
 
J

Joanna Carter [TeamB]

"Alexander Mueller" <[email protected]> a écrit dans le message de [email protected]...

| I have two interfaces, IBrowseable and INode.
| A member-function of IBrowseable, 'getChildren', returns
| List<INode>-collections.
|
| Now i want classes that implement IBrowseable
| to return different kinds of lists, so they can return
| a List of any class that implements INode - as long as
| they hold a collection of this INode-type.
|
| Now, how do i specify the type of list i want to be returned?
| If i write:
|
| List<INode> getChildren (INode nodetype);
|
| I need an *object* of INode to be passed in, but i simply want
| to denote a certain INode-implementation shall be returned
|
| If i write:
|
| List<INode> getChildren (Type nodetype);
|
| that would be okay, but it accepts any types, so it is not exactly
| okay. I know i could use an enum for all accepted types, but i think
| there must be a built-in-way to simply denote the specific INode-
| implementation is queried, without needing an instance of it.

Try this :

class NodeGetter : IBrowseable
{
public List<nodeT> GetChildren<nodeT>() where nodeT : INode
{
...
}
}

....used like this :

{
List<ClassThatImplementsINode> list =
anIBrowseableImplementer.GetChildren<ClassThatImplementsINode();
}

Joanna
 
A

Alexander Mueller

11.03.2006 23:12 said:
"Alexander Mueller" <[email protected]> a écrit dans le message de news:

Bonne soirée, Joanna!
| I have two interfaces, IBrowseable and INode.
| A member-function of IBrowseable, 'getChildren', returns
| List<INode>-collections.
|
| Now i want classes that implement IBrowseable
| to return different kinds of lists, so they can return
| a List of any class that implements INode.
| If i write:
|
| List<INode> getChildren (INode nodetype);
|
| I need an *object* of INode to be passed in, but i simply want
| to denote a certain INode-implementation shall be returned
|
| If i write:
|
| List<INode> getChildren (Type nodetype);
|
| it accepts any types, so it is not exactly okay.

Try this :

class NodeGetter : IBrowseable
{
public List<nodeT> GetChildren<nodeT>() where nodeT : INode
{
...
}
}

....used like this :

{
List<ClassThatImplementsINode> list =
anIBrowseableImplementer.GetChildren<ClassThatImplementsINode();
}

Thanks a lot for your response, generics and constraints are definitely the
point for me to start from.
I couldn't compile it like this, the compiler complained that i need to
derive the NodeGetter-class of your example from the class that implements INode
(or something of that kind, I worked on it yesterday so i forgot the details :) )
I'll have to do some further reading and testing, to get familiar with the
syntax and the principals and get it going.

Mfg,
Alex
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top