Skip to main content

Problem:

If you have an object reference instance, is it possible to tell if it is a CharArray or a something else at runtime.

Resolution:

You can use the "class" method on the object and compare that to the class in class-control.

Example program is attached. The code to do this is:-

      $set ooctrl( P)

       program-id. TestClass.

      ******************************************************************

      *

      *    Author  - Micro Focus Technology Support (davs)

      *

      ******************************************************************

       special-names.

           call-convention 74 is winapi.  *> Litlinked apis

       class-control.

       *> OLE automation classes

           CharacterArray    is class "chararry"

           orderedcollection is class "ordrdcll"

           olesup            is class "olesup"

           exceptMgr         is class "exptnmgr"

           oleExceptMgr      is class "oleexpt"

           entrycallback     is class "entrycll"

           .

       working-storage section.

       01  aCharArray                 object reference.

       01  theClass                   object reference.

       01  aCollection                object reference.

       01  ws-size                    PIC S9(9) COMP-5 value 10.

       local-storage section.

       procedure division.

       start-it section.

           invoke CharacterArray "withValue" using z"Test"

               returning aCharArray

           invoke orderedcollection "ofReferences" using ws-size

               returning aCollection

           invoke aCharArray "class" returning theclass

           if theClass = orderedcollection

               display "aCharArray is an ordered collection"

           else

               display "aCharArray is NOT an ordered collection"

           end-if

           if theClass = CharacterArray

               display "aCharArray is a CharacterArray"

           else

               display "aCharArray is NOT an CharacterArray"

           end-if

           invoke aCollection "class" returning theclass

           if theClass = orderedcollection

               display "aCollection is an ordered collection"

           else

               display "aCollection is NOT an ordered collection"

           end-if

           if theClass = CharacterArray

               display "aCollection is a CharacterArray"

           else

               display "aCollection is NOT an CharacterArray"

           end-if

           goback

           .

Attachments:

TestClass.zip

Old KB# 4294