オブジェクト

このヘルプでは、COBOL、C#、VB.NET でのオブジェクトの使い方について説明します。

COBOL のオブジェクト

01 hero  type SuperHero value new SuperHero.
01 hero2 type SuperHer.
01 obj object.
01 reader type StreamReader.
01 lin string.

*> No "With" construct
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 lin to reader::ReadLine
        if lin = null
            exit perform
        end-if
    end-perform
finally
    if reader not = null
        invoke reader::Dispose
    end-if
end-try

C# のオブジェクト

SuperHero hero = new SuperHero();



// No "With" construct
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);
  }
}

VB.NET のオブジェクト

Dim hero As SuperHero = New SuperHero
' or
Dim hero As New SuperHero

With hero
  .Name = "SpamMan"
  .PowerLevel = 3
End With
hero.Defend("Laura Jones")
hero.Rest()     ' Calling Shared method
' or
SuperHero.Rest()

Dim hero2 As SuperHero = hero  ' Both reference the same object
hero2.Name = "WormWoman"
Console.WriteLine(hero.Name)   ' Prints WormWoman

hero = Nothing    ' Free the object

If hero Is Nothing Then _
  hero = New SuperHero

Dim obj As Object = New SuperHero
If TypeOf obj Is SuperHero Then _
  Console.WriteLine("Is a SuperHero object.")






' Mark object for quick disposal
Using reader As StreamReader = File.OpenText("test.txt")
  Dim line As String = reader.ReadLine()
  While Not line Is Nothing
    Console.WriteLine(line)
    line = reader.ReadLine()
  End While
End Using

これらの例の一部は、ハーディング大学コンピュータ サイエンス学部の Frank McCown 博士が作成したもので、クリエイティブ コモンズ ライセンスに基づいて使用が許可されています。