Updating .NET Properties on Value Types

Unlike JADE, .NET has reference and value types (a reference type is a C# ‘class’ or a C++/CLI ‘ref class’ type, while a value type is a C# ‘struct’ or a C++/CLI ‘value class’ type). Both types, when imported from a .NET assembly, become JADE classes that are equivalent to reference types.

At run time, .NET value types are therefore converted to JADE reference types. This introduces a potential trap for the unwary. The problem is best explained by an example.

An imported .NET assembly generates the following JADE classes.

MyRectangle, which has properties x, y, width, height, and so on
(In .NET, MyRectangle is a value type)
MyClass, which has a property called 'rect' of type MyRectangle
(In .NET, MyClass is a reference type)

The following JADE logic updates the rect property of a MyClass instance in the expected way.

vars
    c: MyClass;
    r, r2: MyRectangle;
begin
    // create object
    create c;
    c.createDotNetObject();
    // set rectangle value
    r := c.rect;
    r.x := 10;
    c.rect := r;
    // refetch rect and output current value
    r2 := c.rect;
    write r2.x;                                  // output '10' as expected
end;

The following similar JADE logic fails to update the rect property of a MyClass instance.

vars
    c: MyClass;
    r, r2: MyRectangle;
begin
    // create object
    create c;
    c.createDotNetObject();
    // set rectangle value
    c.rect.x := 10;                              // does not update c
    // refetch rect and output current value
    r2 := c.rect;
    write r2.x;                                  // outputs '0'
end;

The reason the c.rect.x := 10; instruction fails to update c is that a temporary reference is created to represent the c.rect value type. It is the temporary reference that is updated with the new value for the x property. Unlike the first coding example, nothing is ever done with the temporary reference, so the original instance c remains unchanged.