new syntax for adding items to collections

new syntax for adding items to collections

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


matt tag posted on Wednesday, June 17, 2009


Bear with me, please-  just learning the intricacies of CSLA 3.6.3.  (trying to convert a project from 1.51, ooof).

In the old days, when adding an item to a collection, I would often have methods like this one on my collection classes

Class CollectionOfObjs

    Public Function Add() As SomeObj
        Dim o As
SomeObj = SomeObj.NewObject
        List.Add(o)
        Return o
    End Function

End Class

Now, the Add method is built into the BusinessListBase, so my old Add method above has a naming conflict.  My first thought was I could just remove my own Add method above.  However, SomeObj.NewObject is scoped as Friend, so I currently cannot easily move the "add" code to the UI.  So this won't work from the UI.

o = fList.Add(SomeObj.NewObject)   <- does not work, NewObject is "Friend" and inaccessible from UI

Is the preferred convention to simply rename my "Add" function above to "AddItem" and leave it as-is, or should I instead change the scoping of the
NewObject factory method on the child object to Public so I can add it from the UI?

thanks
matt tag

RockfordLhotka replied on Wednesday, June 17, 2009

Actually Add() is part of (I think) BindingList<T> - it comes from some Microsoft base class, not mine.

I think you can do as you suggest - create AddItem().

You can also use the IBindingList approach to "light up" the AddNew() method by overriding AddNewCore(), and that might be the better idea. Then you'd work automatically with data binding against grids too.

matt tag replied on Wednesday, June 17, 2009

overriding AddNewCore will work also - except in the cases where I need to pass in some parameter from the outside that in turn gets passed to the child .NewObject  For example, the NewObject might be passed a Default value or something.  Then my code is more like

Class CollectionOfObjs

    Public Function AddItem(iDefaultValue as integer) As SomeObj
        Dim o As
SomeObj = SomeObj.NewObject(iDefaultValue)
        List.Add(o)
        Return o
    End Function

End Class


I'm not sure how I would pass these default values through to an overridden AddNewCore.

RockfordLhotka replied on Wednesday, June 17, 2009

Yeah, you really can't pass any parameters to AddNewCore() - that's one limitation of the approach. That whole thing is designed to support datagrid controls through data binding, and of course data binding would never have a parameter (or any knowledge of such a concept) to pass into the method.

Copyright (c) Marimer LLC