[Gtk-sharp-list] Enumerable TreeModel Problems

Iggy iggy.ma at gmail.com
Mon Sep 29 16:40:53 EDT 2008


So i have a new class that inherits from TreeStore, and I'd like to
implement IEnumerable so you can foreach through it. The
implementation code is pretty simple. The problem is that this code
works perfect the first time, but if it is run through again it gets
Object Reference Not Found errors. It seems like once this foreach
gets done the TreeStore loses everything in it, or its almost as if
its made into a null TreeStore. I can't for the life of me figure out
why it loses everything when the foreach is done...


[code]
public class EnumerableTreeStore : TreeStore, IEnumerable, IEnumerator
    {
        private int _Index = -1;
        public int Index { get { return _Index; } }

        TreeIter ActiveIter;
        TreePath path;

        public exTreeStore() : base() { }
        public exTreeStore(params GLib.GType[] types) : base(types) { }
        public exTreeStore(params Type[] types) : base(types) { }

        public object Current
        {
            get
            {
                 //If the ActiveIter isn't set, set it to the first one
                if (_Index == -1)
                {
                    this.GetIterFirst(out ActiveIter);
                    _Index = 0;
                }

                return ActiveIter;
            }
        }

        public void Reset()
        {
            _Index = -1;
            ActiveIter = TreeIter.Zero;
        }

        public bool MoveNext()
        {
            // If the ActiveIter isn't set, set it to the first one
            if (_Index == -1)
            {
                this.GetIterFirst(out ActiveIter);
                _Index = 0;

                return true;
            }
            else
            {
                // Get the Current Path
                path = this.GetPath(ActiveIter);

                // If the Iter has any Children
                if (this.IterHasChild(ActiveIter))
                {
                    // Move down to the first Child
                    path.Down();

                    // Get the Active TreeIter || Set whether or not
the Iter is valid
                    bValidIter = this.GetIter(out ActiveIter, path);
                }
                // If the Iter doesn't have any children
                else
                {
                    // Move to the Next TreeIter
                    path.Next();

                    // Get the Active TreeIter || Set whether or not
the Iter is valid
                    bValidIter = this.GetIter(out ActiveIter, path);

                    // If the Iter is not Valid and there are still
Parent Tree Iters
                    while (!bValidIter && path.Depth > 1)
                    {
                        // Move the Path Up to the Parent Iter Depth
                        path.Up();

                        // Get the Parent TreeIter
                        this.GetIter(out ActiveIter, path);

                        // Get the next Active TreeIter || Set whether
or not the Iter is valid
                        bValidIter = this.IterNext(ref ActiveIter);
                    }
                }

                // If we are returning a valid TreeIter, advance the _Index
                if (bValidIter) _Index++;
                else Reset();

                // Return whether or not there is a valid Iter
                return bValidIter;
            }
        }

        public IEnumerator GetEnumerator()
        {
            return this;
        }
}
[/code]


More information about the Gtk-sharp-list mailing list