Re: [Vala] collecting delegates




That's a bug in Vala.
The problem is that delegate actually consists of two values - function pointer and object pointer. That's normally done by using two separate variables. Thus, for collections, you need to pack it into one value. Vala doesn't do it automatically at the moment I think (perhaps it's fixed in master, need to check).

You can work around it by doing this:

[Compact]
private class MyDelgTypeWrapper {
        public MyDelgType deleg;
        public MyDelgTypeWrapper (MyDelgType deleg) { this.deleg = deleg; }
}

...

list += new MyDelgTypeWrapper (this.dtest);

...

list[i].deleg(i+1, "hello");



On 08/09/2009 11:28 AM, lariamat wrote:
Dear List
I wanted to check the possibility to put delegates into an array or a
GLib.List and then firing them one by one in a foreach way.
After some attempts I found a compiling solution that unfortunately does
not work.
Can anybody tell me what I am doing wrong here?
Thanks
lariamat

public class DGLTest : GLib.Object {
        public DGLTest() {
                print("construct\n");
                list = {};
        }

        //delegatetype
        public delegate void MyDelgType(int aint, string bstr);
        
        //Array for collecting delegates
        private MyDelgType[] list;
        
        
        //delegate for testing
        public void dtest(int a, string b) {
                for(int i=0;i<a;i++)
                        print("%s-%d\n", b, i);
        }
        
        //test function for adding delegates to the array and calling them
afterwards
        public void run() {
                for(int i=0;i<4;i++) {
                        list+=(MyDelgType)this.dtest;
                }
                for(int i=0;i<list.length; i++) {
                        list[i](i+1, "hello");
                }
        }
        
        public static GLib.MainLoop loop;
        public static int main(string[] args) {
                loop = new GLib.MainLoop(null, false);
                DGLTest dglt = new DGLTest();
                dglt.run();
                loop.run();
                return 0;
        }
}



[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]