Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
20200806_VBAのクラスでNewと同時に引数付きコンストラクタを起動する代替案
Option Explicit
Sub Sample00()
Dim obj As MyClass
Set obj = New MyClass
obj.Init1 "太郎", 10
Debug.Print obj.ToString
End Sub
Rem 1.宣言と生成を同時に実行する
Sub Sample10()
Dim obj As New MyClass
obj.Init1 "太郎", 10
Debug.Print obj.ToString
End Sub
Sub Sample11()
Dim arr(10)
Dim i As Long
For i = 0 To 10
Set arr(i) = New MyClass
arr(i).Init "a" & i, i
Debug.Print arr(i).ToString
Next
End Sub
Rem 2.コロンを使ってマルチステートメントにする
Sub Sample20()
Dim obj As MyClass
Set obj = New MyClass: obj.Init1 "太郎", 10
Set obj = New MyClass: Call obj.Init1("太郎", 10)
Set obj = New MyClass: Set obj = obj.Init1("太郎", 10)
Debug.Print obj.ToString
End Sub
Rem 3.生成式を組み込み関数で包む
Sub Sample30()
Dim obj As MyClass
Set obj = Array(New MyClass)(0).Init1("太郎", 10)
Set obj = VBA.Array(New MyClass)(0).Init1("太郎", 10)
Set obj = VBA.CVar(New MyClass).Init1("太郎", 10)
' Set obj = (New MyClass).Init("太郎", 10)
' Set obj = CVar(New MyClass).Init("太郎", 10)
Debug.Print obj.ToString
End Sub
Rem 4.生成式を自作関数で包む
Sub Sample40()
Dim obj As MyClass
Set obj = ToMyClass(New MyClass).Init1("太郎", 10)
Debug.Print obj.ToString
End Sub
Function ToMyClass(obj) As MyClass
Set ToMyClass = obj
End Function
Rem 5.生成とコンストラクタ実行を自作関数で行う
Sub Sample50()
Dim obj As MyClass
Set obj = CreateMyClass("太郎", 10)
Debug.Print obj.ToString
End Sub
Function CreateMyClass(pName, pAge) As MyClass
Set CreateMyClass = New MyClass
Call CreateMyClass.Init1(pName, pAge)
End Function
Rem 6.CallByNameを使う
Sub Sample60()
Dim obj As MyClass
Set obj = CallByName(New MyClass, "Init1", VbMethod, "太郎", 10)
Debug.Print obj.ToString
End Sub
Rem 7.デフォルトインスタンスを使う
Sub Sample70()
Dim obj As MyClass
Set obj = MyClass.Init1("太郎", 10)
Debug.Print obj.ToString
End Sub
'NG
Sub Sample71()
Dim obj As MyClass
Set obj = MyClass.Init1("太郎", 10)
Dim obj2 As MyClass
Set obj2 = MyClass.Init1("花子", 8)
Debug.Print obj.ToString
Debug.Print obj2.ToString
End Sub
'OK
Sub Sample72()
Dim obj As MyClass
Set obj = MyClass.Init2("太郎", 10)
Debug.Print obj.ToString
End Sub
'NG
Sub Sample73()
Dim obj As MyClass
Set obj = MyClass.Init3("太郎", 10)
Debug.Print "obj", obj.ToString
Debug.Print "MyClass", MyClass.ToString
End Sub
'OK
Sub Sample74()
Dim obj As MyClass
Set obj = MyClass.Init4("太郎", 10)
Debug.Print "obj", obj.ToString
Debug.Print "MyClass", MyClass.ToString
End Sub
Rem 8.クラス内に生成関数を追加する
Sub Sample80()
Dim obj As MyClass
Set obj = MyClass.CreateMyClass("太郎", 10)
Debug.Print obj.ToString
End Sub
Option Explicit
Private name_
Private age_
Rem 標準モジュール用
Public Function Init1(pName, pAge) As MyClass
Set Init1 = Me
name_ = pName
age_ = pAge
End Function
Rem デフォルトインスタンス用
Public Function Init2(pName, pAge) As MyClass
Set Init2 = New MyClass
Init2.Name = pName
Init2.Age = pAge
End Function
Rem NG
Public Function Init3(pName, pAge) As MyClass
Set Init3 = New MyClass
name_ = pName
age_ = pAge
End Function
Rem デフォルトインスタンス用
Public Function Init4(pName, pAge) As MyClass
If Me Is MyClass Then
With New MyClass
Set Init4 = .Init4(pName, pAge)
End With
Exit Function
End If
Set Init4 = Me
name_ = pName
age_ = pAge
End Function
Public Property Let Name(pName): name_ = pName: End Property
Public Property Let Age(pAge): age_ = pAge: End Property
Public Function ToString() As String
ToString = "名前:=" & name_ & " 年齢:=" & age_ & "歳"
End Function
Public Function CreateMyClass(pName, pAge) As MyClass
Set CreateMyClass = New MyClass
Call CreateMyClass.Init1(pName, pAge)
End Function
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.