Advertisement
Guest User

Untitled

a guest
Oct 24th, 2014
120
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.25 KB | None | 0 0
  1. VERSION 1.0 CLASS
  2. BEGIN
  3. MultiUse = -1 'True
  4. END
  5. Attribute VB_Name = "Constructor"
  6. Attribute VB_GlobalNameSpace = False
  7. Attribute VB_Creatable = False
  8. Attribute VB_PredeclaredId = True
  9. Attribute VB_Exposed = False
  10. Option Explicit
  11.  
  12. Private myName as String
  13.  
  14. Private Sub Class_Initialize()
  15. myName = Cstr(Int((100) * rnd + 1))
  16. End Sub
  17.  
  18. Public Property Get Name() as String
  19. Name = myName
  20. End Property
  21.  
  22. Public Property Get PieceOfMe(Optional value as String) As Constructor
  23. Attribute Item.VB_UserMemId = 0
  24. myName = value
  25. Set PieceOfMe = Me
  26. End Property
  27.  
  28. Option Explicit
  29.  
  30. ' vba4all.com
  31. Public Enum TestType
  32. eStatic
  33. eInstance
  34. [_Min] = eStatic
  35. [_Max] = eInstance
  36. End Enum
  37.  
  38. Sub Main()
  39.  
  40. ' Debug.Print Constructor
  41. ' *NOTE : fails because Constructor's default GET returns an instance of Constructor
  42. ' Constructor.Name = "newName"
  43. ' *NOTE2 : Explicit assignment of the .Name property not possible (removed to improve the class encapsulation)
  44. ' : To assign a new name you have to use the default property i.e Set T = T("New Name")
  45.  
  46. Dim testOption As TestType
  47. Dim i As Long
  48.  
  49. ' * I am only presenting 2 tests so the Enum thingy is a bit of an overkill
  50. For i = TestType.[_Min] To TestType.[_Max]
  51. testOption = i
  52.  
  53. ' reset static defualt
  54. Set Constructor = New Constructor
  55.  
  56. ' Singleton pattern - as a VBA static class
  57. If testOption = eStatic Then
  58. Debug.Print "***STATIC"
  59.  
  60. ' whenever calling this it will always return the defualt (random number)
  61. Debug.Print Constructor.Name, "Default call to Constructor.Name"
  62. ' unless it has been changed by one of the following calls throughout the life of the Excel application
  63. ' *this call pretty much means that if you ever change the default this call will always reflect the last change
  64.  
  65. ' ****
  66. ' *** SETTING THE DEFAULT OR AN INSTANCE BY CALLING THE CLASS NAME AND PASSING PARAMETERS
  67. Set Constructor = Constructor("foo")
  68. Debug.Print Constructor.Name, "Set Constructor = Constructor(""foo"") and debug call Constructor.Name"
  69.  
  70. ' ** however, you can reset the Constructor.Name (ask for a new, default by calling
  71. Set Constructor = New Constructor
  72. Debug.Print Constructor.Name, "Set Constructor = new Constructor -> [reset] assigned a new random"
  73.  
  74. ' Working with INSTANCES
  75. Else
  76. Debug.Print "***INSTANCES"
  77.  
  78. Debug.Print Constructor.Name, "Constructor.Name -> Current static default [reseted]"
  79.  
  80. ' create an new variable of Test type
  81. Dim T As Constructor
  82.  
  83. ' Assing to the currently stored value
  84. Set T = Constructor
  85. Debug.Print T.Name, "Set T = Constructor -> Should equal the current default"
  86.  
  87. ' Create a new Constructor instance ( grabs the new random number )
  88. Set T = New Constructor
  89. ' or
  90. 'Dim T1 As New Constructor - same behaviour (grabs new random) but one line declaration
  91. Debug.Print T.Name, "Set T = new Constructor - > new random for this instance"
  92. ' *NOTE - it's possible that this number is the same as previous because the Random Range is 100
  93.  
  94. ' *** PARAMETARISED CONSTRUTOR
  95. ' new instance calling the static name and passing a parameter
  96. ' that changes the default as well as the instance
  97. Dim T1 As Constructor
  98. Set T1 = Constructor("fooInst")
  99.  
  100. Debug.Print T1.Name, "T1 = Constructor(""fooInst"") -> assigned through the static class"
  101. Debug.Print Constructor.Name, "Constructor.Name -> static is now equal to what T1 is"
  102.  
  103. ' another separate instance using NEW Constructor
  104. Dim T2 As New Constructor
  105. Debug.Print T2.Name, "Dim T2 as New Test = just to verify, a new instance via a NEW Constructor"
  106. Debug.Print Constructor.Name, "Constructor.Name -> did NOT change, still equal to T1 due to the last assignment"
  107.  
  108. ' changing instance via another instance
  109. Set T1 = T2
  110. Debug.Print T1.Name, "T1 result of: Set T1 = T2"
  111. Debug.Print T2.Name, "T2 result of: Set T1 = T2 -> T1 and T2 are equal"
  112.  
  113. ' changing instance via another instance + PARAMETER
  114. Set T1 = T2("both")
  115. Debug.Print T1.Name, "T1.Name -> due to: Set T1 = T2(""both"")"
  116. Debug.Print T2.Name, "T2.Name -> due to: Set T1 = T2(""both"") - > T1 and T2 are now equal as T1 points to the T2"
  117. Debug.Print Constructor.Name, "Constructor.Name -> did NOT change, still equal to itself because it hasn't been used by other instances"
  118.  
  119. End If
  120. Next
  121.  
  122. End Sub
  123.  
  124. VERSION 1.0 CLASS
  125. BEGIN
  126. MultiUse = -1 'True
  127. END
  128. Attribute VB_Name = "MultipleParams"
  129. Attribute VB_GlobalNameSpace = False
  130. Attribute VB_Creatable = False
  131. Attribute VB_PredeclaredId = True
  132. Attribute VB_Exposed = False
  133. Option Explicit
  134.  
  135. Private myName as String
  136. Private myNumber as Long
  137.  
  138. Private Sub Class_Initialize()
  139. myName = Cstr(Int((100) * rnd + 1))
  140. End Sub
  141.  
  142. Public Property Get Name() as String
  143. Name = myName
  144. End Property
  145.  
  146. Public Property Get PieceOfMe(Optional newName as String, Optional theNumber as Long) As MultipleParams
  147. Attribute PieceOfMe.VB_UserMemId = 0
  148. myName = newName
  149. myNumber = theNumber
  150. Set PieceOfMe = Me
  151. End Property
  152.  
  153. Sub Main()
  154.  
  155. Dim multiParams As MultipleParams
  156. Set multiParams = MultipleParams("the Name", 1)
  157.  
  158. End Sub
  159.  
  160. VERSION 1.0 CLASS
  161. BEGIN
  162. MultiUse = -1 'True
  163. END
  164. Attribute VB_Name = "ParamArrayConstructor"
  165. Attribute VB_GlobalNameSpace = False
  166. Attribute VB_Creatable = False
  167. Attribute VB_PredeclaredId = True
  168. Attribute VB_Exposed = False
  169. Option Explicit
  170.  
  171. Private Sub Class_Initialize()
  172. myName = Cstr(Int((100) * rnd + 1))
  173. End Sub
  174.  
  175. Public Property Get Name() as String
  176. Name = myName
  177. End Property
  178.  
  179. Public Property Get PieceOfMe(paramArray arr() as Variant) As ParamArrayConstructor
  180. Attribute PieceOfMe.VB_UserMemId = 0
  181. ' handle the paramArray
  182. Dim v as Variant
  183. For each v in arr
  184. Debug.? v
  185. Next
  186. Set PieceOfMe = Me
  187. End Property
  188.  
  189. Sub Main()
  190.  
  191. Dim param As ParamArrayConstructor
  192. Set param = ParamArrayConstructor("oh", "my", "lord", "paramarray", "works too!")
  193.  
  194. End Sub
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement