Gilja's logs of the struggles with programming

シン母駆け出しエンジニアの奮闘記

Classの書き方

ClassのProperty設定

Option Explicit

'クラスSampleの定義
Class Sample
  'Caption プロパティ
  Public Caption
End Class

'オブジェクト変数
Dim objSample1
Set objSample1 = New Sample
objSample1.Caption = "hoge"

MsgBox objSample1.Caption '//ここで「hoge」と表示する

Set objSample = Nothing

Property設定時に処理したい/Propertyに初期設定したものを取得したい

'まず、Privateのときは変数にm_と付けるのが慣習らしいのでそれに習う

Class Sample
  '変数設定
  Private m_strCaption

  'Captionプロパティの値を設定
  Public Property Let Caption(Value) '//インスタンスのプロパティに値が代入される際実行
    m_strCaption = Value '//この中で値をこねくりまわせる!!
  End Property

  'Captionプロパティの値を取得
  Public Property Get Caption '//インスタンスのプロパティが参照される際実行
    Caption = m_strCaption
  End Property

  'Public Default Property Get Captionとすると、「.Caption」とせずとも読み込める(規定のプロパティになる)

End Class
Class Sample
  'プロパティに規定値をClass内に設定したいとき
  Private m_strCaption

  Private Sub Class_Initialize() '//Initializeイベント(コンストラクタとも呼ぶ)
    m_strCaption = "hoge" '//インスタンス生成される度に実行される(規定値を設定しておく)
  End Sub

  Public Property Get Caption
    Caption = m_strCaption
  End Property

End Class

Dim objSample1
Set objSample1 = New Sample
MsgBox objSample1.Caption '// 「hoge」と表示される
Set objSample1 = Nothing

インスタンスメソッド

Class Sample
  Public Caption

  Public Sub Show() '//メソッドの書き方
    MsgBox Me.Caption '//Me とはインスタンスオブジェクト
  End Sub
End Class

Dim objSample1
Set objSample1 = New Sample
objSample1.Caption = "hoge"

objSample1.Show '//インスタンスメソッド呼び出し 今回のケースでは、「hoge」と表示される処理

Set objSample1 = Nothing

デフォルトクラス

RegExp 正規表現のクラスはデフォルトで使用できるのだ!

最後に問題を解こう

Option Explicit

Class Circle

  Private m_intRadius
  Private m_intDiameter
  Private m_dblArea

  Public Property Let Radius(value)
    m_intRadius = value
    m_intDiameter = m_intRadius * 2
    m_dblArea = m_intRadius ^ 2 * 3.14
  End Property

  Public Property Get Radius
    Radius = m_intRadius
  End Property
  Public Property Get Diameter
    Diameter = m_intDiameter
  End Property
  Public Property Get Area
    Area = m_dblArea
  End Property

  Public Sub ShowDetail(key)
    Private strMsg

    Select Case key
      Case "半径"
        strMsg = "半径:" & Me.Radius
      Case "直径"
        strMsg = "直径:" & Me.Diameter
      Case "面積"
        strMsg = "面積:" & Me.Area
      Case "全部"
        strMsg = "半径:" & Me.Radius & vbCrLf & _
                         "直径:" & Me.Diameter & vbCrLf & _
                         "面積:" & Me.Area & vbCrLf & _
    End Select

    MsgBox strMsg
  End Sub

End Class

Dim objCircle1
Set objCircle1 = New Circle
Dim intInput
intInput = InputBox("何の要素を取得したいですか?数字で選択してください。" & vbCrlf & _ 
                                   "1:半径、2:直径、3:面積、4:全部")
If Not (intInput = 1 Or intInput = 2 Or intInput = 3 Or intInput = 4) Then
  Msgbox "不正です"
Else
  objCircle1.Radius = 3
  If intInput = 1 Then
    strInput = "半径"
  ElseIf intInput = 2 Then
    strInput = "直径"
  ElseIf intInput = 3 Then
    strInput = "面積"
  Else
    strInput = "全部"
  End If

    objCircle.ShowDetail(strInput)

End If

Set objCircle = Nothing

答え合わせは明日しましょー