Solved

Cannot convert type when using a custom generic collection

Posted on 2008-10-19
4
1,298 Views
Last Modified: 2013-12-17
I have a base entity class, and a generic collection class constrained to use the entity class.

I then created a subclass of each, FFAccount and FFAccountCollection.

All is well until I try to use the generic class with in the program.  I am trying to assign an instance of the  FFAccountCollection class to a property based on the generic base class.


// the base classes

public class SWBase{}
 

public class SWCollectionBase<T> : List<T> where T : SWBase{}
 
 

// the sub classes

public class FFAccount : SWBase{}
 

public class FFAccountCollection : SWCollectionBase<FFAccount>{}
 
 

class Test

 {

   // A property based on the base generic collection

   public SWCollectionBase<SWBase> BusinessObjCollection;

 }

  

  

void RunIt()

 {

   FFAccountCollection amgc = new FFAccountCollection();

  	

   Test tst = new Test();

   tst.BusinessObjCollection = amgc;    ///// ERROR

  }

  

  Error	6	Cannot implicitly convert type 'SWCollectionBase<FFAccount>' to 'SWCollectionBase<SWBase>'

Open in new window

0
Comment
Question by:DanielPat
  • 2
4 Comments
 
LVL 19

Expert Comment

by:drichards
ID: 22754378
Test.BusinessObjectCollection is of type SWCollectionBase<SWBase>, while FFAccountCollection is derived from SWCollectionBase<FFAccount>, so you can't set BusinessCollection to amgc in your code as they are different types not related by inheritance.  You'd have to derive FFAccountCollection from SWCollectionBase<SWBase>.  You could still add FFAccount objects to this collection as FFAccount derives from SWBase, but you'd only get access to the SWBase interface on collection members unless you did casts to FFAccount.

What exactly are you trying to accomplish?
0
 

Author Comment

by:DanielPat
ID: 22754640
I thought I would be able to do exactly what you tell me I cannot!  In otherwords, add FFAccount objects to the collection and access properties of both FFAccount and the base class.  

Should I be using an interface to represent the base class instead?

In otherwords something like this:

public class SWCollectionBase<T> : List<T> where T : ISWBase{}


0
 
LVL 3

Expert Comment

by:trunghieubkit
ID: 22754722
You can assign directly

tst.BusinessObjCollection = amgc;

You can fix by using a loop and assign each item of list instead

void RunIt()

{

	FFAccountCollection amgc = new FFAccountCollection();
 

	Test tst = new Test();

	foreach(FFAccount ff in amgc)

	{

		tst.BusinessObjCollection.Add(ff);

	}

}

Open in new window

0
 
LVL 19

Accepted Solution

by:
drichards earned 500 total points
ID: 22755002
Assigning each member individuallin a loop as trunghieubkit suggests is a possibiilty, although I doubt it is what you are after since you now have two collections instead of one.  In fact, you'd have to add a line:

    tst.BusinessObjCollection = new SWCollectionBase<SWBase>();

prior to the foreach to avoid a null reference exception.

Using an interface is not going to help you since you will still have exactly the same problem.  The basic problem is that you have defined two different list types (via generics) that have compatible member items, which is why the individual member copy works, but the collections themselves are of different types.

The only way assigning collection references will work is if the collections are of exactly the same type, which is this case means SWCollectionBase<SWBase>.  You can assign any type derived from SWBase as members of this collection, which may also not be what you want.  You could mix FFAccount objects and any other type derived from SWBase in the same list.

To suggest a better solution, I would have to know what your design parameters are.  As a first cut, I'd say make SWBase an abstract base class or an interface as you suggest, and give it methods that derived classes are required to implement and then just use SWCollectionBase<SWBase> as the collection class.  You don't need FFAccountCollection in that case.  This does leave you with the problem of non-specific collections I mentioned earlier.

The design will be more complex if you want a series of very type specific collections that can be assigned into a Test.BusinessObjCollection.
0

Featured Post

Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

Join & Write a Comment

This document covers how to connect to SQL Server and browse its contents.  It is meant for those new to Visual Studio and/or working with Microsoft SQL Server.  It is not a guide to building SQL Server database connections in your code.  This is mo…
More often than not, we developers are confronted with a need: a need to make some kind of magic happen via code. Whether it is for a client, for the boss, or for our own personal projects, the need must be satisfied. Most of the time, the Framework…
Illustrator's Shape Builder tool will let you combine shapes visually and interactively. This video shows the Mac version, but the tool works the same way in Windows. To follow along with this video, you can draw your own shapes or download the file…
Access reports are powerful and flexible. Learn how to create a query and then a grouped report using the wizard. Modify the report design after the wizard is done to make it look better. There will be another video to explain how to put the final p…

708 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now