gtkgtk3vala

Why identifier expected with this demo code


I am trying demo code from this page:

public class My_combobox{
    private Gtk.ListStore liststore;
    private ComboBox combobox;
    liststore = new Gtk.ListStore(1, typeof (string));
    Gtk.TreeIter iter;
    liststore.append(out iter);
    liststore.set(iter, 0, "Rafael Nadal", -1);
    liststore.append(out iter);
    liststore.set(iter, 0, "Roger Federer", -1);
    liststore.append(out iter);
    liststore.set(iter, 0, "Novak Djokovic", -1);
    cellrenderertext = new CellRendererText();
    combobox = new ComboBox();
    combobox.set_model(liststore);
    combobox.pack_start(cellrenderertext, true);
    combobox.add_attribute(cellrenderertext, "text", 0);
    //combobox.changed.connect(on_combobox_changed);
}

However, I am getting following errors:

$ valac formatGUI_v2.vala --pkg=gtk+-3.0
formatGUI_v2.vala:65.12-65.12: error: syntax error, expected identifier
    liststore = new Gtk.ListStore(1, typeof (string));
              ^
formatGUI_v2.vala:65.31-65.31: error: syntax error, `new' modifier not allowed on creation method
    liststore = new Gtk.ListStore(1, typeof (string));
                                 ^
formatGUI_v2.vala:73.44-73.44: error: syntax error, `new' modifier not allowed on creation method
    cellrenderertext = new CellRendererText();
                                           ^
formatGUI_v2.vala:74.25-74.25: error: syntax error, `new' modifier not allowed on creation method
    combobox = new ComboBox();
                           ^
Compilation failed: 4 error(s), 0 warning(s)

Where is the problem and how can it be corrected?

I tried to put code in constructor:

public class My_combobox{
    public void My_combobox(){
        private Gtk.ListStore liststore;
        private ComboBox combobox;
        liststore = new Gtk.ListStore(1, typeof (string));
        Gtk.TreeIter iter;
        liststore.append(out iter);
        liststore.set(iter, 0, "Rafael Nadal", -1);
        liststore.append(out iter);
        liststore.set(iter, 0, "Roger Federer", -1);
        liststore.append(out iter);
        liststore.set(iter, 0, "Novak Djokovic", -1);
        cellrenderertext = new CellRendererText();
        combobox = new ComboBox();
        combobox.set_model(liststore);
        combobox.pack_start(cellrenderertext, true);
        combobox.add_attribute(cellrenderertext, "text", 0);
        //combobox.changed.connect(on_combobox_changed);
    }
}

But still, I get following errors, both with gtk3 and gtk2:

$ valac formatGUI_v2.vala --pkg=gtk+-2.0
formatGUI_v2.vala:64.11-64.13: error: syntax error, following expression/statement delimiter `;' missing
        private Gtk.ListStore liststore;
                ^^^
formatGUI_v2.vala:64.14-64.14: error: syntax error, expected identifier
        private Gtk.ListStore liststore;
                   ^
formatGUI_v2.vala:66.13-66.13: error: syntax error, expected identifier
        liststore = new Gtk.ListStore(1, typeof (string));
                  ^
formatGUI_v2.vala:66.32-66.32: error: syntax error, `new' modifier not allowed on creation method
        liststore = new Gtk.ListStore(1, typeof (string));
                                     ^
formatGUI_v2.vala:74.42-74.42: error: syntax error, `new' modifier not allowed on creation method
        cellrenderertext = new CellRendererText();
                                               ^
formatGUI_v2.vala:75.26-75.26: error: syntax error, `new' modifier not allowed on creation method
        combobox = new ComboBox();
                               ^
Compilation failed: 6 error(s), 0 warning(s)

Solution

  • The demo code you link to does make the difference between declaring the fields in the class and then initialising them in the constructor, but that has been lost in the example in your question.

    Your code should be more of the form:

    public class My_combobox
    {
        // Here you declare the field
        private Gtk.ListStore liststore;
    
        // Here, in the constructor, you instantiate and initialise
        // the ListStore and assign it to the field. This is called when
        // the class is instantiated as an object at runtime
        public My_combobox()
        {
          liststore = new Gtk.ListStore(1, typeof (string));
          Gtk.TreeIter iter;
          liststore.append(out iter);
          liststore.set(iter, 0, "Rafael Nadal", -1);
        }
    }