I have the following method which compiles:
private void onModifiedFilter(FormControl sender)
{
FormDataSource mainAccount_ds = sender.formRun()
.dataSource(formDataSourceStr(MainAccount, MainAccount));
mainAccount_ds.executeQuery();
}
As I have no other use for mainAccount_ds
, I would like to inline the value and simplify the code to:
private void onModifiedFilter(FormControl sender)
{
sender.formRun()
.dataSource(formDataSourceStr(MainAccount, MainAccount))
.executeQuery();
}
However, this fails to compile with:
Severity Code Description Project File Line Suppression State Error ClassDoesNotContainMethod: Class 'FormObjectSet' does not contain a definition for method 'executeQuery' and no extension method 'executeQuery' accepting a first argument of type 'FormObjectSet' is found on any extension class. Packt_MainAccountExtension (ISV) [ExpenseManagement] C:\AOSService\PackagesLocalDirectory\Bin\XppSource\ExpenseManagement\AxClass_Packt_MainAccountForm_Extension.xpp 18
mainAccount_ds
alter the ability for this method to compile?To answer your main question: Because the compiler cannot determine the correct downcast type from the return value of the dataSource
method.
To answer your additional questions:
The addition of the mainAccount_ds
variable tells the compiler what type of the return value of the dataSource
method is expected at run time. Note that the executeQuery
method is only defined in the FormDataSource
class, but not in the FormObjectSet
parent class, which is the type of the return value of the dataSource
method.
I don't think so. While there is an as
operator to do explicit downcasts, I haven't found a way to use it inside a chained method call expression.
I hope the other answers cleared up that no magic is happening here. The compiler correctly prevents you in this case from an undefined downcast.
In addition, you might want to read up on casting in x++. Casting and Expression operators are a good place to start.