[Gtk-sharp-list] removing childrens of a node in treeview

Christopher David Howie me at chrishowie.com
Thu Aug 19 21:44:27 EDT 2010


On 08/19/2010 09:33 PM, Rampage wrote:
> Wow, wonderfull, it really works like a charm, i still don't understand 
> what out and ref means, i'm really n00b in c# so it was a bit tricky, 
> but now everything seems to work perfectly

Ref and out mean that instead of passing parameters by value, you pass
them by reference.  The differences between out and ref are twofold:

* Out parameters must be assigned to in the method in which they are
declared before that method returns.

* Ref parameters must be assigned before they can be passed to another
method.

For example:

void Foo(out int a)
{
    a = 5; // Not doing this (leaving the method without assigning to
           // a) would be a compile-time error.
}

void Main()
{
    int b;
    Foo(out b);
    Console.WriteLine(b); // Writes "5"
}

If you would like, think of ref as an "in-and-out" parameter.  Both
allow the method being called to mess with locals and fields of other
methods/objects.

You must specify out/ref when calling the method too, for two reasons:
(1) It makes overloading possible (Foo(int) and Foo(ref int)) since
there would be no other way to disambiguate between a ref/non-ref
parameter, and (2) it makes it obvious at the call-site that the method
is allowed to change the local/field you have supplied.

In this particular case, the iter passed in to the Remove() method is
ref because a value is passed in (the iter to be removed) and a value is
passed out (the next iter in the store).

You may wonder why the next iter is not just returned... well, TreeIter
is a value type, so there is no "null" equivalent (except
TreeIter.Zero), so a bool is returned instead to tell you if there is
another row.  This makes loops easier to write:

while (model.Remove(ref node)) { ... }

Instead of something more verbose:

while ((node = model.Remove(node)) != TreeIter.Zero) { ... }

Anyway, I think I'm rambling now.

> Thank you very much for the help.
> 
> PS: there are so many things i would like to ask you about gtk# i didn't 
> find documentation about.. but i don't want to bug you too much :)

No problem.  If you have specific questions, just mail the list.  If I
have free time I'll reply; if not then someone else will probably respond.

-- 
Chris Howie
http://www.chrishowie.com
http://en.wikipedia.org/wiki/User:Crazycomputers

If you correspond with me on a regular basis, please read this document:
http://www.chrishowie.com/email-preferences/

PGP fingerprint: 2B7A B280 8B12 21CC 260A DF65 6FCE 505A CF83 38F5

------------------------------------------------------------------------
                    IMPORTANT INFORMATION/DISCLAIMER

This document should be read only by those persons to whom it is
addressed.  If you have received this message it was obviously addressed
to you and therefore you can read it.

Additionally, by sending an email to ANY of my addresses or to ANY
mailing lists to which I am subscribed, whether intentionally or
accidentally, you are agreeing that I am "the intended recipient," and
that I may do whatever I wish with the contents of any message received
from you, unless a pre-existing agreement prohibits me from so doing.

This overrides any disclaimer or statement of confidentiality that may
be included on your message.


More information about the Gtk-sharp-list mailing list