Use .NET nullable value types
- Last Updated: March 30, 2020
- 2 minute read
- OpenEdge
- Version 12.2
- Documentation
In ProxyGen, if you specify that a supported value type
parameter or return value can have the Unknown value (?), and
you have chosen the option to use nullable value types, the method
is defined with the corresponding nullable type; otherwise the parameter
is defined using the .NET value type.
The following is an example of a C# proxy method signature that
does not support Unknown value (?) for the first and third parameters:
|
The following is an example of a C# proxy method that allows
all the parameter values to have Unknown value (?), using nullable
value types where necessary:
|
.NET nullable types are represented using the following syntax:
Syntax
|
Where ValueType is a built-in .NET value
type that has a corresponding nullable type, and ? identifies
the value type as nullable.
A nullable type is actually a generic structure type and the ValueType?
syntax is shorthand for the generic structure System.Nullable<ValueType>.
These forms are interchangeable. However, the .NET Open Client
uses the ValueType? form to represent nullable
types in a proxy.
The following table shows the mapping between ABL primitive types and the corresponding .NET nullable value types.
| This ABL primitive type... | Maps to this .NET nullable type... |
|---|---|
|
|
|
|
|
|
|
|
|
|
|
|
To set the value of an object with a nullable value type (nullable
type object), you can directly assign it a value as if you were
setting an object with the corresponding value type, and you can
also assign it directly to null. When reading the
value of a nullable type object, you need to know if the object
is null (has no value) in order to avoid throwing
an exception when reading its value, which is undefined when set
to null. Also, to read the value of a nullable type
object, you must access a property of the object that contains the
value. Therefore, the System.Nullable<ValueType> generic
structure supports the following read-only properties for nullable
type objects:
-
HasValue— A property of typeSystem.Booleanthat istrueif the object has a value and isfalseotherwise (the object is set tonull) -
Value— A property of type ValueType that returns the object's value ifHasValueistrue, and throws aSystem.InvalidOperationExceptionexception ifHasValueisfalse
You can also directly test a nullable type object for null using
the == and != operators, and directly
access the value of a non-null nullable type object.
For example, within a C# method, you can use nullable type objects as follows:
|
For more information on the characteristics of nullable value types, including additional methods and C# operators that they support, see the Microsoft .NET documentation on using nullable types in C#.