Trouble with generics in 2.0

Trouble with generics in 2.0

Old forum URL: forums.lhotka.net/forums/t/255.aspx


miker55 posted on Friday, June 02, 2006

In Csla 1.5 I had a BusinessBase class and a BusinessCollectionBase class.  This allowed me to declare a variable like so:

public IList<BusinessCollectionBase> DetailTables;

I have tried everything I can think of to declare a similar variable in 2.0.  I was only able to do this:

public IList<IEditableCollection> DetailTables;

But then this code breaks:

foreach (BusinessBase obj in DetailTables[ii])

It tells me "foreach statement cannot operate on variables of type 'Csla.Core.IEditableCollection' because 'Csla.Core.IEditableCollection' does not contain a public definition for 'GetEnumerator'"

Basically, I need to be able to do two things in 2.0 that I can't figure out yet:

  a.) declare a variable whose type is a list of BusinessListBase objects

  b.) enumerate through that list and call methods from BusinessBase on each object in each list

Is this even possible with the new generic<t,c> structure?  It seems very limiting in this regard...

TIA,

Mike Rodriguez

 

Jav replied on Friday, June 02, 2006

You will need to translate the following VB suggestions into C#.

Csla.BusinessListBase is a MustInherit class, from which you can derive a collection class like:

Public Class AddressCollection
         Inherits Csla.BusinessListBase(Of AddressCollection, Address)

End Class

For this to work, you will also need 

Public Class Address
         Inherits Csla.BusinessBase(Of Address)

End Class 

You can now do
            Dim Coll as AddressCollection = AddressCollection.GetAddressCollection(.....)
            For each obj as Address in Coll
                     obj.city = "Chicago"
            End For

I hope this helps.

Jav

miker55 replied on Friday, June 02, 2006

Hi Jav,

 

That works fine if you know the type.  My collection is generic (not literally) and the type won’t be known until runtime.  It was easy to make a generic list of the type BusinessListBase before, then at runtime fill it with different classes.  You can’t do that now in 2.0 because you need specific type info.

 

Thanks,

 

Mike

 

RockfordLhotka replied on Friday, June 02, 2006

You need to find a non-generic base type for BindingList<T> (because that's what I inherit from to create BusinessListBase<T,C>). IList is probably your answer, because BindingList<T> implements IList<T>, which includes IList.

So I think (haven't tried, as I'm theoretically on holiday) that you could do this:

public IList<IList> DetailTables;

Since IList comes from IEnumerable, there should be support for foreach.

foreach (IList obj in DetailTables[ii])

If that doesn't work, it might be that BindingList<T> also includes ICollection - but I'm not sure about that (it might be ICollection<T>, which wouldn't be directly useful).

xal replied on Friday, June 02, 2006

Mike,
What are you using this code? is it a form? or another class?

I have some generic controls / forms and when I use lists I declare them like:

Public Class SomeClass(Of T as BusinessListBase(Of T, C), C as BusinessBase(Of C))

Public DetailTables as IList(Of T)

End Class

The only "issue" is that you have to specify the type of C.
I hope that's what you're looking for.

Andrés

Copyright (c) Marimer LLC