【问题标题】:How to declare fields?如何声明字段?
【发布时间】:2016-05-08 10:26:50
【问题描述】:

我在 Vala 中编写了一个 GUI 程序。当我编译它时,编译器会产生这个错误:

The name e1 does not exist in the context of Subtract.minus  

代码是:

using Gtk;
class Subtract:Window{
    public Subtract(){
        this.title="Subtract program";
        this.destroy.connect(Gtk.main_quit);
        var e1=new Entry();
        var e2=new Entry();
        var lbl=new Label("Result");
        var btn=new Button.with_label("Subtract");
        var box=new Box(Gtk.Orientation.VERTICAL,5);
        box.add(e1);
        box.add(e2);
        box.add(lbl);
        box.add(btn);
        this.add(box);
        btn.clicked.connect(minus);
    }
    public void minus(){
        int a=int.parse(e1.get_text());
        int b=int.parse(e2.get_text());
        int result=a-b;
        lbl.set_label(result.to_string());
    }
    public static int main(string[]args){
        Gtk.init(ref args);
        var win=new Subtract();
        win.show_all();
        Gtk.main();
        return 0;
    }
}

如何使变量可以通过minus 方法访问。

【问题讨论】:

  • 我从未使用过 Vala,但我认为 Subtract 是创建 e1 的类范围,而减号是该类的成员。 Vala 如何处理变量的类作用域?
  • 您的 e1e2 变量是 Subtract() 构造函数的本地变量。您需要在 Subtract 类中声明它们。 (我假设您熟悉 Python 或 Ruby 之类的语言,其中实例变量在函数中定义;这不是 Vala 的工作方式。您需要在方法旁边声明它们。)
  • 我不知道如何用 vala 语言声明它们。

标签: scope gtk vala


【解决方案1】:

您必须将小部件的变量(至少 e1e2lbl)声明为字段:

using Gtk;

class Subtract: Window {

    // Fields (sometimes also called "attributes")
    private Entry e1;
    private Entry e2;
    private Label lbl;
    private Button btn;
    private Box box;

    public Subtract () {
        title = "Subtract program";
        destroy.connect (Gtk.main_quit);
        // You don't have to use "this." to access fields in Vala
        // I.e. "this.e1" is equivalent to "e1" in the code below
        e1 = new Entry ();
        e2 = new Entry ();
        lbl = new Label ("Result");
        btn = new Button.with_label ("Subtract");
        box = new Box (Gtk.Orientation.VERTICAL, 5);
        box.add (e1);
        box.add (e2);
        box.add (lbl);
        box.add (btn);
        add (box);
        btn.clicked.connect (minus);
    }

    public void minus () {
        // The compiler happily accepts "e1" (etc.) here now
        // since I have declared them as fields
        int a = int.parse (e1.get_text ());
        int b = int.parse (e2.get_text ());
        int result = a - b;
        lbl.set_label (result.to_string ());
    }

    public static int main (string[] args) {
        Gtk.init (ref args);
        var win = new Subtract ();
        win.show_all ();
        Gtk.main ();
        return 0;
    }
}

PS:正确的技术术语在这里是“范围”。你的代码在构造函数的范围内有变量,我的代码作为变量作为类范围的字段,这使得它们在类的所有方法中可见。

Vala 编译器称其为“上下文”,在本例中大致相同。

【讨论】:

  • 请更正这个程序,以便我理解我的问题。因为 vala 的教程比较少。
  • 我已更正您的代码。我的答案中的代码运行良好,关键是类开头的私有属性声明。这类似于 C++ / C# / Java 和其他语言,顺便说一句。
  • Vala 教程中也有介绍:wiki.gnome.org/Projects/Vala/Tutorial#Basics-1
  • 如果你不介意的话,有你的任何id,比如google plus,telegram。我有一个电报ID。你可以联系我。延斯·米伦霍夫。我的电报 ID 是“rahilin”
  • 如果您愿意,可以加入 Vala IRC 频道:irc://irc.gimp.org/vala 我的昵称是“jensm”。
猜你喜欢
  • 2021-09-23
  • 2021-12-08
  • 2016-04-13
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-11-07
  • 1970-01-01
  • 2019-09-10
相关资源
最近更新 更多