Solved

Cannot convert type when using a custom generic collection

Posted on 2008-10-19
4
1,304 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

AWS Certified Solutions Architect - Associate

This course has been developed to provide you with the requisite knowledge to not only pass the AWS CSA certification exam but also gain the hands-on experience required to become a qualified AWS Solutions architect working in a real-world environment.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Recently while returning home from work my wife (another .NET developer) was murmuring something. On further poking she said that she has been assigned a task where she has to serialize and deserialize objects and she is afraid of serialization. Wha…
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…
NetCrunch network monitor is a highly extensive platform for network monitoring and alert generation. In this video you'll see a live demo of NetCrunch with most notable features explained in a walk-through manner. You'll also get to know the philos…
If you’ve ever visited a web page and noticed a cool font that you really liked the look of, but couldn’t figure out which font it was so that you could use it for your own work, then this video is for you! In this Micro Tutorial, you'll learn yo…

617 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