初音 玲 HATSUNE, Akira
| まずは設計ありき |
|---|
If Not 条件式1 Goto <ラベル1>
実行文1
Goto <ラベル>
<ラベル1>
If Not 条件式2 Goto <ラベル2>
実行文2
Goto <ラベル>
<ラベル2>
:
<ラベル>
実行文
:
|
<ラベル>
If Not 条件文1 Goto <ラベル1>
実行文1
実行文2
:
実行文n
Goto <ラベル>
<ラベル1>
実行文
:
|
Private Sub subPrint()
On Error Goto errPrint:
実行文1
実行文2
:
実行文n
exitPrint:
On Error Resume Next
Exit Sub
errPrint:
MsgBox Error$
Resume exitPrint:
End Sub
|
| オブジェクト指向の特徴 |
|---|


Option Explicit Private Sub cmdGet_Click() ' これ以外にローカル変数にアクセスする方法はない lblResult = strLocalGet() End Sub Private Function strLocalGet() As String Static lngCount As Long lngCount = lngCount + 1 strLocalGet = lngCount ' ローカル変数を返却 End Function |



| Visual Basicにおけるオブジェクト指向 |
|---|
Set objCls = New クラスモジュール名のようにクラスモジュールを実体化する命令を実行する必要がある。もし、実体化せずにクラスモジュールを直接指定してもなにもできない(図6)。ここまでであれば、十分オブジェクト指向言語と言えるだろう。

Option Explicit Private intNum As Integer Private Property Get prpValue() As Integer ' クラス内プロパティ prpValue = intNum End Property Private Property Let prpValue(ByVal vintValue As Integer) ' クラス内プロパティ intNum = vintValue End Property Friend Property Get fprpValue() As Integer ' プロジェクト内プロパティ fprpValue = intNum End Property Friend Property Let fprpValue(ByVal vintValue As Integer) ' プロジェクト内プロパティ intNum = vintValue End Property Public Property Get pprpValue() As Integer ' 公開プロパティ pprpValue = intNum End Property Public Property Let pprpValue(ByVal vintValue As Integer) ' 公開プロパティ intNum = vintValue End Property Private Function intValueGet() As Integer ' クラス内メソッド End Function Private Sub subValueGet() ' 公開メソッド intNum = intNum + 1 End Sub Private Sub subValueSet(ByVal vintValue As Integer) ' クラス内メソッド intNum = vintValue End Sub Friend Function fintValueGet() As Integer ' プロジェクト内メソッド fintValueGet = intNum End Function Friend Sub fsubValueGet() ' 公開メソッド intNum = intNum + 1 End Sub Friend Sub fsubValueSet(ByVal vintValue As Integer) ' プロジェクト内メソッド intNum = vintValue End Sub Public Function pintValueGet() As Integer ' 公開メソッド pintValueGet = intNum End Function Public Sub psubValueGet() ' 公開メソッド intNum = intNum + 1 End Sub Public Sub psubValueSet(ByVal vintValue As Integer) ' 公開メソッド intNum = vintValue End Sub |

Option Explicit Private aobjCls(1 To 2) As clsCls Private Sub cmdPropSet_Click(index As Integer) aobjCls(index).fprpValue = aobjCls(index).fprpValue + 1 lblFint(1).Caption = aobjCls(1).fintValueGet lblFprp(1).Caption = aobjCls(1).fprpValue lblPint(1).Caption = aobjCls(1).pintValueGet lblPprp(1).Caption = aobjCls(1).pprpValue lblFint(2).Caption = aobjCls(2).fintValueGet lblFprp(2).Caption = aobjCls(2).fprpValue lblPint(2).Caption = aobjCls(2).pintValueGet lblPprp(2).Caption = aobjCls(2).pprpValue End Sub Private Sub Form_Load() Set aobjCls(1) = New clsCls Set aobjCls(2) = New clsCls End Sub Private Sub Form_QueryUnload( _ Cancel As Integer, UnloadMode As Integer) Set aobjCls(1) = Nothing Set aobjCls(2) = Nothing End Sub |
aobjCls(index).psubValueGetのようにメソッドとして実装するものだが、プロパティの定義の中で値の演算を行ない、演算後の値を返却するような作りとすることももちろん可能だ。しかし、誰にも誤解されないようなインターフェイスとするためには、プロパティの定義の中で変数値を変更するなどは行なわないほうがよいだろう。

Option Explicit
Private colClses As New Collection
Private Sub cmdPropSet_Click(index As Integer)
colClses.Item(index).psubValueGet
On Error Resume Next
lblFint(1).Caption = colClses.Item(1).fintValueGet
lblFprp(1).Caption = colClses.Item(1).fprpValue
lblPint(1).Caption = colClses.Item(1).pintValueGet
lblPprp(1).Caption = colClses.Item(1).pprpValue
lblFint(2).Caption = colClses.Item(2).fintValueGet
lblFprp(2).Caption = colClses.Item(2).fprpValue
lblPint(2).Caption = colClses.Item(2).pintValueGet
lblPprp(2).Caption = colClses.Item(2).pprpValue
End Sub
Private Sub Form_Load()
Dim aobjCls(1 To 2) As clsCls
Set aobjCls(1) = New clsCls
Set aobjCls(2) = New clsCls
colClses.Add aobjCls(1)
colClses.Add aobjCls(2)
Set aobjCls(1) = Nothing
Set aobjCls(2) = Nothing
End Sub
Private Sub Form_QueryUnload(Cancel As Integer, _
UnloadMode As Integer)
Dim objCls As clsCls
For Each objCls In colClses
colClses.Remove 1
Next
End Sub
|


Option Explicit Implements clsCls Private Function clsCls_pintValueGet() As Integer End Function Private Property Let clsCls_pprpValue(ByVal RHS As Integer) End Property Private Property Get clsCls_pprpValue() As Integer End Property Private Sub clsCls_psubValueGet() End Sub Private Sub clsCls_psubValueSet(ByVal vintValue As Integer) End Sub |
Option Explicit Implements clsCls Private objCls As clsCls Private Sub Class_Initialize() Set objCls = New clsCls End Sub Private Sub Class_Terminate() Set objCls = Nothing End Sub Private Function clsCls_pintValueGet() As Integer ' 公開メソッド clsCls_pintValueGet = objCls.pintValueGet End Function Private Property Let clsCls_pprpValue(ByVal RHS As Integer) ' 公開プロパティ objCls.pprpValue = RHS End Property Private Property Get clsCls_pprpValue() As Integer ' 公開プロパティ clsCls_pprpValue = objCls.pprpValue End Property Private Sub clsCls_psubValueGet() ' 公開メソッド objCls.psubValueGet objCls.psubValueGet ' 増分値を2倍に拡張 End Sub Private Sub clsCls_psubValueSet(ByVal vintValue As Integer) ' 公開メソッド objCls.psubValueSet (vintValue) End Sub |
| Visual Basic.NETの可能性と互換性 |
|---|