[Gtk-sharp-list] Preventing Dups In a ListStore

Michael Hutchinson m.j.hutchinson at gmail.com
Fri Jan 4 19:36:25 EST 2008


On Jan 4, 2008 4:54 PM, Adam Tauno Williams <adam at morrison-ind.com> wrote:
> I have a ListStore -
> panelStore = new Gtk.ListStore (typeof (Whitemice.ZOGI.Entity));
>  - used to hole a collection of participants.  In the UI the user can
> search for additional participants in a dialog which then puts the
> selected participants into the list;  all good.  But what is the
> simplest / most efficient way to prevent dups in a ListStore?  I don't
> want to have the same entity added twice, if it is already there I'd
> prefer to just ignore the operation.
>
> Like -
>
> if (!(panelStore.Contains(entity))) panelStore.AppendValues(entity);
>
>  - but, of course, there is no Contains [or equivalent] method in a
> ListStore.  And the ListStore doesn't seem to care about storing
> multiple references to an object.
>
> Do I need to iterate the contents every time I add an object in order to
> prevent dups?  Or is there some kind of short-cut that I'm missing?

AFAIK you'd have to iterate it (though ForEach will simplify this). Of
course, that's what a Contains () call would do anyway -- if you want
O(1) contains calls, you'd need a hashtable, but a list obviously
needs... a list. Depending on the details of the code, storing a
separate hashtable for these lookups might or might not be more
efficient.

Alternatively you could implement a TreeModel interface around another
collection, but that would require GTK# from SVN for GInterface
support.

-- 
Michael Hutchinson
http://mjhutchinson.com


More information about the Gtk-sharp-list mailing list