Training
Module
Choose the correct data type in your C# code - Training
Choose the correct data type for your code from several basic types used in C#.
This browser is no longer supported.
Upgrade to Microsoft Edge to take advantage of the latest features, security updates, and technical support.
In MOF, numbers are digits that describe numerical values. MOF provides a variety of data types that translate into Automation, and also allows those numbers to be in different formats. The following table lists the numeric values that MOF supports.
Data type | Automation type | Description |
---|---|---|
sint8 | VT_I2 | Signed 8-bit integer. |
sint16 | VT_I2 | Signed 16-bit integer. |
sint32 | VT_I4 | Signed 32-bit integer. |
sint64 | VT_BSTR | Signed 64-bit integer in string form. This type follows hexadecimal or decimal format according to the American National Standards Institute (ANSI) C rules. |
real32 | VT_R4 | 4-byte floating-point value that follows the Institute of Electrical and Electronics Engineers, Inc. (IEEE) standard. |
real64 | VT_R8 | 8-byte floating-point value that follows the IEEE standard. |
uint8 | VT_UI1 | Unsigned 8-bit integer. |
uint16 | VT_I4 | Unsigned 16-bit integer. |
uint32 | VT_I4 | Unsigned 32-bit integer. |
uint64 | VT_BSTR | Unsigned 64-bit integer in string form. This type follows hexadecimal or decimal format according to ANSI C rules. |
Although flexible, MOF code does encounter some changes when dealing with Automation:
You must encode 64-bit integers as strings.
Automation does not support a 64-bit integral type.
Automation types do not always correspond in bit size to MOF data types.
For example, Automation uses VT_I4 to return an unsigned 16-bit value. This discrepancy exists because of sign-extension problems. If Automation used VT_I2 instead of VT_I4, 65,536 would appear to be the value 1, causing type and range problems. Similarly, Automation represents the uint32 type as VT_I4 because there exists no larger integer type to contain uint32.
You do not need to change any representation for 8-bit numeral types.
Automation supports VT_UI1, an unsigned 8-bit type.
MOF supports long constants. You declare a long constant using a simple series of digits with an optional negative sign. A long constant cannot exceed the size of the variable that is declared to hold it. Some examples of long constants are 1000 and 12310.
MOF also supports alternate numerical formats. The following table lists the special characters you must use to describe hexadecimal, binary, and octal constants.
Constant | Special character | Example |
---|---|---|
Decimal |
None |
val = 65 |
Hexadecimal |
0x prefix |
val = 0x41 |
Octal |
Leading 0 |
val = 0101 |
Binary |
Trailing B |
val = 1000001B |
You can use a floating-point constant to represent scientific notation as well as fractions, as shown next:
3.14
-3.14
-1.2778E+02
WMI considers floating-point constants as VT_R8 types for Automation.
The following example describes class and instance declarations that illustrate how to use each of the numeric data types to set properties:
Class NumericDataClass
{
[key] uint8 Duint8;
SInt8 Dchar;
UInt16 Dtword;
Sint16 Dinst16;
UInt32 Ddword;
Sint32 Dinst1;
Sint32 Dinst2;
Sint32 Dinst3;
Sint32 Dinst4;
Sint32 Dinst5;
Real32 Dfloat;
Real64 Ddouble1;
Real64 Ddouble2;
};
instance of NumericDataClass
{
Duint8 = 122;
Dchar = -128;
Dtword = 30;
Dinst16 = -1445;
Ddword = 6987777;
Dinst1 = -455589;
Dinst2 = 23;
Dinst3 = 03; // Base 8
Dinst4 = 0xFe; // Base 16
Dinst5 = 11b; // Base 2
Dfloat = 3.1478;
Ddouble1 = 99987.3654;
Ddouble2 = 2.3e-2;
};
Training
Module
Choose the correct data type in your C# code - Training
Choose the correct data type for your code from several basic types used in C#.