プロパティ

このヘルプでは、COBOL、C#、VB.NET でのプロパティの使い方について説明します。

COBOL のプロパティ

      $set sourceformat(free)
class-id MyClass.

    01 _size binary-long private.
    method-id get property #Size.
        procedure division returning ret as binary-long.
        set ret to _size
    end method.

    method-id set property #Size.
        procedure division using by value val as binary-long.
        if val < 0
            set _size to 0
        else
            set _size to val
        end-if
    end method.

end class.

class-id a.
    method-id main.
        01 foo type MyClass value new MyClass.
        add 1 to foo::Size
        display foo::Size
    end method.
end class a.

C# のプロパティ

private int _size;

public int Size
{
  get
  {
    return _size;
  }
  set
  {
    if (value < 0)
    {
      _size = 0;
    }
    else
    {
      _size = value;
    }
  }
}
foo.Size++;

VB.NET のプロパティ

Private _size As Integer

Public Property Size() As Integer
  Get
    Return _size
  End Get
  Set (ByVal Value As Integer)
    If Value < 0 Then
      _size = 0
    Else
      _size = Value
    End If
  End Set
End Property

foo.Size += 1

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