回答編集履歴

1

追記

2020/06/27 07:40

投稿

sousuke
sousuke

スコア3828

test CHANGED
@@ -31,3 +31,141 @@
31
31
  ![イメージ説明](c06c7a337b98b651bb3ef1821f4b8887.png)
32
32
 
33
33
  こう。
34
+
35
+
36
+
37
+ ### 直接関係ないですがそこまでやるんなら
38
+
39
+ IConstructorクラス作って
40
+
41
+ ```vba
42
+
43
+ Public Function Init(ByVal Args As Variant) As Boolean
44
+
45
+
46
+
47
+ End Function
48
+
49
+ ```
50
+
51
+ 標準モジュールにfunction書いて
52
+
53
+ ```vba
54
+
55
+ Public Function Init(ClassObject As IConstructor, ParamArray Args() As Variant) As IConstructor
56
+
57
+ Call ClassObject.Init(Args)
58
+
59
+ Set Init = ClassObject
60
+
61
+ End Function
62
+
63
+ ```
64
+
65
+ クラスでIConstructorをImplementsして
66
+
67
+ ```vba
68
+
69
+ Implements IControl
70
+
71
+ Implements IConstructor
72
+
73
+
74
+
75
+ Private WithEvents m_TextBox As Access.TextBox
76
+
77
+
78
+
79
+ ' Initiarizeの代わり
80
+
81
+ Private Function IConstructor_Init(ByVal Args As Variant) As Boolean
82
+
83
+ Set m_TextBox = Args(0)
84
+
85
+ m_TextBox.OnClick = "[EVENT PROCEDURE]"
86
+
87
+ End Function
88
+
89
+
90
+
91
+ Public Sub IControl_TestSub()
92
+
93
+ Debug.Print "IControl_MyTextBox_TestSub!"
94
+
95
+ End Sub
96
+
97
+
98
+
99
+ Private Sub m_TextBox_Click()
100
+
101
+ Debug.Print "IControl_MyTextBox_OnClick!"
102
+
103
+ End Sub
104
+
105
+
106
+
107
+ ```
108
+
109
+ フォーム側
110
+
111
+ ```vba
112
+
113
+ Private TestTextBox(2) As IControl
114
+
115
+
116
+
117
+ Private Sub Form_Open(Cancel As Integer)
118
+
119
+ ' Dim myText As IControl_MyTextBox
120
+
121
+ ' Set myText = New IControl_MyTextBox
122
+
123
+ ' myText.Initialize Me.txt
124
+
125
+
126
+
127
+ ' 上3行が1行に!初期化の引数も初期化ロジックもクラス側に書けるしかっこいい、それだけ
128
+
129
+ Set TestTextBox(0) = Init(New IControl_MyTextBox, Me.txt)
130
+
131
+
132
+
133
+ ' Dim myRequired As IControl_RequiredTextBox
134
+
135
+ ' Set myRequired = New IControl_RequiredTextBox
136
+
137
+ ' myRequired.Initialize Me.txt
138
+
139
+ '
140
+
141
+ ' Dim myNumberCheck As IControl_NumberCheckTextBox
142
+
143
+ ' Set myNumberCheck = New IControl_NumberCheckTextBox
144
+
145
+ ' myNumberCheck.Initialize Me.txt
146
+
147
+ '
148
+
149
+ ' Set TestTextBox(0) = myText
150
+
151
+ ' Set TestTextBox(1) = myRequired
152
+
153
+ ' Set TestTextBox(2) = myNumberCheck
154
+
155
+ End Sub
156
+
157
+ ```
158
+
159
+
160
+
161
+ VBAはオブジェクトの破棄に参照カウント制を使っているといわれています。
162
+
163
+ 相互参照がされると永続的に破棄されない状態になる場合もあるので
164
+
165
+ ```vba
166
+
167
+ Set obj = Nothing
168
+
169
+ ```
170
+
171
+ はしっかり意識したほうがメモリリークしないでいい…かも。