CounterCreationDataCollection.AddRange Method

Definition

Adds multiple CounterCreationData instances to the collection.

Overloads

AddRange(CounterCreationData[])

Adds the specified array of CounterCreationData instances to the collection.

AddRange(CounterCreationDataCollection)

Adds the specified collection of CounterCreationData instances to the collection.

AddRange(CounterCreationData[])

Adds the specified array of CounterCreationData instances to the collection.

public:
 void AddRange(cli::array <System::Diagnostics::CounterCreationData ^> ^ value);
public void AddRange (System.Diagnostics.CounterCreationData[] value);
member this.AddRange : System.Diagnostics.CounterCreationData[] -> unit
Public Sub AddRange (value As CounterCreationData())

Parameters

value
CounterCreationData[]

An array of CounterCreationData instances to append to the existing collection.

Exceptions

value is null.

Applies to

AddRange(CounterCreationDataCollection)

Adds the specified collection of CounterCreationData instances to the collection.

public:
 void AddRange(System::Diagnostics::CounterCreationDataCollection ^ value);
public void AddRange (System.Diagnostics.CounterCreationDataCollection value);
member this.AddRange : System.Diagnostics.CounterCreationDataCollection -> unit
Public Sub AddRange (value As CounterCreationDataCollection)

Parameters

value
CounterCreationDataCollection

A collection of CounterCreationData instances to append to the existing collection.

Exceptions

value is null.

Examples

The following example demonstrates how to use the AddRange(CounterCreationDataCollection) method overload to add CounterCreationData objects from one CounterCreationDataCollection to another CounterCreationDataCollection.

#using <System.dll>

using namespace System;
using namespace System::Diagnostics;
int main()
{
   PerformanceCounter^ myCounter;
   try
   {
      String^ myCategoryName;
      int numberOfCounters;
      Console::Write( "Enter the number of counters : " );
      numberOfCounters = Int32::Parse( Console::ReadLine() );
      array<CounterCreationData^>^myCounterCreationData = gcnew array<CounterCreationData^>(numberOfCounters);
      for ( int i = 0; i < numberOfCounters; i++ )
      {
         Console::Write( "Enter the counter name for {0} counter : ", i );
         myCounterCreationData[ i ] = gcnew CounterCreationData;
         myCounterCreationData[ i ]->CounterName = Console::ReadLine();
      }
      CounterCreationDataCollection^ myCounterCollection = gcnew CounterCreationDataCollection( myCounterCreationData );
      Console::Write( "Enter the category Name : " );
      myCategoryName = Console::ReadLine();

      // Check if the category already exists or not.
      if (  !PerformanceCounterCategory::Exists( myCategoryName ) )
      {
         CounterCreationDataCollection^ myNewCounterCollection = gcnew CounterCreationDataCollection;

         // Add the 'CounterCreationDataCollection' to 'CounterCreationDataCollection' Object*.
         myNewCounterCollection->AddRange( myCounterCollection );
         PerformanceCounterCategory::Create( myCategoryName, "Sample Category", myNewCounterCollection );
         for ( int i = 0; i < numberOfCounters; i++ )
         {
            myCounter = gcnew PerformanceCounter( myCategoryName,myCounterCreationData[ i ]->CounterName,"",false );

         }
         Console::WriteLine( "The list of counters in CounterCollection are: " );
         for ( int i = 0; i < myNewCounterCollection->Count; i++ )
            Console::WriteLine( "Counter {0} is '{1}'", i + 1, myNewCounterCollection[ i ]->CounterName );
      }
      else
      {
         Console::WriteLine( "The category already exists" );
      }
   }
   catch ( Exception^ e ) 
   {
      Console::WriteLine( "Exception: {0}.", e->Message );
   }
}
using System;
using System.Diagnostics;

public class CounterDataCollectionExample
{
    public static void Main()
    {
        try
        {

            string myCategoryName;
            int numberOfCounters;
            Console.Write("Enter the number of counters : ");
            numberOfCounters = int.Parse(Console.ReadLine());
            CounterCreationData[] myCounterCreationData =
               new CounterCreationData[numberOfCounters];
            for (int i = 0; i < numberOfCounters; i++)
            {
                Console.Write("Enter the counter name for {0} counter : ", i);
                myCounterCreationData[i] = new CounterCreationData();
                myCounterCreationData[i].CounterName = Console.ReadLine();
            }
            CounterCreationDataCollection myCounterCollection =
               new CounterCreationDataCollection(myCounterCreationData);
            Console.Write("Enter the category Name : ");
            myCategoryName = Console.ReadLine();
            // Check if the category already exists or not.
            if (!PerformanceCounterCategory.Exists(myCategoryName))
            {
                CounterCreationDataCollection myNewCounterCollection =
                   new CounterCreationDataCollection();
                // Add the 'CounterCreationDataCollection' to 'CounterCreationDataCollection' object.
                myNewCounterCollection.AddRange(myCounterCollection);

                PerformanceCounterCategory.Create(myCategoryName, "Sample Category",
                PerformanceCounterCategoryType.SingleInstance, myNewCounterCollection);

                Console.WriteLine("The list of counters in CounterCollection are: ");
                for (int i = 0; i < myNewCounterCollection.Count; i++)
                    Console.WriteLine("Counter {0} is '{1}'", i + 1, myNewCounterCollection[i].CounterName);
            }
            else
            {
                Console.WriteLine("The category already exists");
            }
        }
        catch (Exception e)
        {
            Console.WriteLine("Exception: {0}.", e.Message);
            return;
        }
    }
}

Imports System.Diagnostics

Public Class CounterDataCollectionExample
   Private Shared myCounter As PerformanceCounter
   Public Shared Sub Main()
      Try

         Dim myCategoryName As String
         Dim numberOfCounters As Integer
         Console.Write("Enter the number of counters : ")
         numberOfCounters = Integer.Parse(Console.ReadLine())
         Dim myCounterCreationData(numberOfCounters-1) As CounterCreationData
         Dim i As Integer
         For i = 0 To numberOfCounters - 1
            Console.Write("Enter the counter name for {0} counter : ", i)
            myCounterCreationData(i) = New CounterCreationData()
            myCounterCreationData(i).CounterName = Console.ReadLine()
         Next i
         Dim myCounterCollection As New CounterCreationDataCollection(myCounterCreationData)
         Console.Write("Enter the category Name : ")
         myCategoryName = Console.ReadLine()
         ' Check if the category already exists or not.
         If Not PerformanceCounterCategory.Exists(myCategoryName) Then
            Dim myNewCounterCollection As New CounterCreationDataCollection()
            ' Add the 'CounterCreationDataCollection' to 'CounterCreationDataCollection' object.
            myNewCounterCollection.AddRange(myCounterCollection)

                PerformanceCounterCategory.Create(myCategoryName, "Sample Category", _
                       PerformanceCounterCategoryType.SingleInstance, myNewCounterCollection)

            Console.WriteLine("The list of counters in CounterCollection are: ")

            For i = 0 To myNewCounterCollection.Count - 1
               Console.WriteLine("Counter {0} is '{1}'", i + 1, _
                                             myNewCounterCollection(i).CounterName)
            Next i
         Else
            Console.WriteLine("The category already exists")
         End If

      Catch e As Exception
         Console.WriteLine("Exception: {0}.", e.Message)
         Return
      End Try
   End Sub
End Class

Applies to