COBOL provides predefined usages corresponding to many of the most commonly used .NET types. These names can be used when declaring a data item, and anywhere a type specifier is expected - see Specifying Type Names. For example:
01 anInteger binary-long.
.NET or JVM COBOL | .NET type | JVM type | C# keyword | Description |
---|---|---|---|---|
Integer types: | ||||
binary-char | System.SByte | byte | sbyte | An 8-bit signed integer
Not CLS-compliant |
binary-char unsigned | System.Byte | byte | An 8-bit unsigned integer | |
binary-short | System.Int16 | short | short | A 16-bit signed integer
Not CLS-compliant |
binary-short unsigned | System.UInt16 | ushort | A 16-bit unsigned integer | |
binary-long | System.Int32 | int | int | A 32-bit signed integer |
binary-long unsigned | System.UInt32 | uint | A 32-bit unsigned integer
Not CLS-compliant. |
|
binary-double | System.Int64 | long | long | A 64-bit signed integer |
binary-double unsigned | System.UInt64 | ulong | A 64-bit unsigned integer
Not CLS-compliant |
|
Floating point types: | ||||
float-short | System.Single | float | float | A single-precision (32-bit) floating-point number |
float-long | System.Double | double | double | A double-precision (64-bit) floating-point number |
Logical types: | ||||
condition-value | System.Boolean | boolean | bool | A boolean value (true or false) |
Other types: | ||||
character | System.Char | char | char | A unicode (16-bit) character |
decimal | System.Decimal | see JVMDECIMAL. | decimal | A 96-bit decimal value |
Class objects: | ||||
object | System.Object | java.lang.Object | object | The root of the object hierarchy |
string | System.String | java.lang.String | string | An immutable, fixed-length string of Unicode characters |
Collection types: | ||||
list | System.Collections.Generic.IList<T> | java.util.List | An ordered collection of items | |
dictionary | System.Collections.Generic.IDictionary<TKey, TValue> | java.util.Map | A mapping of keys to values | |
set | System.Collections.Generic.ISet<T> | java.util.Set<T> | An unordered collection of items |
Further managed types can be specified in COBOL using the syntax TYPE classname in the data item definition. For example:
01 objMyType type MyType.
All the COBOL data types in the table above and any further .NET types that you define are mapped onto the corresponding native type (.NET or JVM), but they must conform to the following rules. They:
Any COBOL data item that does not follow these rules, or is of any other category, is not considered to be a native type (.NET or JVM) and is allocated by the Compiler within an internally managed byte array. COBOL pointer data items always point into one of these byte arrays.
If your code references a type without specifying the namespace it belongs to, the Compiler first attempts to resolve this to a type formed by adding the namespace of the current class to the type name. For example:
$set ilusing"System" class-id MyNamespace.EventHandler. 01 o type EventHandler. end class.
In the example above 01 o type EventHandler. resolves to MyNamespace.EventHandler and not to System.EventHandler.
If no such type exists then the Compiler tries to resolve the unspecified type in the order listed below:
.NET COBOL distinguishes between:
All value types can be turned into reference types by a process known as boxing. For example, you can set an object reference to a value type, such as a binary-long.
Boxing happens automatically when needed, for example when a value type is passed as a parameter to a method that expects an object as a parameter.
You can box explicitly by assigning a value type to a generic object (such as System.Object in .NET COBOL or java.lang.Object in JVM COBOL).
You can unbox value types to restore the original value type.
When you specify TYPE classname in the data item definition:
During boxing, the system copies the value into the object heap and returns a reference to it. When the reference is no longer active (because nothing in the program is holding on to it), the space in the object heap will eventually be regained by the garbage collector.
See the ValueTypes sample, available from Start > All Programs > Micro Focus Enterprise Developer > Samples > Visual COBOL Samples, under COBOL for .NET.