this - 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
?
解决方案
You cannot use this
to reference instance variables in X++. Like in C++.
You can (and must) use this
to refer to instance methods.
推荐阅读
- jvm - 对于 JVM 字段描述符,为什么使用 Z 表示布尔值,L 表示类的实例,而 J 表示长?
- html - 如何在我的 CSS 样式定义中排除没有子菜单的项目?
- excel - Excel公式查找文本中的所有文本
- spring-boot - 如何创建用于签署 JWT 令牌的 Spring 安全密钥?
- jekyll - 如何使用 jekyll 处理 md 文件中的标头?
- javascript - 搜索过滤器上的 jquery - 换行
- html - 如何选择具有共享基类和附加类的元素的子元素?
- php - Google Chart Area 不显示 mysql 数据库
- arrays - Swift:如何正确地将字符串值与多个值进行比较
- angular - Angular 的路由器的 data 属性是通过 url 传递的吗?