Example of Enumeration

Trying to figure out how to use enumerations.  Can anybody help?

I also noticed that visual Cobol doesn't support the Enumeration TryParse?  The goal is to edit the domain extension and ignoring the case.

 

Here is what I have so far.

enum-id DomainExtensions.
01  binary-long.
      78  com.        
      78  edu.        
      78  gov.       
end enum.

 

Now trying to use it is the problem. 

      

method-id ValidateDomain private

01 domainExt type DomainExtensions.

procedure division using by value domainString as String

                   returning validDomain as type Boolean.

   set validDomain to false.

   set domainExt to new DomainExtensions()

   set domainExt to (type DomainExtensions)::Enum::Parse(type DomainExtensions, domainString, true)

   set domainExt to type (DomainExtensions)::Enum::Parse(type DomainExtensions, domainString, true)          

   set domainExt to type (type DomainExtensions)::Enum::Parse(type DomainExtensions, domainString, true)          

   set domainExt to type Enum(type DomainExtensions)::Parse(type DomainExtensions, domainString, true)

   set domainExt to type Enum::Parse(type DomainExtensions, domainString, true).

   if domainExt = type DomainExtensions::com

end method.

Tags:

Parents
  • For the parse, you will need something like:

      set domainExt to type Enum::Parse(type of DomainExtensions, domainString, true)

                as type DomainExtensions

    Note that the first parameter is a Type object, which can be returned using the TYPE OF operator.

    Also this method returns a simple object, which needs to be cast to the correct enumeration type.

    I have a feeling there should be an easier way of converting a string into an enum, but can't think of it right now.

    Also it may be worth pointing out that you don't need to instantiate the enum before the call to the Parse method.  Normally you set these things using syntax like:

    set domainExt to type DomainExtensions::com

Reply
  • For the parse, you will need something like:

      set domainExt to type Enum::Parse(type of DomainExtensions, domainString, true)

                as type DomainExtensions

    Note that the first parameter is a Type object, which can be returned using the TYPE OF operator.

    Also this method returns a simple object, which needs to be cast to the correct enumeration type.

    I have a feeling there should be an easier way of converting a string into an enum, but can't think of it right now.

    Also it may be worth pointing out that you don't need to instantiate the enum before the call to the Parse method.  Normally you set these things using syntax like:

    set domainExt to type DomainExtensions::com

Children
No Data