Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Cannot convert type when using a custom generic collection

Posted on 2008-10-19
4
Medium Priority
?
1,314 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 2000 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

Veeam Task Manager for Hyper-V

Task Manager for Hyper-V provides critical information that allows you to monitor Hyper-V performance by displaying real-time views of CPU and memory at the individual VM-level, so you can quickly identify which VMs are using host resources.

Question has a verified solution.

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

In my previous article (http://www.experts-exchange.com/Programming/Languages/.NET/.NET_Framework_3.x/A_4362-Serialization-in-NET-1.html) we saw the basics of serialization and how types/objects can be serialized to Binary format. In this blog we wi…
In my previous two articles we discussed Binary Serialization (http://www.experts-exchange.com/A_4362.html) and XML Serialization (http://www.experts-exchange.com/A_4425.html). In this article we will try to know more about SOAP (Simple Object Acces…
This Micro Tutorial will teach you how to add a cinematic look to any film or video out there. There are very few simple steps that you will follow to do so. This will be demonstrated using Adobe Premiere Pro CS6.
In a question here at Experts Exchange (https://www.experts-exchange.com/questions/29062564/Adobe-acrobat-reader-DC.html), a member asked how to create a signature in Adobe Acrobat Reader DC (the free Reader product, not the paid, full Acrobat produ…
Suggested Courses

916 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