Reputation: 47
To program a special CollectionEditor, I looked at the Microsoft example, http://www.dotnetframework.org/default.aspx/DotNET/DotNET/8@0/untmp/whidbey/REDBITS/ndp/fx/src/Designer/CompMod/System/ComponentModel/Design/CollectionEditor@cs/1/CollectionEditor@cs. There's a for loop that I don't understand.
object[] items = createdItems.ToArray();
for (int i=0; i<items.length; i++)="" {="" destroyinstance(items[i]);="" }="" createditems.clear();="" if="" (removeditems="" !="null)" removeditems.clear();="" restore="" the="" original="" contents.="" because="" objects="" get="" parented="" during="" createandaddinstance,="" underlying="" collection="" gets="" changed="" add,="" but="" not="" other="" operations.="" all="" consumers="" of="" this="" dialog="" can="" roll="" back="" every="" single="" change,="" will="" at="" least="" additions,="" removals="" and="" reordering.="" see="" asurt="" #85470.="" (originalitems="" &&="" (originalitems.count=""> 0)) {
// Do some Code
}
So far I only knew for loopsfor(initializer condition; iterator){}
What happens at this for-loop?
Many thanks
Ottilie
I want to understand what is going on in this section of the code. Since I implement my own CollectionEditor, the description of the interface does not help me. The task of the method is "Aborts changes made in the editor.", but I would like to understand exactly how it does it. Similar for loops occur elsewhere in the code and I cannot match them with the loop specification. Is there a definition for for-loops anywhere that I could apply here?
Ottilie
Upvotes: 0
Views: 357
Reputation: 131423
This is invalid code. This is not a link to the .NET Source code. It's a site with a copy of the Visual Studio 2005-era code (Whidbey). The site has serious formatting errors and that code is simply garbled.
Windows Forms is open source now, hosted on Github. The actual CollectionEditor.cs file is quite clean. The code for OKButton_Clicked is quite simple :
private void OKButton_Click(object sender, EventArgs e)
{
try
{
if (!_dirty || !CollectionEditable)
{
_dirty = false;
DialogResult = DialogResult.Cancel;
return;
}
if (_dirty)
{
object[] items = new object[_listbox.Items.Count];
for (int i = 0; i < items.Length; i++)
{
items[i] = ((ListItem)_listbox.Items[i]).Value;
}
Items = items;
}
if (_removedItems != null && _dirty)
{
object[] deadItems = _removedItems.ToArray();
for (int i = 0; i < deadItems.Length; i++)
{
DestroyInstance(deadItems[i]);
}
_removedItems.Clear();
}
_createdItems?.Clear();
_originalItems?.Clear();
_listbox.Items.Clear();
_dirty = false;
}
catch (Exception ex)
{
DialogResult = DialogResult.None;
DisplayError(ex);
}
}
Upvotes: 1