Absent Member.
Absent Member.

Type Casting

Dear community,

I need your help in order to cast a type 'dynamically', using the GetType() method to set the target Type instead of hardcoding the Type.

Below you can find a simple sample code.

procedure division using inObj as object.
     set (inObj as type Class2)::c1x1 to "test".                      <- OK but hardcoded
     set (inObj as type inObj::GetType)::c1x1 to "test".        <- Error COBCH0813

When I try to compile (VC v2.2u2) the above code, I get the error "COBCH0813 : Feature not yet supported when compiling for .NET".

Does anyone have any suggestion or alternative solution?

Tags (1)
1 Reply
Micro Focus Expert
Micro Focus Expert

RE: Type Casting

The Visual COBOL forum would be a better place for this question. (Unfortunately the forums aren't organized in a terribly obvious manner.)

In any case, though, you should always preface a question like this by stating which environment you're compiling for - in this case .NET - because it makes a big difference when you're talking about extensions to the standard COBOL language.

All that said, I don't believe you can currently do this without using reflection - though someone who knows all the latest compiler features might correct me on that. Here's an example using reflection that works for .NET Managed COBOL under ED 2.2 Update 2.

      $set sourceformat(variable)
      $set ilusing"System"
      $set ilusing"System.Reflection"

       class-id testcb.

       method-id Main (args as string occurs any) static.
           declare testObj = new Class2
           invoke self::TestCast(testObj)
           display 'Value of testObj::c1x1 is "' testObj::c1x1 '"'
       end method Main.

       method-id TestCast (inObj as object) static.
           declare objType = inObj::GetType
           declare c1x1Prop = objType::GetProperty("c1x1")
           invoke c1x1Prop::SetValue(inObj, "test")
       end method TestCast.

       end class testcb.

       class-id Class2.
       1 val string property as "c1x1".
       end class Class2.

As you can see, method TestCast uses System.Type and System.Reflection.PropertyInfo to get dynamic access to a property named "c1x1", which it then sets to a string. Generally you'd want exception handling (try/catch) somewhere around this sort of thing, since you're bypassing static type checking.

The opinions expressed above are the personal opinions of the authors, not of Micro Focus. By using this site, you accept the Terms of Use and Rules of Participation. Certain versions of content ("Material") accessible here may contain branding from Hewlett-Packard Company (now HP Inc.) and Hewlett Packard Enterprise Company. As of September 1, 2017, the Material is now offered by Micro Focus, a separately owned and operated company. Any reference to the HP and Hewlett Packard Enterprise/HPE marks is historical in nature, and the HP and Hewlett Packard Enterprise/HPE marks are the property of their respective owners.