Casting and templates...

O

Oriane

Hi there

The following code doesn't complie:
________________________________________________________
public interface ITree
{
ITree GetFather ();
List<ITree> GetChildren();
}


public class A : ITree
{
protected A m_father;
protected List<A> m_ListChildren;

public ITree GetFather() { return m_father; }
public List<ITree> GetChildren() { return m_ListChildren; }
}
________________________________________________________

since "Error 2 Cannot implicitly convert type
'System.Collections.Generic.List<XMLTreeview.A>' to
'System.Collections.Generic.List<XMLTreeview.ITree>'

However A inherits from ITree, so the cast should be working no...
Is there any workaround ?

Oriane
 
A

Alex Meleta

Hi Oriane,

Classes use polymorphism till they are not generic types :)
So, you can use something like this "new List<ITree>(m_ListChildren.ToArray());"
to convert the values.

Regards, Alex
[TechBlog] http://devkids.blogspot.com


O> ......
O> public class A : ITree
O> {
O> protected A m_father;
O> protected List<A> m_ListChildren;
O> public ITree GetFather() { return m_father; }
O> public List<ITree> GetChildren() { return m_ListChildren; }
O> } .....
O> However A inherits from ITree, so the cast should be working no... Is
O> there any workaround ?
 
O

Oriane

Alex Meleta said:
Hi Oriane,

Classes use polymorphism till they are not generic types :) Good point :-(
So, you can use something like this "new
List<ITree>(m_ListChildren.ToArray());" to convert the values.
Set and match !!

Thanks !!
 

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