Solved

Cannot convert type when using a custom generic collection

Posted on 2008-10-19
4
1,302 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

Forrester Webinar: xMatters Delivers 261% ROI

Guest speaker Dean Davison, Forrester Principal Consultant, explains how a Fortune 500 communication company using xMatters found these results: Achieved a 261% ROI, Experienced $753,280 in net present value benefits over 3 years and Reduced MTTR by 91% for tier 1 incidents.

Question has a verified solution.

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

Flash (http://en.wikipedia.org/wiki/Adobe_Flash) has evolved over the years to where it has become a masterful tool for displaying content screen.  It has excellent layout placement, UI precision as well as rendering capabilities. This, along with t…
The object model of .Net can be overwhelming at times – so overwhelming that quite trivial tasks often take hours of research. In this case, the task at hand was to populate the datagrid from SQL Server database in Visual Studio 2008 Windows applica…
A short tutorial showing how to set up an email signature in Outlook on the Web (previously known as OWA). For free email signatures designs, visit https://www.mail-signatures.com/articles/signature-templates/?sts=6651 If you want to manage em…
How to Install VMware Tools in Red Hat Enterprise Linux 6.4 (RHEL 6.4) Step-by-Step Tutorial

710 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