IEntityCollection question - Sounds dumb to me.

Posts   
 
    
bswami1
User
Posts: 16
Joined: 06-Jan-2005
# Posted on: 16-Jun-2005 20:02:33   

Why does this not work


ProgramEntity MyProg = (ProgramEntity) 
UManager.Program_ReportToShow(this.PageUserID,MyOrg.OrgID).Items[0];

while this works.


ProgramEntity MyProg = null;
foreach (ProgramEntity pe in UManager.Program_ReportToShow(this.PageUserID,MyOrg.OrgID) )
{
     MyProg = pe;
     break;
}

//Pick value from MyProg...

Basically I seem unable to pick the value from the indexer.

Configuration: Adapter, v1.0.2004.2, sql server 2000.

Otis avatar
Otis
LLBLGen Pro Team
Posts: 39933
Joined: 17-Aug-2003
# Posted on: 16-Jun-2005 20:43:24   

change:


ProgramEntity MyProg = (ProgramEntity)
UManager.Program_ReportToShow(this.PageUserID,MyOrg.OrgID).Items[0];

in


ProgramEntity MyProg = (ProgramEntity)
UManager.Program_ReportToShow(this.PageUserID,MyOrg.OrgID)[0];

Frans Bouma | Lead developer LLBLGen Pro
swallace
User
Posts: 648
Joined: 18-Aug-2003
# Posted on: 16-Jun-2005 20:44:32   

When in doubt, add more stuff... Try this:

ProgramEntity MyProg = ( (ProgramEntity) (
UManager.Program_ReportToShow(this.PageUserID,MyOrg.OrgID).Items[0] ) ) ;

Note the additional parenthesis.

bswami1
User
Posts: 16
Joined: 06-Jan-2005
# Posted on: 16-Jun-2005 21:33:29   

No, I tried, both code. It would not work.

I can do foreach, bind to a datagrid. Both work ok. When I use a indexer, it would not work.

May be it is my namespace ....



...default vs.net asp .net stuff

using DomainManager;
using GMENextDB.EntityClasses; 
using GMENextDB.HelperClasses;
using GMENext.Usercontrol;
using SD.LLBLGen.Pro.ORMSupportClasses;

Sorry for being a trouble about this small thing.

bswami1
User
Posts: 16
Joined: 06-Jan-2005
# Posted on: 16-Jun-2005 21:34:11   

When I say it would not work, I get null reference exception.

Otis avatar
Otis
LLBLGen Pro Team
Posts: 39933
Joined: 17-Aug-2003
# Posted on: 16-Jun-2005 22:47:53   

what does: UManager.Program_ReportToShow do?

Frans Bouma | Lead developer LLBLGen Pro
bswami1
User
Posts: 16
Joined: 06-Jan-2005
# Posted on: 17-Jun-2005 22:06:52   

        public EntityCollection Program_ReportToShow (int userID, byte orgID)
        {
            EntityCollection roles = this.UserPositionCurrent(userID);
            EntityCollection ProgramsToReturn = new EntityCollection();

            foreach (UserPositionEntity up in roles)
            {
                if (up.OrgID == orgID)
                {
                    EntityCollection ToAdd = this.ProgramForRole(up);
                    this.UniqueCollection(ToAdd,ProgramsToReturn);
                }
            }
            return ProgramsToReturn;
        }



        private void UniqueCollection(EntityCollection toAdd, EntityCollection addedTo)
        {
            foreach (IEntity2 entity in toAdd)
            {
                if (! addedTo.Contains(entity) )
                {
                    addedTo.Add(entity);
                }
            }
        }


Otis avatar
Otis
LLBLGen Pro Team
Posts: 39933
Joined: 17-Aug-2003
# Posted on: 18-Jun-2005 12:11:18   

Well, the method returns a collection.

So the indexer should compile, though you index item 0. It might be the collection is emtpy, which would give an error. Foreach over the collection will work, as it then just skips the loop (don't foreach over this collection directly, first store it in a variable, as the method is expensive: every foreach iteration will call the method again). Could that be the problem?

Frans Bouma | Lead developer LLBLGen Pro
bswami1
User
Posts: 16
Joined: 06-Jan-2005
# Posted on: 20-Jun-2005 21:17:29   

No, there are entities in the collection. I found out by i) foreach ii) binding to the grid.

Otis avatar
Otis
LLBLGen Pro Team
Posts: 39933
Joined: 17-Aug-2003
# Posted on: 20-Jun-2005 21:57:27   

Hmm. Well the indexer on EntityCollection just returns List[index], so there's nothing special about it.

If you get a nullreference exception, could you please paste the stacktrace? Very strange as the indexer looks like:


public virtual IEntity2 this [int index]
{
    get { return (IEntity2)List[index]; }
    set 
    { 
        if(_isReadOnly && !EntityCollectionBase2.InDesignMode)
        {
            throw new InvalidOperationException("This collection is read-only");
        }
        // first dereference the current entity on the index specified so the entity on list[index] can't keep this collection 
        // into memory.
        if(List[index]!=null)
        {
            ((EntityBase2)List[index]).EntityContentsChanged-= new EventHandler(EntityInListOnEntityContentsChanged);
        }
        List[index]=value;
        if(!_deserializationInProgress)
        {
            if(_containingEntity!=null)
            {
                value.SetRelatedEntity(_containingEntity, _containingEntityMappedField);
            }

            OnListChanged(index, ListChangedType.ItemAdded);
        }

        // subscribe to changed event so the list can signal changes to bound controls.
        value.EntityContentsChanged+=new EventHandler(EntityInListOnEntityContentsChanged);
    }
}

i.e.: the get is a single statement...

Frans Bouma | Lead developer LLBLGen Pro