在 D365/X++ 中,为什么当我使用 "this" 表示变量存在于实例上时 '(' 无效?

In D365/X++, why is '(' invalid when I use "this" to indicate the variable lives on the instance?

我有以下 class:

public class MyDialogSelect extends RunBase
{
    private DialogField nameField;

    // Snipped for brevity

    public Object dialog()
    {
        Dialog dialog = super();
        nameField = dialog.addField(extendedTypeStr(CustName));
        // Snipped for brevity
        return dialog;
    }

    public void dialogSelectCtrl()
    {
        CustTable customerTable = CustTable::find(accountField.value());
        nameField.value(customerTable.name());
        // Snipped for brevity
    }
}

这会按预期编译和工作。

但是,我更喜欢使用关键字 this 来指示变量何时属于实例,因此我尝试将其更改为:

public class MyDialogSelect extends RunBase
{
    private DialogField nameField;

    // Snipped for brevity

    public Object dialog()
    {
        Dialog dialog = super();
        this.nameField = dialog.addField(extendedTypeStr(CustName));
        // Snipped for brevity
        return dialog;
    }

    public void dialogSelectCtrl()
    {
        CustTable customerTable = CustTable::find(accountField.value());
        this.nameField.value(customerTable.name());
        // Snipped for brevity
    }
}

但是,这不会编译,而是导致 Invalid token '('.。 但是,如果我在 nameField.value(customerTable.name()); 之前删除 this, 它再次按预期工作。 (注:我还是在this.nameField = dialog.addField(extendedTypeStr(CustName));中注明了this)。

当我在调用方法的 属性 之前包含 this 时,为什么它不能编译? 我也观察到 this.nameField.enabled(false) 也失败了。 关于 x++ 何时允许、不允许或需要 this?

,是否有我应该理解的更一般的规则或原则?

this指的是开发的上下文,在你的例子中,this指的是class的整体。如果你添加另一个方法,你将使用 this 调用该方法。

随着最新的表单模式更改,我相信 MSFT 的建议是使用对话框表单模式而不是 class 来生成对话框。

您不能使用 this 在 X++ 中引用实例变量。就像在 C++ 中一样。

您可以(并且必须)使用 this 来引用实例方法。