Previous Topic Next topic Print topic


Objects - in COBOL and C#

Objects in Managed COBOL

01 hero  type SuperHero value new SuperHero.
01 hero2 type SuperHero.
01 obj object.
01 reader type StreamReader.
01 myLine string.
 
set hero::Name to SpamMan
set hero::PowerLevel to 3
 
invoke hero::Defend("Laura Jones")
invoke type SuperHero::Rest   *> Calling static method
 
set hero2 to hero     *> Both reference the same object
set hero2::Name to "WormWoman"
display hero::Name    *> Prints WormWoman
 
set hero to null      *> Free the object
 
if hero = null
    set hero to new SuperHero
end-if
 
set obj to new SuperHero
if obj is instance of type SuperHero
    display "Is a SuperHero object."
end-if
 
*> No 'using' construct in COBOL
try
    set reader to type File::OpenText("test.txt")
    perform until exit
        set myLine to reader::ReadLine
        if myLine = null
            exit perform
        end-if
    end-perform
finally
    if reader not = null
        invoke reader::Dispose
    end-if
end-try  

Objects in C#

SuperHero hero = new SuperHero(); 
 

 
 
hero.Name = "SpamMan"; 
hero.PowerLevel = 3; 
 
 
hero.Defend("Laura Jones");
SuperHero.Rest();   // Calling static method
 
SuperHero hero2 = hero;   // Both reference the same object 
hero2.Name = "WormWoman"; 
Console.WriteLine(hero.Name);   // Prints WormWoman
 
hero = null ;   // Free the object
 
if (hero == null)
  hero = new SuperHero();
 
Object obj = new SuperHero(); 
if (obj is SuperHero)
{ 
  Console.WriteLine("Is a SuperHero object.");
} 
  
// Mark object for quick disposal
using (StreamReader reader = File.OpenText("test.txt")) 
{
  string line;
  while ((line = reader.ReadLine()) != null)
  {
    Console.WriteLine(line);
  }
}
Previous Topic Next topic Print topic