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
?
You cannot use this
to reference instance variables in X++. Like in C++.
You can (and must) use this
to refer to instance methods.