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:

Parents
  • 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.

Reply
  • 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.

Children
No Data