[Vala] collecting delegates



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;
        }
}

Attachment: delegatelisttest.vala
Description: Text Data



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