المساعد الشخصي الرقمي

مشاهدة النسخة كاملة : Weird Collection Thing [modified]



C# Programming
04-24-2009, 08:24 PM
I have the following collection:


public class PopupWindowItem
{
public Object WindowObject { get; set; } // wpf window object
public UDPWindowType WindowType { get; set; } // wpf tabitem object
public bool AllowMultipleInstances { get; set; }

public PopupWindowItem(Object window, UDPWindowType windowType, bool allowMultiple)
{
this.WindowObject = window;
this.WindowType = windowType;
this.AllowMultipleInstances = allowMultiple;
}
}

public class PopupWindowManager : List
{
public PopupWindowManager()
{
this.Clear();
}
}


When the user clicks a button to open a window, a new PopupWindowItem is created and populated with the indicated objects. This part woerks fine. Here's the code:

WindowAccountManager accountManager = new WindowAccountManager();
accountManager.Closed += new EventHandler(accountManager_Closed);
m_popupWindowMgr.Add(new PopupWindowItem(accountManager, UDPWindowType.AccountManager, false));
accountManager.Show();


When the application is closed, I traverse the PopupWindowManager collection and close the windows indicated therein. This is where it gets interesting. Here's the code I'm using:

while (m_popupWindowMgr.Count > 0)
{
switch (m_popupWindowMgr[0].WindowType)
{
case UDPWindowType.AccountManager :
{
// When you close the Window object inside the PopupWindowItem object,
// the PopupWindowItem object is removed from the collection, and the
// collection's count is reduced by 1.
((WindowAccountManager)(m_popupWindowMgr[0].WindowObject)).Close();
}
break;
}
}


When I call close on the window object inside the PopupWindowItem object, the whole PopupWindowObject is removed from the collection. As you might guess this is unexpected behavior. Why is it doing this?


"Why don't you tie a kerosene-soaked rag around your ankles so the ants won't climb up and eat your candy ass..." - Dale Earnhardt, 1997
-----
"...the staggering layers of obscenity in your statement make it a work of art on so many levels." - Jason Jystad, 10/26/2001



modified on Friday, April 24, 2009 12:15 PM