-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathclsGeneric.cls
240 lines (191 loc) · 6.74 KB
/
clsGeneric.cls
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
VERSION 1.0 CLASS
BEGIN
MultiUse = -1 'True
Persistable = 0 'NotPersistable
DataBindingBehavior = 0 'vbNone
DataSourceBehavior = 0 'vbNone
MTSTransactionMode = 0 'NotAnMTSObject
END
Attribute VB_Name = "clsGeneric"
Attribute VB_GlobalNameSpace = False
Attribute VB_Creatable = True
Attribute VB_PredeclaredId = False
Attribute VB_Exposed = False
Option Explicit
Private mData As Dictionary
Private Sub Class_Initialize()
' Create a hash table to store the values
Set mData = New Dictionary
mData.CompareMode = TextCompare
End Sub
Private Sub Class_Terminate()
' Destroy the hash table
Set mData = Nothing
End Sub
Public Function z_Dictionary() As Dictionary
' Return the hash table
Set z_Dictionary = mData
End Function
Public Sub Inherit(ByRef obj As Variant)
' Inherit all the properties and methods of another object
Dim dict As Dictionary
Dim SuperClass As clsGeneric
Dim i As Long
Dim keys() As Variant
If TypeName(obj) <> "clsGeneric" Then
MsgBox "You can't inherit from this type of object!", vbCritical
End
End If
Set SuperClass = obj
Set dict = SuperClass.z_Dictionary
keys = dict.keys
For i = 0 To UBound(keys, 1)
' Inherit all except name
If StrComp(keys(i), "_ClassName", vbTextCompare) <> 0 Then
Change keys(i), dict(keys(i))
End If
Next i
End Sub
Private Sub Lookup(ByVal Name As String, ByRef Data As Variant)
Dim dict As Dictionary
Dim SuperClass As clsGeneric
If mData.Exists(Name) Then
' Pull it from the object
If IsObject(mData(Name)) Then
Set Data = mData(Name)
Else
Data = mData(Name)
End If
ElseIf mData.Exists("_SuperClass") Then
' Pull it from the superclass
Set SuperClass = mData("_SuperClass")
Set dict = SuperClass.z_Dictionary
If dict.Exists(Name) Then
If IsObject(dict(Name)) Then
Set Data = dict(Name)
Else
Data = dict(Name)
End If
Else
Form1.Display "Class '" & dict("_ClassName") & "' doesn't have a property '" & Name & "'!"
End If
End If
End Sub
Private Sub Change(ByVal Name As String, ByRef Data As Variant)
If mData.Exists(Name) Then
mData.Remove Name
End If
mData.Add Name, Data
End Sub
Public Property Get Stat(ByVal Name As String) As Variant
Lookup Name, Stat
End Property
Public Property Let Stat(ByVal Name As String, ByRef Value As Variant)
Change Name, Value
End Property
Public Function SaveToString() As String
Dim keys() As Variant
Dim i As Long
Dim s As String
keys = mData.keys
For i = 0 To UBound(keys, 1)
If Not IsObject(mData(keys(i))) Then
s = s & keys(i) & ": " & mData(keys(i)) & ", "
End If
Next i
If Len(s) > 2 Then
SaveToString = Left$(s, Len(s) - 2)
End If
End Function
Private Sub DoEvent(ByVal Method As String, ParamArray args() As Variant)
On Error GoTo Err_Init
Dim SuperClass As clsGeneric
Dim dict As Dictionary
Dim EventName As String
Dim MethodName As String
' Make sure this is an instance, not a class
If mData.Exists("_SuperClass") = False Then
MsgBox "You can't call a method of the class, only INSTANCES of that class!", vbCritical
End
End If
' Retrieve the superclass methods
Set SuperClass = mData("_SuperClass")
Set dict = SuperClass.z_Dictionary
' Look up this event in the methods
EventName = "On" & Method
If dict.Exists(EventName) = False Then
Form1.Display dict("_ClassName") & " doesn't handle event '" & Method & "'!"
Exit Sub
End If
MethodName = dict(EventName)
' Execute the specified function
Select Case UBound(args, 1):
Case Is < 0: CallByName Me, MethodName, VbMethod
Case 0: CallByName Me, MethodName, VbMethod, args(0)
Case 1: CallByName Me, MethodName, VbMethod, args(0), args(1)
Case 2: CallByName Me, MethodName, VbMethod, args(0), args(1), args(2)
Case 3: CallByName Me, MethodName, VbMethod, args(0), args(1), args(2), args(3)
Case 4: CallByName Me, MethodName, VbMethod, args(0), args(1), args(2), args(3), args(4)
Case 5: CallByName Me, MethodName, VbMethod, args(0), args(1), args(2), args(3), args(4), args(5)
Case 6: CallByName Me, MethodName, VbMethod, args(0), args(1), args(2), args(3), args(4), args(5), args(6)
Case 7: CallByName Me, MethodName, VbMethod, args(0), args(1), args(2), args(3), args(4), args(5), args(6), args(7)
Case Else: MsgBox "Increase the number of cases in DoMethod!", vbCritical
End
End Select
Exit Sub
Err_Init:
If Err.Number = 438 Then
Form1.Display "ERROR - Function " & MethodName & " not found!"
Else
MsgBox Err.Number & " - " & Err.Description, vbCritical
End
End If
End Sub
'--------------------------------------------------------------------------------
' Events
'--------------------------------------------------------------------------------
Public Sub Speak()
DoEvent "Speak"
End Sub
Public Sub Gallop(ByVal Speed As Long)
DoEvent "Gallop", Speed
End Sub
Public Sub Fly(ByVal AirSpeedVelocity As Long)
DoEvent "Fly", AirSpeedVelocity
End Sub
'--------------------------------------------------------------------------------
' Methods
'--------------------------------------------------------------------------------
Public Sub z_Horse_Gallop(ByVal Speed As Long)
Display "'" & Me.Name & "' is galloping! " & Speed & " miles per hour."
End Sub
Public Sub z_Horse_Speak()
Display "The " & Me.Color & " horse '" & Me.Name & "' whinnies!"
End Sub
Public Sub z_Bird_Speak()
Display "The " & Me.Color & " bird '" & Me.Name & "' chirps!"
End Sub
Public Sub z_Bird_Fly(ByVal AirSpeedVelocity As Long)
Display Me.Name & " flies! " & AirSpeedVelocity & " feet per second."
End Sub
'--------------------------------------------------------------------------------
' Properties
'--------------------------------------------------------------------------------
Public Property Get Name() As String
Lookup "Name", Name
End Property
Public Property Let Name(ByVal Value As String)
Change "Name", Value
End Property
Public Property Get Color() As String
Lookup "Color", Color
End Property
Public Property Let Color(ByVal Value As String)
Change "Color", Value
End Property
Public Property Get FeatherCount() As Long
Lookup "FeatherCount", FeatherCount
End Property
Public Property Let FeatherCount(ByVal Value As Long)
Change "FeatherCount", Value
End Property