Methods
Table of Contents
A method is a block of code containing statements. The statements in that block – the method – will be executed by calling the method and specifying any required method arguments.
Code examples
In the model of the AI-Framework a method can be declared in different ways.
The simple way is straight forward.
When local variables are needed, the body is introduced differently.
When passing arguments, these are added in the brackets of the method name with their type, and the type is repeated near the end.
Below are a few code examples.
Simple example
public IMethodCall SomeMethod()
{
return Method(() => new Body()
{
DoSomething();
...
})
.SetWorkDescription("demonstrate a method")
.SetImage(CommonImages.NiceIcon)
.SetDisabledWhenDetails(ArticleView.NoItemsReason)
.Call();
}
The MethodCallDecorators on lines 8 to 11, like
.SetWorkDescription
and .SetImage are actually properties of this method and not required. Other MethodCallDecorators are for example .AddDisabledWhen, .CopyPropertiesFromFunction and .SetToolTipText.
Local variables
If local variables are needed within the method, the declaration is like this.
public IMethodCall SomeMethodWithLocalVars()
{
return Method(() =>
{
var item = EditItem.Article.CreateItemReference();
return new Body
{
DoSomething();
...
};
});
}
Note:
The body is introduced on line 6 with the statement return new Body
.
This is not the same as the => new body in the simple method declaration (line 3).
Arguments
When arguments are passed to a method, the parameters must be defined in the definition of the method. This is done as follows.
See also Parameters and arguments
public IMethodCall SomeMethodWithLocalVars(SalesInvoiceEntity invoiceEntity)
{
return Method(() =>
{
var item = EditItem.Article.CreateItemReference();
return new Body
{
DoSomething();
...
};
}, invoiceEntity);
}
Note the definition of the arguments after the second-last curly bracket, on line 11.
These arguments get their type within the parenthesis of the method name (line 1):
public IMethodCall NameMethod (SalesInvoiceEntity invoiceEntity).