OPC Studio User's Guide and Reference
OnWrite Method
Example 



OpcLabs.ServerOpcUA Assembly > OpcLabs.EasyOpc.UA.NodeSpace Namespace > UAServerNode Class : OnWrite Method
The event arguments.

The value of this parameter cannot be null (Nothing in Visual Basic).

Raises the Write event, supplying the OPC UA write data.
Syntax
'Declaration
 
Protected Overridable Sub OnWrite( _
   ByVal e As UADataVariableWriteEventArgs _
) 
'Usage
 
Dim instance As UAServerNode
Dim e As UADataVariableWriteEventArgs
 
instance.OnWrite(e)
protected virtual void OnWrite( 
   UADataVariableWriteEventArgs e
)
protected:
virtual void OnWrite( 
   UADataVariableWriteEventArgs^ e
) 

Parameters

e
The event arguments.

The value of this parameter cannot be null (Nothing in Visual Basic).

Exceptions
ExceptionDescription

One of the arguments provided to a method is not valid.

This is a usage error, i.e. it will never occur (the exception will not be thrown) in a correctly written program. Your code should not catch this exception.

A null reference (Nothing in Visual Basic) is passed to a method that does not accept it as a valid argument.

This is a usage error, i.e. it will never occur (the exception will not be thrown) in a correctly written program. Your code should not catch this exception.

Remarks

The design of this method, including its naming and purpose, is according to Event Design recommendations in "Member design guidelines" by Microsoft.

In the derived classes, you can override this method instead of having to add an event handler for the event.

As opposed to the behavior of the Write event, this method is also always called for child nodes, even when the HandleChildNodes property is set to false.

Example
// This example shows how to define a custom folder class which implements writing to its data variables.
// You can use any OPC UA client, including our Connectivity Explorer and OpcCmd utility, to connect to the server. 
//
// Find all latest examples here: https://opclabs.doc-that.com/files/onlinedocs/OPCLabs-OpcStudio/Latest/examples.html .
// OPC client, server and subscriber examples in C# on GitHub: https://github.com/OPCLabs/Examples-OPCStudio-CSharp .
// Missing some example? Ask us for it on our Online Forums, https://www.opclabs.com/forum/index ! You do not have to own
// a commercial license in order to use Online Forums, and we reply to every post.

using System;
using OpcLabs.BaseLib.NodeSpace;
using OpcLabs.EasyOpc.UA;
using OpcLabs.EasyOpc.UA.NodeSpace;
using OpcLabs.EasyOpc.UA.OperationModel;

namespace UAServerDocExamples._UAServerNode
{
    class OnWrite
    {
        /// <summary>
        /// A folder in the OPC UA address space, with specialized write behavior.
        /// </summary>
        class FolderWithOnWrite : UAFolder
        {
            public FolderWithOnWrite(string name) : base(name)
            {
            }

            /// <summary>
            /// Processes the supplied OPC UA write data.
            /// </summary>
            /// <param name="e">The event arguments.</param>
            protected override void OnWrite(UADataVariableWriteEventArgs e)
            {
                // Obtain the state associated with the data variable that is being written, and display it on the console
                // together with the new value.
                Console.WriteLine($"Data variable {e.DataVariable.State}, value written: {e.AttributeData.Value}");
            }
        }

