「Visual Basic 6の基礎 - 関数」の版間の差分

ナビゲーションに移動 検索に移動
 
140行目: 140行目:
  result = Sum(1, 2, 3, 4, 5)  ' result = 15
  result = Sum(1, 2, 3, 4, 5)  ' result = 15
  </syntaxhighlight>
  </syntaxhighlight>
<br><br>
== プロパティ ==
==== プロパティとは ====
プロパティは内部データへのアクセスを制御して、値の検証やその他のロジックを追加することができる。<br>
<br>
プロパティには、以下に示す3つの主要なアクセサがある。<br>
* Get
*: 値の読み取り
* Let
*: 値の設定 (通常の値型)
* Set
*: オブジェクトの設定
<br>
<br>
==== プロパティ ====
  <syntaxhighlight lang="vb">
  <syntaxhighlight lang="vb">
  Private m_Name As String
  Private m_Name As String
156行目: 168行目:
  Me.Name = "Test"    ' 設定
  Me.Name = "Test"    ' 設定
  Debug.Print Me.Name  ' 取得
  Debug.Print Me.Name  ' 取得
</syntaxhighlight>
<br>
==== データの検証 ====
<syntaxhighlight lang="vb">
Property Let Age(value As Integer)
    If value >= 0 And value <= 150 Then
      mAge = value
    Else
      Err.Raise 1000, "Invalid age value"
    End If
End Property
</syntaxhighlight>
<br>
==== 計算値の提供 ====
<syntaxhighlight lang="vb">
Property Get TotalPrice() As Currency
    TotalPrice = mBasePrice * (1 + mTaxRate)
End Property
</syntaxhighlight>
<br>
==== カプセル化による安全性確保 ====
<syntaxhighlight lang="vb">
Private mPassword As String
Property Let Password(value As String)
    mPassword = Encrypt(value)
End Property
</syntaxhighlight>
<br>
==== 参照整合性の管理 ====
<syntaxhighlight lang="vb">
Private mParent As Form
Property Set Parent(obj As Form)
    If Not obj Is Nothing Then
      Set mParent = obj
      ' その他の初期化処理
      ' ...略
    End If
End Property
  </syntaxhighlight>
  </syntaxhighlight>
<br><br>
<br><br>

案内メニュー