NETJVM 

Data Types in Managed COBOL

COBOL provides predefined usages corresponding to many of the most commonly used managed code 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.

Predefined Managed Types

Managed COBOL (.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

Referencing Types Using the TYPE Keyword

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 managed types that you define are mapped onto the corresponding native type (.NET or JVM), but they must conform to the following rules. They:

  • Must be declared at 01 (or 77) level
  • Must have no REDEFINES or RENAMES
  • Must never be the subject of reference modification
  • Must never be the subject of the ADDRESS OF clause
  • May use format 3 of the OCCURS clause to declare an array

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.

Resolving Types

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:

  1. if the current compilation includes a single type with this name and with no namespace, the Compiler resolves it to this type.
  2. if a referenced assembly includes a single type with the same name and with no namespace, the Compiler resolves to this type.
  3. if the current compilation includes a single type with this name and with the same namespace as the current type, the Compiler resolves it to this type
  4. if a referenced assembly includes a single type with this name and with the same namespace as the current type, the Compiler resolves it to this type
  5. if among the namespaces specified with the ILUSING directives a single type with the same name, the Compiler resolves to this type.
  6. if among the namespaces specified with the ILUSING directives there are more than one namespaces that include the same type, the Compiler emits an error.

Value Types, Reference Types and Boxing

Managed COBOL distinguishes between:

  • Value types (such as binary-long and System.DateTime in .NET). Value type data items contain the actual data. For example, a COBOL binary-long data item contains a 32 bit integer.
  • Reference types, which are allocated on the object heap. Reference type data items hold a reference into the object heap. Reference types are under the management of the garbage collector.

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:

  • If classname represents a reference type, you get a reference type object.
  • If classname represents a value type, you get a value type object.

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 Visual COBOL > Samples, under COBOL for .NET.