クラスの作成4(クラスの実装)
 クラスの作成1〜3で説明したメソッドとプロパティの設定,参照を利用して
実際に使用するクラスを作成します。

 メソッドの第1と第2引数をプロパティを設定することで取得させます。
サンプルコードでは新たにClass2を作成して第1引数のデータ配列と第2引数の
データ数を設定または参照できるプロパティを作成しています。

● サンプル ●

Private intData As Integer
Private dblArrenge As Variant

Public Property Let DataNumber(ByVal DataNumber As Integer)
    intData = DataNumber
End Property

Public Property Get DataNumber() As Integer
    DataNumber = intData
End Property

Public Property Let Arrangement(ByVal Arrangement As Variant)
    dblArrenge = Arrangement
End Property

Public Property Get Arrangement() As Variant
    Arrangement = dblArrenge
End Property

Public Function Sum() As Double
  
    Dim Counter As Integer
    Dim total As Variant
    
    For Counter = 1 To intData
        total = total + dblArrenge(Counter)
        Debug.Print total
    Next Counter
    
    Sum = total
  
End Function

● 動作確認 ●

Sub Class_Sample4()
    Dim cn As New ADODB.Connection
    Dim rst As ADODB.Recordset
    Dim cls As Class2
    Dim tanka(300) As Variant
    Dim 合計 As Double
    Dim i As Integer
 
    Set cn = New ADODB.Connection
    cn.ConnectionString = _
        "Provider=microsoft.jet.oledb.4.0;" & _
        "Data Source=d:\NorthWind.mdb"
    cn.Open
 
    Set rst = New ADODB.Recordset
    rst.Source = "商品"
    rst.ActiveConnection = cn
    rst.CursorLocation = adUseClient
    rst.CursorType = adOpenDynamic
    rst.LockType = adLockOptimistic
    rst.Open

    With rst
        Do While Not .EOF
            i = i + 1
            tanka(i) = !単価
            .MoveNext
        Loop
    End With

    Set cls = New Class2
    cls.Arrangement = tanka()
    cls.DataNumber = rst.RecordCount
    合計 = cls.Sum()
    MsgBox 合計
 
    rst.Close
    cn.Close
    Set rst = Nothing
    Set cn = Nothing
    Set cls = Nothing

End Sub

(実行結果)
メッセージボックスに「73560」が表示されます。

● 補足説明 ●

配列tanka()をByValで引き渡しているのでデータ型はVariantとしています。

Access97/2000



戻る


Excel Word Access VBA! モーグ