        public static void Main1()
        {
            // Instantiate the server object.
            // By default, the server will run on endpoint URL "opc.tcp://localhost:48040/".
            var server = new EasyUAServer();

            // Create a folder node.
            var myFolder = new FolderWithOnWrite("MyFolder");
            server.Add(myFolder);

            // Create data variables in the folder. Distinguish them by their state.
            myFolder.Add(new UADataVariable("MyDataVariable1").ValueType<int>().SetState(1));
            myFolder.Add(new UADataVariable("MyDataVariable2").ValueType<int>().SetState(2));
            myFolder.Add(new UADataVariable("MyDataVariable3").ValueType<int>().SetState(3));
            myFolder.Add(new UADataVariable("MyDataVariable4").ValueType<int>().SetState(4));
            myFolder.Add(new UADataVariable("MyDataVariable5").ValueType<int>().SetState(5));

            // Start the server.
            Console.WriteLine("The server is starting...");
            server.Start();

            Console.WriteLine("The server is started.");
            Console.WriteLine();

            // Let the user decide when to stop.
            Console.WriteLine("Press Enter to stop the server...");
            Console.ReadLine();

            // Stop the server.
            Console.WriteLine("The server is stopping...");
            server.Stop();

            Console.WriteLine("The server is stopped.");
        }
    }
}
' This example shows how to define a custom folder class which implements writing to its data variables.
' You can use any OPC UA client, including our Connectivity Explorer and OpcCmd utility, to connect to the server. 
'
' Find all latest examples here: https://opclabs.doc-that.com/files/onlinedocs/OPCLabs-OpcStudio/Latest/examples.html .
' OPC client and subscriber examples in VB.NET on GitHub: https://github.com/OPCLabs/Examples-QuickOPC-VBNET .
' Missing some example? Ask us for it on our Online Forums, https://www.opclabs.com/forum/index ! You do not have to own
' a commercial license in order to use Online Forums, and we reply to every post.

Imports System
Imports OpcLabs.BaseLib.NodeSpace
Imports OpcLabs.EasyOpc.UA
Imports OpcLabs.EasyOpc.UA.NodeSpace
Imports OpcLabs.EasyOpc.UA.OperationModel

Namespace _UAServerNode
    Partial Friend Class OnWrite
        ''' <summary>
        ''' A folder in the OPC UA address space, with specialized write behavior.
        ''' </summary>
        Class FolderWithOnWrite
            Inherits UAFolder

            Public Sub New(ByVal name As String)
                MyBase.New(name)
            End Sub

            ''' <summary>
            ''' Processes the supplied OPC UA write data.
            ''' </summary>
            ''' <param name="e">The event arguments.</param>
            Protected Overrides Sub OnWrite(ByVal e As UADataVariableWriteEventArgs)
                ' Obtain the state associated with the data variable that is being written, and display it on the console
                ' together with the new value.
                Console.WriteLine($"Data variable {e.DataVariable.State}, value written: {e.AttributeData.Value}")
            End Sub
        End Class

        Shared Sub Main1()
            ' Instantiate the server object.
            ' By default, the server will run on endpoint URL "opc.tcp://localhost:48040/".
            Dim server = New EasyUAServer()

            ' Create a folder node.
            Dim myFolder = New FolderWithOnWrite("MyFolder")
            server.Add(myFolder)

            ' Create data variables in the folder. Distinguish them by their state.
            myFolder.Add(New UADataVariable("MyDataVariable1").ValueType(Of Integer)().SetState(1))
            myFolder.Add(New UADataVariable("MyDataVariable2").ValueType(Of Integer)().SetState(2))
            myFolder.Add(New UADataVariable("MyDataVariable3").ValueType(Of Integer)().SetState(3))
            myFolder.Add(New UADataVariable("MyDataVariable4").ValueType(Of Integer)().SetState(4))
            myFolder.Add(New UADataVariable("MyDataVariable5").ValueType(Of Integer)().SetState(5))

            ' Start the server.
            Console.WriteLine("The server is starting...")
            server.Start()

            Console.WriteLine("The server is started.")
            Console.WriteLine()

            ' Let the user decide when to stop.
            Console.WriteLine("Press Enter to stop the server...")
            Console.ReadLine()

            ' Stop the server.
            Console.WriteLine("The server is stopping...")
            server.Stop()

            Console.WriteLine("The server is stopped.")
        End Sub
    End Class
End Namespace
Requirements

Target Platforms: .NET Framework: Windows 10 (selected versions), Windows 11 (selected versions), Windows Server 2016, Windows Server 2022; .NET: Linux, macOS, Microsoft Windows

See Also