OPC Studio User's Guide and Reference
Add Method (_StructuredData)
Example 



OpcLabs.BaseLib Assembly > OpcLabs.BaseLib.DataTypeModel.ComTypes Namespace > _StructuredData Interface : Add Method
The name of the field.
The generic data to be added.
Adds generic data for the specified field name to the structured data.
Syntax
'Declaration
 
Sub Add( _
   ByVal fieldName As String, _
   ByVal genericData As Object _
) 
'Usage
 
Dim instance As _StructuredData
Dim fieldName As String
Dim genericData As Object
 
instance.Add(fieldName, genericData)

Parameters

fieldName
The name of the field.
genericData
The generic data to be added.
Example

.NET

COM

// This example shows different ways of constructing generic data.
//
// Find all latest examples here: https://opclabs.doc-that.com/files/onlinedocs/OPCLabs-OpcStudio/Latest/examples.html .

using System;
using System.Collections;
using OpcLabs.BaseLib.DataTypeModel;

namespace UADocExamples.ComplexData._GenericData
{
    class _Construction
    {
        public static void Main1()
        {
            // Create enumeration data with value of 1.
            var enumerationData = new EnumerationData(1);
            Console.WriteLine(enumerationData);

            // Create opaque data from an array of 2 bytes, specifying its size as 15 bits.
            var opaqueData1 = new OpaqueData(new byte[] {0xAA, 0x55}, sizeInBits:15);
            Console.WriteLine(opaqueData1);

            // Create opaque data from a bit array.
            var opaqueData2 = new OpaqueData(new BitArray(new[] { false, true, false, true, false }));
            Console.WriteLine(opaqueData2);

            // Create primitive data with System.Double value of 180.0.
            var primitiveData1 = new PrimitiveData(180.0d);
            Console.WriteLine(primitiveData1);

            // Create primitive data with System.String value.
            var primitiveData2 = new PrimitiveData("Temperature is too high!");
            Console.WriteLine(primitiveData2);

            // Create sequence data with two elements, using collection initializer syntax.
            var sequenceData1 = new SequenceData
            {
                opaqueData1,
                opaqueData2
            };
            Console.WriteLine(sequenceData1);

            // Create the same sequence data, using the Add method.
            var sequenceData2 = new SequenceData();
            sequenceData2.Elements.Add(opaqueData1);
            sequenceData2.Elements.Add(opaqueData2);
            Console.WriteLine(sequenceData2);

            // Create the same sequence data, using an array (an enumerable) of its elements.
            var sequenceData3 = new SequenceData(
                new GenericDataCollection(new[] {opaqueData1, opaqueData2}));
            Console.WriteLine(sequenceData3);

            // Create structured data with two members, using collection initializer syntax.
            var structuredData1 = new StructuredData
            {
                {"Message", primitiveData2},
                {"Status", enumerationData}
            };
            Console.WriteLine(structuredData1);

            // Create the same structured data using the Add method.
            var structuredData2 = new StructuredData();
            structuredData2.Add("Message", primitiveData2);
            structuredData2.Add("Status", enumerationData);
            Console.WriteLine(structuredData2);

            // Create union data.
            var unionData1 = new UnionData("DoubleField", primitiveData1);
            Console.WriteLine(unionData1);
        }
    }
}
# This example shows different ways of constructing generic data.
#
# Find all latest examples here: https://opclabs.doc-that.com/files/onlinedocs/OPCLabs-OpcStudio/Latest/examples.html .
# OPC client and subscriber examples in Python on GitHub: https://github.com/OPCLabs/Examples-QuickOPC-Python .
# The QuickOPC package is needed. Install it using "pip install opclabs_quickopc".
import opclabs_quickopc

# Import .NET namespaces.
from System import *
from System.Collections import *
from OpcLabs.BaseLib.DataTypeModel import *


# Create enumeration data with value of 1.
enumerationData = EnumerationData(1)
print(enumerationData)

# Create opaque data from an array of 2 bytes, specifying its size as 15 bits.
opaqueData1 = OpaqueData(
    [0xAA, 0x55],
    15) # sizeInBits
print(opaqueData1)

# Create opaque data from a bit array.
bitArray = BitArray(5)
bitArray[0] = False
bitArray[1] = True
bitArray[2] = False
bitArray[3] = True
bitArray[4] = False
opaqueData2 = OpaqueData(bitArray)
print(opaqueData2)

# Create primitive data with System.Double value of 180.0.
primitiveData1 = PrimitiveData(180.0)
print(primitiveData1)

# Create primitive data with System.String value.
primitiveData2 = PrimitiveData('Temperature is too high!')
print(primitiveData2)

# Create sequence data with two elements, using the Add method.
sequenceData2 = SequenceData()
sequenceData2.Elements.Add(opaqueData1)
sequenceData2.Elements.Add(opaqueData2)
print(sequenceData2)

# Create structured data with two members, using the Add method.
structuredData2 = StructuredData()
structuredData2.Add('Message', primitiveData2)
structuredData2.Add('Status', enumerationData)
print(structuredData2)

