Quantcast
Channel: Visual COBOL - Forum - Recent Threads
Viewing all articles
Browse latest Browse all 4356

RE: Type Casting

$
0
0

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.


Viewing all articles
Browse latest Browse all 4356

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>