Generic Collection Creation

Posts   
 
    
MGrassman
User
Posts: 7
Joined: 11-Dec-2006
# Posted on: 15-Sep-2010 00:49:52   

I'm trying to create a collection with Generics.

where E : MarketEntity

public class GenericMapper<E, B>
        where E : CommonEntityBase
        where B : IBusinessObject
    {

         public static EntityCollectionBase<E> ToCollection(List<B> oldBusinessObjecList)
        {
            if (oldBusinessObjecList == null) return null;
            //This line give me an error
            EntityCollectionBase<E> newCollecion = new EntityCollectionBase<E>();
            foreach (B businessObject in oldBusinessObjecList)
            {
                Mapper.CreateMap<B, E>();
                newCollecion.Add(Mapper.Map<B, E>(businessObject));
            }
            return newCollecion;
        }

    }

EntityCollectionBase<E> newCollecion = new EntityCollectionBase<E>();

This gives me a compile error but seems to work when I pass EntityCollectionBase<E> into a function.

Any Suggestions?

MTrinder
User
Posts: 1461
Joined: 08-Oct-2008
# Posted on: 15-Sep-2010 17:25:44   

Without checking the code, I would suspect that EntityCollectionBase is abstract, so you can't create an instance of it. Try

 EntityCollectionBase<E> newCollecion = new EntityCollection<E>();

instead

Matt

MGrassman
User
Posts: 7
Joined: 11-Dec-2006
# Posted on: 15-Sep-2010 17:41:26   

I received the following error

The type or namespace name 'EntityCollection' could not be found (are you missing a using directive or an assembly reference?)

Thanks,

MTrinder
User
Posts: 1461
Joined: 08-Oct-2008
# Posted on: 16-Sep-2010 11:46:29   

It's in "HelperClasses" of your generated DatabaseGeneric project, so you need to reference that.

Matt

MGrassman
User
Posts: 7
Joined: 11-Dec-2006
# Posted on: 17-Sep-2010 21:52:25   

Attached is a list of my HelperClasses I don't see that in there?

Walaa avatar
Walaa
Support Team
Posts: 14983
Joined: 21-Aug-2005
# Posted on: 21-Sep-2010 09:56:45   

I think you need to use something like:

public static void Stuff<E,C>()
            where E: EntityBase, new() 
            where C : EntityCollectionBase<E>, new()
{
                EntityCollectionBase<E> Collection = new C(); 
                EntityBase Entity = new E();
}