Your Privacy Matters: We use our own and third-party cookies to improve your experience on our website. By continuing to use the website we understand that you accept their use. Cookie Policy
455
how to check for existing owner before adding one?
posted

Hello,

How do you check that an owner exists in the OwnerCollection before you try to add a new one? 

                    if (this._calendarInfo.Owners[ownerID] == null)
                    {
                        taskedOwner = this._calendarInfo.Owners.Add(ownerID);
                        baseTask.Resources.Clear();
                        baseTask.Resources.Add(taskedOwner);

                    }

but it threw an exception on the first line about the key not being found. 

I also tried the IndexOf() and GetItem() methods, but I got an index of 15 when the owner list was empty. 

Sorry if this is a simple question, I'm a C++ programmer learning C# and I didn't see example where it traversed the owner collection before adding a new one. 

Thanks,

Myca

  • 455
    Verified Answer
    posted

    Nevermind, I got it.  I just wasn't patient enough with what I had.  Here's what worked:

                        index = this._calendarInfo.Owners.IndexOf(ownerID);
                        if (index >= 0)
                        {
                            taskedOwner = (Owner)this._calendarInfo.Owners.GetItem(index);
                        }
                        else
                        {
                            taskedOwner = this._calendarInfo.Owners.Add(ownerID);
                        }
                        
                        if (baseTask != null)
                        {
                            baseTask.Resources.Clear();
                            baseTask.Resources.Add(taskedOwner);
                        }