eric’s extremeboredom

adventures into and out of extreme boredom.

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.


Categorized as Open Source, Mono, Technology, Software Development, Technology

1 Comments

  1. Great !

Leave a Reply