Define new methodsΒΆ

Define a method in C#

Within a NetLogic, the C# syntax to define a new method (for example, Foo`) is the following:

public void Foo()
{

}

The code that the method has to execute is written inside curly brackets ({}).

Note

in the example, the method does not execute any code.

Multiple methods can be defined within a class.

Define an OPC UA method in C#

To define an OPC UA method, therefore can be referenced in Q Studio at design time and also callable at runtime by an OPC UA client, write [ExportMethod] before the method. Below is an example:

[ExportMethod]
public void Foo()
{

}

Create an OPC UA method in Q Studio

  1. Inside the Project panel, click the desired node, then click image1, NetLogic, runtime Netlogic: a new NetLogic is created.

  2. Right-click NetLogic, New, Method:

    • The following code is added in NetLogic:

      [ExportMethod]
      public void MethodName()
      {
      
      }
      
    • NetLogic opens in the editor of the set code.

  3. (Optional) Rename the method in C#.

Define method arguments in C#

Method arguments are defined in round brackets (()) after the method name. An OPC UA method can have any number of input and output arguments.

Important

if the arguments of a method associated with an event are edited in Q Studio, the method must be re-associated.

To define an input argument, indicate the data type between round brackets followed by the argument name. Separate each argument with a comma.

In the following example, two integer input arguments, speed and rpm, are declared:

public void Foo(int speed, int rpm)
{

}

To define an output argument, indicate the keyword out followed by the data type and argument name between round brackets. Separate each argument with a comma. Then assign the values to the output arguments.

In the following example, two output arguments are defined, result and motorSpeed, of string and integer types respectively:

public void Foo(out string result, out int motorSpeed)
{
    result= "Ok";
    motorSpeed = motor.speed;
}

To define both input and output arguments for the same method, the syntax does not change. In the following example, two input and two output arguments are defined:

public void Foo(int speed, int rpm, out string result, out int motorSpeed )
{
    result = "Ok";
    motorSpeed = motor.speed;
}