|
例えば、キャラのプロパティを設定して、コレクションに登録する部分のコードが面倒なので、シンプルに1行にしたいというときは、クラスモジュールに下記のようにメソッドを追加します。
クラスモジュール Chara
--------------------
Option Explicit
Public Name As String
Public HP As Long
Public AttacK As Long
Public Defence As Long
Public Skill As String
Public Sub Init(aCol As Collection, aName As String, aHP As Long, aAttacK As Long, aDefence As Long, aSkill As String)
Name = aName
HP = aHP
AttacK = aAttacK
Defence = aDefence
Skill = aSkill
aCol.Add Me, Name
End Sub
-------------------
そうすると前回の回答の使用例のコードは下記のようになります。
Public Sub test1()
Dim colChara As Collection
Set colChara = New Collection
Dim C As Chara
Set C = New Chara
C.Init colChara, "ガイ", 200, 90, 50, "兜割り"
Set C = New Chara
C.Init colChara, "トム", 300, 30, 90, "鉄壁の守り"
Debug.Print colChara("ガイ").HP
Debug.Print colChara("トム").Defence
End Sub
|
|