Skip to content

Working with numeric data types

qingxiao_ren edited this page Sep 5, 2018 · 1 revision

Working with numeric data types (Counter32, Counter64, Gauge32 and TimeTicks)

Simple Network Management Protocol defines a number of numeric data types. They can be summarized as:

Signed 32-bit integer derived data types:

Unsigned 32-bit integer derived data type:

Unsigned 64-bit integer derived data type:

TimeTicks SMI value type is also represented as a unsigned 32-bit integer but it manages a time value and is discussed later in this document.

Because of the easy mapping between c# data types and SNMP SMI numeric variable types, mapping between the classes and values they represent is as follows:

c# type Snmp#Net class
Int32 Counter32, Integer32
UInt32 Gauge32, UInteger32
UInt64 Counter64

Each of the numeric data type classes stores internally the specific value type it represents. This value is accessible from outside the class using Value property. Additionally, each numeric value class has an implicit conversion defined to the c# value type it stores.

What this means is that you can perform same operations with the Snmp#Net numeric value class as you can with the base type it represents.

Here is a basic example of some of the available operations:

Counter32 c32 = new Counter32(Int32.MaxValue);
Gauge32 g32 = new Gauge32();
g32.Value = UInt32.MaxValue;
Counter64 c64 = new Counter64("1234");
Console.WriteLine("Counter32 {0} Gauge32 {1} Counter64 {2}", c32, g32, c64);
// Prints: Counter32 2147483647 Gauge32 4294967295 Counter64 1234
Console.WriteLine("Counter32 - 10 = {0}", c32 - 10);
// Prints: Counter32 - 10 = 2147483637
UInt64 result = c64 + 100;
// Prints: Counter64 + 100 = 1334
Console.WriteLine("Counter64 + 100 = {0}", result);

As you can see from the above example, you can use all Snmp#Net numeric types as regular c# numeric variables.

In the example you can also see that multiple constructors exist. Each class can be initialized with the default value of 0 by calling the base constructor with no arguments. Argument can be value of the specific type represented by the class, a string representation of a numeric value or another instance of the value class for the copy constructor.

Once initialized, each class value can be retrieved or set using the Value property or set using Set() methods.

All numeric value classes support comparison methods Equals() and CompareTo() and comparison operators == and !=. Through implicit conversion to base c# data types, each numeric type class supports all mathematical operations available for the base types.

When polling Counter32 or Counter64 MIB values you have to be careful to check for counter loop which happens when counter reaches the maximum value it can store and loops back to zero. Both 32-bit and 64-bit classes provide static methods Counter32.Diff(Counter32,Counter32) and Counter64.Diff(Counter64,Counter64) that will calculate the difference between the two counter value with counter loop taken into account.

TimeTicks value type

TimeTicks SMI value is a elapsed time value in 100milisecond periods (10 periods per second). It is internally represented as an unsigned 32-bit integer and is derived from the UInteger32 Snmp#Net numeric value class. Through inheritance, TimeTicks class has all the functionality described above related to numeric data type classes.

TimeTicks Snmp#Net class provides a few operations that are specific to the TimeTicks SMI data type. These are:

TimeTicks class has a definition for the explicit conversion to the TimeSpan class to allow for easier manipulation of time periods:

TimeTicks ts = new TimeTicks(1200);
Console.WriteLine("TimeTicks: {0}", ts.ToString());
// Prints: TimeTicks: 0d 0h 0m 12s 0ms
TimeSpan tspan = (TimeSpan)ts;
Console.WriteLine("TimeSpan: {0}", tspan.ToString());
// Prints: TimeSpan: 00:00:12

You can retrieve the total number of milliseconds represented by the class using TimeTicks.Miliseconds property.

Finally, TimeTicks.ToString() will return the time ticks value string representation demonstrated in the example above.

All other methods and properties are the same as in all other unsigned 32-bit integer derived SMI data type classes.