首页 > 解决方案 > In D365/X++, why is '(' invalid when I use "this" to indicate the variable lives on the instance?

问题描述

I have the following 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 compiles and works as expected.

However, I prefer using the keyword this to indicate when variables belong to the instance, so I try changing it this to:

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

But, this won't compile, instead resulting in Invalid token '('.. However, if I remove this before nameField.value(customerTable.name());, it works as expected again. (Note: I still indicate this in this.nameField = dialog.addField(extendedTypeStr(CustName));).

Why won't it compile when I include this before a property which invokes a method? I've also observed this with this.nameField.enabled(false) also failing. Is there a more general rule or principle I should understand here about when x++ allows, disallows, or requires this?

标签: thisinstancex++dynamics-365-operationsinvalid-characters

解决方案


You cannot use this to reference instance variables in X++. Like in C++.

You can (and must) use this to refer to instance methods.


推荐阅读