Connectivity Software User's Guide and Reference
Add(String,SparkplugMetricElement) Method
Example 



OpcLabs.EasySparkplug Assembly > OpcLabs.EasySparkplug Namespace > SparkplugPayload Class > Add Method : Add(String,SparkplugMetricElement) Method
The Sparkplug metric name.

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

The Sparkplug metric element.

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

Adds a Sparkplug metric to the payload, specifying the metric name and metric element (metric data and metadata).
Syntax
'Declaration
 
Public Overloads Sub Add( _
   ByVal metricName As String, _
   ByVal metricElement As SparkplugMetricElement _
) 
'Usage
 
Dim instance As SparkplugPayload
Dim metricName As String
Dim metricElement As SparkplugMetricElement
 
instance.Add(metricName, metricElement)

Parameters

metricName
The Sparkplug metric name.

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

metricElement
The Sparkplug metric element.

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

Exceptions
ExceptionDescription

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

This overload is necessary for the collection initializer support.

 

Sparkplug is a trademark of Eclipse Foundation, Inc. "MQTT" is a trademark of the OASIS Open standards consortium. Other related terms are trademarks of their respective owners. Any use of these terms on this site is for descriptive purposes only and does not imply any sponsorship, endorsement or affiliation.

Example
// This example shows how to publish a payload with multiple command metrics for a given edge node.
//
// In order to publish or observe messages for this example, start the SparkplugEdgeNodeConsoleDemo program first.
//
// Find all latest examples here: https://opclabs.doc-that.com/files/onlinedocs/OPCLabs-ConnectivityStudio/Latest/examples.html .
// Sparkplug examples in C# on GitHub: https://github.com/OPCLabs/Examples-ConnectivityStudio-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.EasySparkplug;
using OpcLabs.EasySparkplug.OperationModel;

namespace SparkplugDocExamples.Consumer._EasySparkplugConsumer
{
    class PublishEdgeNodePayload
    {
        public static void Overload1()
        {
            // Note that the default port for the "mqtt" scheme is 1883.
            var hostDescriptor = new SparkplugHostDescriptor("mqtt://localhost");

            // Instantiate the consumer object.
            var consumer = new EasySparkplugConsumer();

            // Create the payload with multiple command metrics.
            var payload = new SparkplugPayload
            {
                { "Simple", new SparkplugMetricData(42) },
                { "Simple2", new SparkplugMetricData(43) }
            };
            
            Console.WriteLine("Publishing...");
            try
            {
                consumer.PublishEdgeNodePayload(hostDescriptor, "easyGroup", "easySparkplugDemo", payload);
            }
            catch (SparkplugException sparkplugException)
            {
                Console.WriteLine($"*** Failure: {sparkplugException.GetBaseException().Message}");
                return;
            }

            Console.WriteLine("Finished.");
        }
    }
}
' This example shows how to publish a payload with multiple command metrics for a given edge node.
'
' In order to publish or observe messages for this example, start the SparkplugEdgeNodeConsoleDemo program first.
'
' Find all latest examples here: https://opclabs.doc-that.com/files/onlinedocs/OPCLabs-ConnectivityStudio/Latest/examples.html .
' Sparkplug examples in C# on GitHub: https://github.com/OPCLabs/Examples-ConnectivityStudio-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.

Imports OpcLabs.EasySparkplug
Imports OpcLabs.EasySparkplug.OperationModel

Namespace Global.SparkplugDocExamples.Consumer._EasySparkplugConsumer
    Class PublishEdgeNodePayload
        Public Shared Sub Overload1()
            ' Note that the default port for the "mqtt" scheme is 1883.
            Dim hostDescriptor = New SparkplugHostDescriptor("mqtt://localhost")

            ' Instantiate the consumer object.
            Dim consumer = New EasySparkplugConsumer()

            ' Create the payload with multiple command metrics.
            Dim payload = New SparkplugPayload From
            {
                {"Simple", New SparkplugMetricData(42)},
                {"Simple2", New SparkplugMetricData(43)}
            }

            Console.WriteLine("Publishing...")
            Try
                consumer.PublishEdgeNodePayload(hostDescriptor, "easyGroup", "easySparkplugDemo", payload)
            Catch sparkplugException As SparkplugException
                Console.WriteLine($"*** Failure: {sparkplugException.GetBaseException().Message}")
                Return
            End Try

            Console.WriteLine("Finished.")
        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