IEnumerable for Gtk.ListStore

I noticed that my patch that makes Gtk.ListStore implement IEnumerable was commited to Gtk# SVN head (thanks!!) so I figured i’d just mention why it’s useful and how to use it.

Preveously if you wanted to traverse all the rows in a ListStore (used for TreeView, ComboBox, etc.) you would have to do something like:

Gtk.TreeIter iter;
trustedNodesListStore.GetIterFirst(out iter);
if (trustedNodesListStore.IterIsValid(iter)) {
    do {
        settings.TrustedNodes.Add((TrustedNodeInfo)trustedNodesListStore.GetValue(iter, 2));
    }  while (trustedNodesListStore.IterNext(ref iter));
}

Now, ListStore implements IEnumerable which adds the GetEnumerator() method. This allows us to simply say:

foreach (object[] currentRow in trustedNodesListStore) {
     settings.TrustedNodes.Add((TrustedNodeInfo)currentRow[2]);
}

NodeStore also implements this, so if you are using NodeView you can take advantage of this as well.

Oh, and to anyone in the Seattle area - the next Seattle Mono User’s Group meeting is tomorrow, check the site for more details.

One Comment

  1. Posted May 15, 2005 at 2:28 am | Permalink

    Great !

Post a Comment

Your email is never published nor shared. Required fields are marked *

*
*