Highlighted
Absent Member.
Absent Member.
3094 views

Example of Enumeration

Jump to solution

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 (1)
0 Likes
1 Solution

Accepted Solutions
Highlighted
Micro Focus Expert
Micro Focus Expert

Are you using Visual COBOL 2.2 or an earlier release?

The following full program compiles fine for me in Visual COBOL 2.2:

       program-id. Program1 as "testenum.Program1".
       data division.
       working-storage section.
       01 validDomain condition-value.
       01 myobj type MyClass.
       procedure division.
           set myobj to new MyClass
           set validDomain to myobj::ValidateDomain("GOV")
           
           goback.
           
       end program Program1.
       class-id MyClass.
       method-id ValidateDomain.
       01 domainExt type DomainExtensions.
       
       procedure division using by value domainString as String
                   returning validDomain as type Boolean.
            
            set validDomain to type Enum::TryParse(domainString, true, domainExt)
            
       end method.

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

View solution in original post

0 Likes
6 Replies
Highlighted
Micro Focus Expert
Micro Focus Expert

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

0 Likes
Highlighted
Micro Focus Expert
Micro Focus Expert

The following appears to work for the simple example where you want to validate if a string exists within a specified e-num and then to return true or false for this.

The second parameter "true" specifies that the search should be case insensitive.

The actual value of the enum found will be returned in DomainExt or the default value if the string is not found.

      method-id ValidateDomain.
      01 domainExt type DomainExtensions.
      procedure division using by value domainString as String
                  returning validDomain as type Boolean.

           set validDomain to type Enum::TryParse(domainString, true, domainExt)

      end method            

0 Likes
Highlighted
Absent Member.
Absent Member.

I wasn't able to get this piece of code to work.  When I copied what you had I got the below error.

Error 1 COBCH0829 : Could not find method 'TryParse' with this signature

0 Likes
Highlighted
Absent Member.
Absent Member.

I wasn't able to get Chris Glazier's code to work, but I was able to get this to work.  Thanks  

0 Likes
Highlighted
Micro Focus Expert
Micro Focus Expert

Are you using Visual COBOL 2.2 or an earlier release?

The following full program compiles fine for me in Visual COBOL 2.2:

       program-id. Program1 as "testenum.Program1".
       data division.
       working-storage section.
       01 validDomain condition-value.
       01 myobj type MyClass.
       procedure division.
           set myobj to new MyClass
           set validDomain to myobj::ValidateDomain("GOV")
           
           goback.
           
       end program Program1.
       class-id MyClass.
       method-id ValidateDomain.
       01 domainExt type DomainExtensions.
       
       procedure division using by value domainString as String
                   returning validDomain as type Boolean.
            
            set validDomain to type Enum::TryParse(domainString, true, domainExt)
            
       end method.

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

View solution in original post

0 Likes
Highlighted
Absent Member.
Absent Member.

The framework was the issue.  I was using 3.5 and this isn't available until 4.0+.

Thanks for your help with this.

0 Likes
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.