# Create union data.
unionData1 = UnionData('DoubleField', primitiveData1)
print(unionData1)

print()
print('Finished.')
' This example shows different ways of constructing generic data.
'
' Find all latest examples here: https://opclabs.doc-that.com/files/onlinedocs/OPCLabs-OpcStudio/Latest/examples.html .

Imports System
Imports System.Collections
Imports OpcLabs.BaseLib.DataTypeModel

Namespace ComplexData._GenericData

    Friend Class _Construction

        Public Shared Sub Main1()
            ' Create enumeration data with value of 1.
            Dim enumerationData = New EnumerationData(1)
            Console.WriteLine(enumerationData)

            ' Create opaque data from an array of 2 bytes, specifying its size as 15 bits.
            Dim opaqueData1 = New OpaqueData(New Byte() {170, 85}, sizeInBits:=15)
            Console.WriteLine(opaqueData1)

            ' Create opaque data from a bit array.
            Dim opaqueData2 = New OpaqueData(New BitArray(New Boolean() {False, True, False, True, False}))
            Console.WriteLine(opaqueData2)

            ' Create primitive data with System.Double value of 180.0.
            Dim primitiveData1 = New PrimitiveData(180)
            Console.WriteLine(primitiveData1)

            ' Create primitive data with System.String value.
            Dim primitiveData2 = New PrimitiveData("Temperature is too high!")
            Console.WriteLine(primitiveData2)

            ' Create sequence data with two elements, using collection initializer syntax.
            Dim sequenceData1 = New SequenceData() From {opaqueData1, opaqueData2}
            Console.WriteLine(sequenceData1)

            ' Create the same sequence data, using the Add method.
            Dim sequenceData2 = New SequenceData
            sequenceData2.Elements.Add(opaqueData1)
            sequenceData2.Elements.Add(opaqueData2)
            Console.WriteLine(sequenceData2)

            ' Create the same sequence data, using an array (an enumerable) of its elements.
            Dim sequenceData3 = New SequenceData(New GenericDataCollection(New OpaqueData() {opaqueData1, opaqueData2}))
            Console.WriteLine(sequenceData3)

            ' Create structured data with two members, using collection initializer syntax.
            Dim structuredData1 = New StructuredData() From { _
                {"Message", primitiveData2}, _
                {"Status", enumerationData}}
            Console.WriteLine(structuredData1)

            ' Create the same structured data using the Add method.
            Dim structuredData2 = New StructuredData()
            structuredData2.Add("Message", primitiveData2)
            structuredData2.Add("Status", enumerationData)
            Console.WriteLine(structuredData2)
        End Sub
    End Class
End Namespace
// This example shows different ways of constructing generic data.
//
// Find all latest examples here : https://opclabs.doc-that.com/files/onlinedocs/OPCLabs-OpcStudio/Latest/examples.html .

class procedure _Construction.Main;
var
  ByteArray1, ByteArray2: OleVariant;
  EnumerationData: _EnumerationData;
  OpaqueData1, OpaqueData2: _OpaqueData;
  PrimitiveData1, PrimitiveData2: _PrimitiveData;
  SequenceData1: _SequenceData;
  StructuredData1: _StructuredData;
begin
  // Create enumeration data with value of 1.
  EnumerationData := CoEnumerationData.Create;
  EnumerationData.Value := 1;
  WriteLn(EnumerationData.ToString);

  // Create opaque data from an array of 2 bytes, specifying its size as 15 bits.
  OpaqueData1 := CoOpaqueData.Create;
  ByteArray1 := VarArrayCreate([0, 1], varVariant);
  ByteArray1[0] := $AA;
  ByteArray1[1] := $55;
  OpaqueData1.SetByteArrayValue(ByteArray1, 15);
  WriteLn(OpaqueData1.ToString);
  // Create opaque data from an array of 1 bytes, specifying its size as 5 bits.
  OpaqueData2 := CoOpaqueData.Create;
  ByteArray2 := VarArrayCreate([0, 0], varVariant);
  ByteArray2[0] := $A;
  OpaqueData2.SetByteArrayValue(ByteArray2, 5);
  WriteLn(OpaqueData2.ToString);

  // Create primitive data with System.Double value of 180.0.
  PrimitiveData1 := CoPrimitiveData.Create;
  PrimitiveData1.Value := 180.0;
  WriteLn(PrimitiveData1.ToString);

  // Create primitive data with System.String value.
  PrimitiveData2 := CoPrimitiveData.Create;
  PrimitiveData2.Value := 'Temperature is too high!';
  WriteLn(PrimitiveData2.ToString);

  // Create sequence data with two elements, using the Add method.
  SequenceData1 := CoSequenceData.Create;
  SequenceData1.Elements.Add(OpaqueData1);
  SequenceData1.Elements.Add(OpaqueData2);
  WriteLn(SequenceData1.ToString);

  // Create structured data with two members, using the Add method.
  StructuredData1 := CoStructuredData.Create;
  StructuredData1.Add('Message', PrimitiveData2);
  StructuredData1.Add('Status', EnumerationData);
  WriteLn(StructuredData1.ToString);

  VarClear(ByteArray2);
  VarClear(ByteArray1);
