Call .NET Methods with Optional Arguments
MATLAB® supports calling .NET methods with optional arguments. In the MATLAB method signature, optional arguments are displayed using the
optional<T> syntax, where T is a
specific type.
To use a default method argument, pass an instance of
System.Reflection.Missing.Value.
Call Nonoverloaded .NET Method
If a .NET method is not overloaded, you do not need to specify all optional
arguments at the end of the parameter list. Omitted arguments use their default
values. To run this example, build and load the NetDocOptional
assembly from the C# source code using the directions in Build and Load .NET Assembly for MATLAB.
C# Method
The str1 and str2 parameters in this
method are optional.
public string Greeting(int x,string str1="hello",string str2="world")Call Method from MATLAB
First, display the Greeting method signature.
methodsview("netdoc.NetDocOptional")| Return Type | Name | Arguments |
|---|---|---|
System.String RetVal | Greeting | (NetDocOptional.MyClass this, |
From within MATLAB, call the Greeting method without the optional
arguments. The returned string shows the default values of the optional
arguments.
mlClass = NetDocOptional.MyClass; Greeting(mlClass,0)
ans = hello world
Next, call the Greeting method using the default value for
the first optional argument and a specified value for the second optional
argument.
def = System.Reflection.Missing.Value;
Greeting(mlClass,0,def,"Mr. Jones")ans = hello Mr. Jones
Finally, call the Greeting method using a specified value
for the first optional argument and the default value for the second optional
argument. In this case, you can omit the last argument.
Greeting(mlClass,0,"My")
ans = My world
NetDocOptional C# Source Code
This C# code defines the NetDocOptional.MyClass class,
which includes the Greeting method with two optional string
arguments.
using System;
using System.Text;
namespace NetDocOptional
{
public class MyClass
{
public string Greeting(
int x,
string str1 = "hello",
string str2 = "world")
{
return str1 + " " + str2;
}
}
}Call Overloaded Method with Optional Arguments
If a .NET class has overloaded methods with optional arguments, MATLAB matches the method based on the number and type of provided input arguments.
If the overloaded methods differ by the type, number, or dimension of their optional arguments, MATLAB first compares the types of mandatory arguments.
If the mandatory argument types are different, MATLAB chooses the first overloaded method defined in the class.
If the mandatory argument types are the same, you must specify enough optional arguments to make the method call unambiguous. Otherwise, MATLAB throws an error.
Call Method from MATLAB
Build your own .NET assembly Doc that defines the
calc method in Class and load it into
MATLAB.
Display the calc method signatures.
methodsview("netdoc.NetDocOptional")| Return Type | Name | Arguments |
|---|---|---|
single scalar RetVal | calc | (Doc.Class this, |
double scalar RetVal | calc | (Doc.Class this, |
Usage in MATLAB
Create a Class object and then call calc
with explicit arguments.
mlClass = Doc.Class; calc(mlClass,3,4)
ans =
7
If you try to use the default values by omitting the arguments, MATLAB cannot determine which signature to use.
calc(mlClass)
Cannot choose between the following .NET method signatures due to unspecified optional arguments in the call to 'calc': 'Doc.Class.calc(Doc.Class this, optional<int32 scalar> x, optional<single scalar> y)' and 'Doc.Class.calc(Doc.Class this, optional<int32 scalar> x, optional<double scalar> y)' You can resolve this ambiguity by specifying enough additional optional arguments so that there is only one possible matching .NET method.
Resolve the ambiguity by specifying enough arguments or by passing
System.Reflection.Missing.Value.
def = System.Reflection.Missing.Value; calc(mlClass,def,def) calc(mlClass,3,def) calc(mlClass,def,4)
ans =
44
ans =
14
ans =
37