Continuing the series of blogs about COBOL and the .Net base class library…
The .Net base class library has a wealth classes and an huge of amount of methods/properties.
The .Net base class library has a key handy namespace that contains are the lovely classes, which is System.Collections.
As you can see from the above link, it does have quite a lot, so for this blob entry I will look at:
arrays of numbers
queues
stacks
Lets start the first example by using an array of numbers… just to demonstrate its not just strings you can assign to the arrays using “values”.
01 numbers binary-long occurs 10
values 10 20 30 40 50 10 20 30 40 50.
01 num binary-long value 0.
01 average binary-double value 0.
procedure division.
perform varying num through numbers
add num to average
end-perform
divide average by numbers::"Length" giving average
display "Average is : " average
Running the above code gives us:
Average is : +00000000000000000030
The next class that is helpful, is the Queue class, this allows us to “Enqueue” and “Dequeue” item using the same methods, the queue is again displayed using the “perform through” syntax.
$set ilusing"System"
$set ilusing"System.Collections"
01 alphabetQueue type "Queue".
01 item object reference.
set alphabetQueue to new type "Queue"()
invoke alphabetQueue::"Enqueue"("A item")
invoke alphabetQueue::"Enqueue"("B item")
invoke alphabetQueue::"Enqueue"("C item")
display "The initial queue is: "
perform varying item through alphabetQueue
display " " item
end-perform
display "".
display "De-Queue an item: " alphabetQueue::"Dequeue"()
display "".
display "Afterwards is: "
perform varying item through alphabetQueue
display " " item
end-perform
Running the above queue example gives:
The initial queue is:
A item
B item
C item
De-Queue an item: A item
Afterwards is:
B item
C item
Next, lets look at the ‘Stack’ class, this is very similar, expect it uses the “Push”, “Pop” methods and we iterate through the Stack using the ‘perform through’ syntax.
$set ilusing"System"
$set ilusing"System.Collections"
01 alphabetStack type "Stack".
01 item object reference.
set alphabetStack to new type "Stack"()
invoke alphabetStack::"Push"("A item")
invoke alphabetStack::"Push"("B item")
invoke alphabetStack::"Push"("C item")
display "The initial stack is: "
perform varying item through alphabetStack
display " " item
end-perform
display "".
display "Pop item: " alphabetStack::"Pop"()
display "".
display "Afterwards is: "
perform varying item through alphabetStack
display " " item
end-perform
Running the above stack example gives:
The initial stack is:
C item
B item
A item
De-Stack an item: C item
Afterwards is:
B item
A item
As you can see using the base class library collection classes with COBOL is just as easy as using C#… so if you are already using COBOL… continue and try out the .Net base class library.. it really is very easy to use and it will make you more productive.. so please explore and enjoy .Net and its Base Class library.