Define methods in C#
Learn how to define methods and OPC UA methods in C#.
Method
Within a NetLogic, define a new method (for example,
Foo
) using the
following syntax:public void Foo() { // Code to run }
TIP:
You can define multiple methods within a
class.
OPC UA method
OPC UA methods:
- Can be referenced at design time.
- Are callable at runtime.
To define an OPC UA method, add
[ExportMethod]
before the method.
See the following
example:[ExportMethod]public void Foo() { // Code to run }
For more information, see Create an OPC UA method.
Method arguments
Define method arguments in round brackets
()
after the method name.
An OPC UA method can have any number of input and output arguments.
IMPORTANT:
If you edit the arguments of a method
associated with an event in
FactoryTalk Optix Studio
, associate the method again.
See Read output values of a method.To define an input argument:
- Indicate the data type between the round brackets followed by the argument name.
- Separate each argument with a comma.
Example: Two integer input arguments,
speed
and
rpm
, are declared:public void Foo(int speed, int rpm) { // Code to run }
To define an output argument:
- Indicate the keywordoutfollowed by the data type and argument name between the round brackets.
- Separate each argument with a comma.
- Assign the values to the output arguments.
Example: Two output arguments,
result
and
motorSpeed
, of string and integer types
respectively:public void Foo(out string result, out int motorSpeed) { result = "Ok"; motorSpeed = motor.speed; }
Define both input and output arguments for the same method. Example: Two input and
two output
arguments:
public void Foo(int speed, int rpm, out string result, out int motorSpeed) { result = "OK"; motorSpeed = motor.speed; }
Provide Feedback