Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 811755
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T01:05:41+00:00 2026-05-15T01:05:41+00:00

I have a C# .NET 2.0 project A (class library) that has a form

  • 0

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?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-15T01:05:41+00:00Added an answer on May 15, 2026 at 1:05 am

    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 Tree is 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 about Tree?

    If you have the Tree class declared in both projects – it’s not the same Tree class.

    One project has to reference the Tree class 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 Oak to a method that expects a generic list of Tree. 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:

    List<Oak> oaklist = new List<Oak>();
    List<Tree> treeList = oaklist; // illegal
    

    Just because Oak inherits Tree does not mean that List<Oak> inherits List<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 of Tree. Assuming you don’t want to change the type of the collection, you can just instantiate an appropriate copy:

    ProjectA.TreeForm tree_form = 
           new ProjectA.TreeForm( new List<Tree>( oaklist.Cast<Tree>() ) );     
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.