メンバのプロパティとメソッドを定義する

プロパティとメソッドはクラスに結びついていて、そのクラスに属するオブジェクトと一緒にのみ使用できます。プロパティとメソッドは Class ステートメントの中で定義します。

  • プロパティ

    保護された変数を操作するために使用される 1 組の関数で、アプリケーションが直接アクセスできないユーザー定義のクラスの Private メンバです。

  • メソッド

    オブジェクトに関する操作を実行するサブルーチンまたは関数です。

Class syntax, containing a member variables, a Sub, Function, and Property

Stack クラスは、いくつかのプロパティとメソッドを使用して、スタックデータ構造体に関して単純な push と pop 操作を実行します。

Class Stack 
  Private idx As Integer
  Stack List As Variant
  Public stackName As String
  Private Sub CheckStack ' Sub is visible only within 
                         ' the class.
    If idx% = 0 Then Error 999
  End Sub
  Sub New
    idx% = 0        ' Initialize idx.
  End Sub

   Private Property Set topValue As Variant
     CheckStack
     Stack(idx%) = topValue  ' Set the top value on the stack.
   End Property
  
   Private Property Get topValue As Variant
     CheckStack
     topValue = Stack(idx%)  ' Get the top value on the stack.
   End Property

   ' Same as Get for topValue.
   Function Top
      Top = topValue   ' Call the topValue Get method.
   End Function

   Sub Push(v)         ' Push a value on the stack.
     idx% = idx%+1
     topValue = v
   End Sub

   Function Pop        ' Pop a value off the stack.
     Pop = topValue
     Erase Stack(idx%)
     idx% = idx%-1
   End Function

   ' Read-only property. There is no Set for Count.
   Property Get Count
     Count = idx%       ' Count the values on the stack.
   End Property

End Class

Dim St As New Stack
Call St.Push("An item on the stack")
Call St.Push("Another item on the stack")
Print "# of items on the stack is ";St.Count
Print "TopValue is ";St.Top

Sub New と Sub Delete を宣言する (オブジェクトの初期化と削除)

クラス定義の中で、2 つの特別なサブルーチンを作成できます。2 つとも常に Public です。Private として宣言することはできません。

  • Sub New

    オブジェクトが作成されるときに自動的に実行されるサブルーチンです。Sub New は New キーワードの付いた Dim ステートメントが実行されるとき、または Sub New が定義されたクラスを参照する Set ステートメントが参照されるときに実行されます。New という名前のサブルーチンとパラメータを定義することにより Sub New を作成し、新たに作成されたオブジェクトを初期化します。1 つのクラスが持つことのできる Sub New は 1 つだけです。

  • Sub Delete

    このサブルーチンが定義されたクラスのオブジェクトが削除されるときに自動的に実行されるサブルーチンです。パラメータを指定せずに Delete という名前でサブルーチンを定義することにより、Sub Delete を作成します。1 つのクラスが持つことのできる Sub Delete は 1 つだけです。

これらのサブルーチンは、スクリプト内のイベントとして使用できます。例えば、Sub New を使用してファイルを開き、Sub Delete を使用してファイルを閉じる File クラスを作成できます。同様に、Sub New を使用して新規ページを開始し、テキストを揃え、マージンを設定し、Sub Delete を使用して印刷ジョブを終了させる PrintJob クラスを作成できます。

サンプルスクリプト内の Sub New は、CustomerAccount オブジェクトのメンバ変数を初期化します。新規 Account オブジェクトを作成する Set ステートメントは、Account クラス用の Sub New で必要とする 3 つのパラメータも渡します。Sub New は、新たに作成されるオブジェクトの 3 つのメンバ変数 (balance@、acctNum&、customerNum&) に引数の値を代入します。

Class Account
  balance As Currency
  acctNum As Long
  customerNum As Long
' Declare Sub New.
  Sub New (newBal As Currency, newAcctNum As Long, _
    newCustNum As Long)
     balance@ = newBal@
     acctNum& = newAcctNum&
     customerNum& = newCustNum&
     Print "New Parms=";balance@, acctNum&, customerNum&
  End Sub
' Declare Sub Delete.
  Sub Delete
     Print "Deleting account record for customer: ";customerNum
  End Sub
End Class
'.....
Dim CustomerAccount As Account
' Create the object.
Set customerAccount = New Account(1234.56, 10001991, 5412)           
Delete customerAccount     ' Explicitly delete the object.