end;
// This example shows different ways of constructing generic data.
//
// Find all latest examples here : https://opclabs.doc-that.com/files/onlinedocs/OPCLabs-OpcStudio/Latest/examples.html .

// Create enumeration data with value of 1.
$EnumerationData = new COM("OpcLabs.BaseLib.DataTypeModel.EnumerationData");
$EnumerationData->Value = 1;
printf("%s\n", $EnumerationData);

// Create opaque data from an array of 2 bytes, specifying its size as 15 bits.
$OpaqueData1 = new COM("OpcLabs.BaseLib.DataTypeModel.OpaqueData");
$ByteArray1[0] = 0xAA;
$ByteArray1[1] = 0x55;
$OpaqueData1->SetByteArrayValue($ByteArray1, 15);
printf("%s\n", $OpaqueData1);

// Create opaque data from a bit array.
$OpaqueData2 = new COM("OpcLabs.BaseLib.DataTypeModel.OpaqueData");
$BitArray = $OpaqueData2->Value;
$BitArray->Length = 5;
$BitArray->Set(0, false);
$BitArray->Set(1, true);
$BitArray->Set(2, false);
$BitArray->Set(3, true);
$BitArray->Set(4, false);
printf("%s\n", $OpaqueData2);

// Create primitive data with System.Double value of 180.0.
$PrimitiveData1 = new COM("OpcLabs.BaseLib.DataTypeModel.PrimitiveData");
$PrimitiveData1->Value = 180.0;
printf("%s\n", $PrimitiveData1);

// Create primitive data with System.String value.
$PrimitiveData2 = new COM("OpcLabs.BaseLib.DataTypeModel.PrimitiveData");
$PrimitiveData2->Value = "Temperature is too high!";
printf("%s\n", $PrimitiveData2);

// Create sequence data with two elements, using the Add method.
$SequenceData1 = new COM("OpcLabs.BaseLib.DataTypeModel.SequenceData");
$SequenceData1->Elements->Add($OpaqueData1);
$SequenceData1->Elements->Add($OpaqueData2);
printf("%s\n", $SequenceData1);

// Create structured data with two members, using the Add method.
$StructuredData1 = new COM("OpcLabs.BaseLib.DataTypeModel.StructuredData");
$StructuredData1->Add("Message", $PrimitiveData2);
$StructuredData1->Add("Status", $EnumerationData);
printf("%s\n", $StructuredData1);
Rem This example shows different ways of constructing generic data.
Rem
Rem Find all latest examples here : https://opclabs.doc-that.com/files/onlinedocs/OPCLabs-OpcStudio/Latest/examples.html .

Option Explicit

' Create enumeration data with value of 1.
Dim EnumerationData: Set EnumerationData = CreateObject("OpcLabs.BaseLib.DataTypeModel.EnumerationData")
EnumerationData.Value = 1
WScript.Echo EnumerationData

' Create opaque data from an array of 2 bytes, specifying its size as 15 bits.
Dim OpaqueData1: Set OpaqueData1 = CreateObject("OpcLabs.BaseLib.DataTypeModel.OpaqueData")
OpaqueData1.SetByteArrayValue Array(&HAA, &H55), 15
WScript.Echo OpaqueData1

' Create opaque data from a bit array.
Dim OpaqueData2: Set OpaqueData2 = CreateObject("OpcLabs.BaseLib.DataTypeModel.OpaqueData")
Dim BitArray: Set BitArray = OpaqueData2.Value
BitArray.Length = 5
BitArray(0) = False
BitArray(1) = True
BitArray(2) = False
BitArray(3) = True
BitArray(4) = False
WScript.Echo OpaqueData2

' Create primitive data with System.Double value of 180.0.
Dim PrimitiveData1: Set PrimitiveData1 = CreateObject("OpcLabs.BaseLib.DataTypeModel.PrimitiveData")
PrimitiveData1.Value = 180.0
WScript.Echo PrimitiveData1

' Create primitive data with System.String value.
Dim PrimitiveData2: Set PrimitiveData2 = CreateObject("OpcLabs.BaseLib.DataTypeModel.PrimitiveData")
PrimitiveData2.Value = "Temperature is too high!"
WScript.Echo PrimitiveData2

' Create sequence data with two elements, using the Add method.
Dim SequenceData1: Set SequenceData1 = CreateObject("OpcLabs.BaseLib.DataTypeModel.SequenceData")
SequenceData1.Elements.Add OpaqueData1
SequenceData1.Elements.Add OpaqueData2
WScript.Echo SequenceData1

' Create structured data with two members, using the Add method.
Dim StructuredData1: Set StructuredData1 = CreateObject("OpcLabs.BaseLib.DataTypeModel.StructuredData")
StructuredData1.Add "Message", PrimitiveData2
StructuredData1.Add "Status", EnumerationData
WScript.Echo StructuredData1
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