how to create a new object in a child collection

how to create a new object in a child collection

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


SethSpearman posted on Tuesday, October 30, 2007

I am embarrased to ask this question.  I know it should be simple but I have spent hours trying to figure out the way to do this. 

I have the following BOs:

Meeting

AgendaItems

AgendaItem

 

Agenda Items is a child collection property of meeting.  Of course, this collection has objects of type AgendaItem.  So Meeting-->AgendaItems-->AgendaItem (a collection of them of course).

Assuming all the BOs are properly implemented.  (An assumption I am definitely willing to visit).

I have written UnitTests to test the meeting BO.  It works fine test for all CRUD in the BO.  So I added the AgendaItems/AgendaItem in the last few days.  I went to mod my unitTests with code like this:

dim meeting as Meeting
meeting= Meeting.NewMeeting

...set biz properties .

Then do
If meeting.IsSavable then meeting=meeting.Save

All that works fine.  So I tried to add the following to my unit test:

dim ai as agendaItem=meeting.AgendaItems.AddNew.  This fails on compile sayinig that no constructor can be found. 

So this raises the simple question for me.  How do I create a new AgendaItem into my AgendaItems collection using the meeting object.  In other words...I want to add an agendaItem to the meeting.  What syntax will work.  I looked for hours in the book and online and could not find a clear example.

I know this is a way noobie question.  Just trying to learn this stuff.

Seth Spearman

ajj3085 replied on Tuesday, October 30, 2007

If you want to allow the use of AddNew, you'll need to override AddNewCore in your BLB class.

        /// <summary>Creates a new instance
       
/// in the list.</summary>
        /// <returns>An <see cref="Object"/>.</returns>
        protected override object AddNewCore() {
            AgendaItem result;

            result = AgendaItem.NewItem();
            OnAddingNew( new AddingNewEventArgs( result ) );
            Add( result );

            return result;
        }

alef replied on Tuesday, October 30, 2007


I have the same question : see thread
http://forums.lhotka.net/forums/thread/18800.aspx

The implementation of AddNewCore is only to support the binding mechanism to a grid.
This will not help for unit testing code.

ajj3085 replied on Tuesday, October 30, 2007

That's simply not true.  If your unit testing code calls AddNew (which is what the post shows), this will solve the issue there as well.

Your issue is different, and I'm not quite sure what you're trying to acomplish.

SethSpearman replied on Tuesday, October 30, 2007

ajj,

Thanks for your post.  That did the trick.  I had the code in my object class but did not think I needed it.  When I uncommented that out then it worked like a charm.

As I looked over the biz obj Book I  realize that Rocky also said that the collection private construction should have AllowNew=True.  When do you use that?  My code worked without it. 

Seth

Copyright (c) Marimer LLC