Skip to main content

In C#, if you have a string (say, TestString) and you want to test it to see if it contains only numeric digits 0-9, you can do this:

I basically just need to know how to code what's on the if statement in Visual COBOL.

Thanks

In C#, if you have a string (say, TestString) and you want to test it to see if it contains only numeric digits 0-9, you can do this:

I basically just need to know how to code what's on the if statement in Visual COBOL.

Thanks

First, a suggestion: Don't use a screenshot to quote code (or for any other purpose, when it can be avoided). Some people may not be able to see the image, and it can't be copied and edited. Just copy and paste the text; it only takes a second, and it's much more usable.

This is a tricky one, actually. The "All" method is not defined by the String class. It's a LINQ extension method, defined in System.Linq.Enumerable; and it's a generic extension method to boot, taking a delegate. So what the C# code is actually invoking is:

System.Linq.Enumerable.All<char>(TestString, char.IsDigit)

It's just that the extension method mechanism hides the details of that from you.

COBOL for .NET can do that too, but there are some details:

  1. You need the System.Linq namespace and a reference to System.Core, which contains the Enumerable class.
  2. You need to use the method keyword to indicate IsDigit is a delegate. (The method keyword is necessary because COBOL can invoke a method with no parameters inline; if you don't have "method" there, the compiler can't distinguish that from invoking something called IsDigit and pass the result to All.)

So, the COBOL version is:

TestString::All(method character::IsDigit)

Here's a complete example using Visual COBOL 5.0 PU1:

$set sourceformat(free)
$set ilusing"System"
$set ilusing"System.Linq"
$set ilref"System.Core"

class-id testcb.

method-id Main (args as string occurs any) static.
perform varying arg as string through args
if arg::All(method character::IsDigit)
display 'String "' arg '" contains only digits'
else
display 'String "' arg '" contains other stuff'
end-if
end-perform
end method Main.

end class testcb.

Compile that with cobol testcb.cbl ilgen ; (or, I suppose, using Visual Studio), and run it as e.g. testcb foo 123 bar7.

If you're using an older version of the product that doesn't support extension methods (it'd have to be pretty old...), something like this might work instead:

if type Enumerable::All[character](arg, method character::IsDigit)