I have a C# .NET 2.0 project A (class library) that has a form (TreeForm) that uses Tree objects.
I have a project B that has a class Oak that inherits Tree. The class Oak adds some properties to Tree.
class Oak : ProjectA.Tree
{
public string LeafColor;
}
TreeForm.cs in the class library is a form that has a DataGrid that databinds to a BindingList of Tree objects.
When I try to reference and use the TreeForm in project B with my Oak objects, it’s looking for objects of type Tree.
// in project B here.
BindingList<Oak> oaklist = BindingList<Oak>();
private void show_treeform_button_Click(object sender, EventArgs e)
{
ProjectA.TreeForm tree_form = new ProjectA.TreeForm(oaklist); // this line gives error
tree_form.Show();
}
I’ve tried casting the Oak objects to Tree, but I get the error “Cannot convert type Oak to Tree”. If I just use the objects in project B as Tree objects it works fine.
How can I use my Oak objects in the TreeForm from project A?
You’re going to need to post a short but complete code sample to get any meaningful help. But let me venture a guess here.
Is the second project (B) referencing the first project (A)? You have to make sure that the type
Treeis actually the same Tree type in both instances. In the example you describe – it sounds like project A actually has a reference to project B – in which case, how does B know aboutTree?If you have the
Treeclass declared in both projects – it’s not the same Tree class.One project has to reference the
Treeclass of the other in order for this arrangement to work the way you expect it to.The general way to organize such projects, is to move the shared classes into a third Class Library assembly, and reference that assembly in both project A and project B. Then you can inherit and consume the types exposed by the class library correctly.
EDIT: So now that you’ve posted some code, it’s possible to give you a more accurate response. It looks like what you’re trying to do is pass a generic list of
Oakto a method that expects a generic list ofTree. This is not something you can do prior to .NET 4 which supports generic coveriance (on interface types BTW).In versions of C# prior to 4, you cannot do:
Just because
OakinheritsTreedoes not mean thatList<Oak>inheritsList<Tree>.You can read more about generic variance at Eric Lippert’s blog.
So in your case, if you want to pass a list of Oaks in to
TreeForm()you need to pass it in as a list ofTree. Assuming you don’t want to change the type of the collection, you can just instantiate an appropriate copy: