a generic count handler for all collection classes

Posts   
 
    
trevorg
User
Posts: 104
Joined: 15-Nov-2007
# Posted on: 03-Mar-2008 19:53:41   

I am trying to write a function that will accept any collection class to check its Count property, but I can't figure out how to receive the collection class:

Public Sub CheckCount(theCollection as ????, ExpectedCount as Integer, Message As String) If theCollection.Count <> ExpectedCount Then Throw New System.Exception(Message)

End Sub

I can't figure out how to receive [theCollection].

If possible, I would like to be able to place this in a global project that references SD.LLBLGen.Pro.ORMSupportClasses, but does not reference my generated code project.

Thanks.

daelmo avatar
daelmo
Support Team
Posts: 8245
Joined: 28-Nov-2005
# Posted on: 04-Mar-2008 01:17:33   

Hi trevorg. Something like this should work.

At your independent class (throw an exception if you want):

using SD.LLBLGen.Pro.ORMSupportClasses;

namespace SomeHelper
{
    public static class SomeHelper
    {
        public static bool CheckCount(IEntityCollection2 collection, int expectedCount)
        {
            return (collection.Count == expectedCount);
        }
    }
}

At your BL/GUI code:

EntityCollection<CustomerEntity> customers = new EntityCollection<CustomerEntity>(new CustomerEntityFactory());

bool res = SomeHelper.SomeHelper.CheckCount(customers, 0);
David Elizondo | LLBLGen Support Team
trevorg
User
Posts: 104
Joined: 15-Nov-2007
# Posted on: 04-Mar-2008 16:50:30   

Ok, I tried that....if I have: Dim AF_HEADCollection As New AF_HEADCollection CheckCount(AF_HEADCollection, 1) I get a compile error: "Option Strict On disallows implicit conversions from 'Domain.CollectionClasses.AF_HEADCollection' to 'SD.LLBLGen.Pro.ORMSupportClasses.IEntityCollection2'."

Where: Public Class AF_HEADCollection Inherits EntityCollectionBase(Of AF_HEADEntity) EntityCollectionBase Inherits CollectionCore(Of TEntity)

Public Class AF_HEADEntity Inherits CommonEntityBase

AF_HEADCollection.Count is implemented in ORMSupportClasses.CollectionCore(Of T)

I tried: Public Sub CheckCount2(ByVal collection As EntityCollectionBase(Of CommonEntityBase), ByVal expectedCount As Integer)

End Sub

But get the error: Value of type 'Domain.CollectionClasses.AF_HEADCollection' cannot be converted to 'SD.LLBLGen.Pro.ORMSupportClasses.EntityCollectionBase(Of Domain.EntityClasses.CommonEntityBase)'.

Seems like this should be an easy thing to do.....

goose avatar
goose
User
Posts: 392
Joined: 06-Aug-2007
# Posted on: 04-Mar-2008 17:09:53   

Try sending the collection explicitly converted to the method, something like: CheckCount(AF_HEADCollection as IEntityCollection2, 1), I'm not sure about the correct vb instruction so please verify this.flushed

trevorg
User
Posts: 104
Joined: 15-Nov-2007
# Posted on: 04-Mar-2008 17:20:11   

CheckCount(CType(myAF_HEADCollection, IEntityCollection2), 1)

No compile error, but get a runtime error:

Unable to cast object of type 'Talisman.EnvisionMaximo.Domain.CollectionClasses.AF_HEADCollection' to type 'SD.LLBLGen.Pro.ORMSupportClasses.IEntityCollection2'.

Surely I am not the first person to write a function that accepts any SelfServiced LLBLGen collection object, there's just got to be a way to do this. It is more so a .Net generics issue than it is a LLBLGen issue, but surely there must be a way to do this!

trevorg
User
Posts: 104
Joined: 15-Nov-2007
# Posted on: 04-Mar-2008 18:41:55   

Got it....have to use a constraint:

Public Sub CheckCount(Of theEntityType As EntityBase)(ByVal theCollection As CollectionCore(Of theEntityType), ByVal expectedCount As Integer)

End Sub