Guest User

Untitled

a guest
Nov 20th, 2017
88
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.47 KB | None | 0 0
  1. Option Compare Database
  2. Option Explicit
  3.  
  4. Private Const mModuleName As String = "IViewEvents"
  5.  
  6. Public Sub OnBeforeDoSomething(ByVal Data As Object, ByRef Cancel As Boolean): End Sub
  7. Public Sub OnAfterDoSomething(ByVal Data As Object): End Sub
  8.  
  9. Private Sub Class_Initialize()
  10. Err.Raise 5, mModuleName, AccessError(5) & " - Interface class must not be instantiated."
  11. End Sub
  12.  
  13. Option Compare Database
  14. Option Explicit
  15.  
  16. Private Const mModuleName As String = "IViewCommands"
  17.  
  18. Public Sub DoSomething(ByVal arg1 As String, ByVal arg2 As Long): End Sub
  19.  
  20. Private Sub Class_Initialize()
  21. Err.Raise 5, mModuleName, AccessError(5) & " - Interface class must not be instantiated."
  22. End Sub
  23.  
  24. Option Compare Database
  25. Option Explicit
  26.  
  27. Private Const mModuleName As String = "ViewAdapter"
  28.  
  29. Public Event BeforeDoSomething(ByVal Data As Object, ByRef Cancel As Boolean)
  30. Public Event AfterDoSomething(ByVal Data As Object)
  31.  
  32. Private mView As IViewCommands
  33.  
  34. Implements IViewCommands
  35. Implements IViewEvents
  36.  
  37. Public Function Initialize(View As IViewCommands) As ViewAdapter
  38. Set mView = mView
  39. Set Initialize = Me
  40. End Function
  41.  
  42. Private Sub IViewCommands_DoSomething(ByVal arg1 As String, ByVal arg2 As Long)
  43. mView.DoSomething arg1, arg2
  44. End Sub
  45.  
  46. Private Sub IViewEvents_OnBeforeDoSomething(ByVal Data As Object, ByRef Cancel As Boolean)
  47. RaiseEvent BeforeDoSomething(Data, Cancel)
  48. End Sub
  49. Private Sub IViewEvents_OnAfterDoSomething(ByVal Data As Object)
  50. RaiseEvent AfterDoSomething(Data)
  51. End Sub
  52.  
  53. Option Compare Database
  54. Option Explicit
  55.  
  56. Private Const mModuleName As String = "ViewAdapter"
  57.  
  58. Private WithEvents mViewAdapter As ViewAdapter
  59.  
  60. Private mData As Object
  61.  
  62. Public Function Initialize(ViewAdapter As ViewAdapter) As Controller
  63. Set mViewAdapter = ViewAdapter
  64. Set Initialize = Me
  65. End Function
  66.  
  67. Private Sub mViewAdapter_AfterDoSomething(ByVal Data As Object)
  68. ' Do stuff
  69. End Sub
  70.  
  71. Private Sub mViewAdapter_BeforeDoSomething(ByVal Data As Object, ByRef Cancel As Boolean)
  72. Cancel = Not Data Is Nothing
  73. End Sub
  74.  
  75. Option Compare Database
  76. Option Explicit
  77. Option Private Module
  78.  
  79. Private Const mModuleName As String = "Constructors"
  80.  
  81. Public Function NewViewAdapter(View As IViewCommands) As ViewAdapter
  82. With New ViewAdapter: Set NewViewAdapter = .Initialize(View): End With
  83. End Function
  84.  
  85. Public Function NewController(ByVal ViewAdapter As ViewAdapter) As Controller
  86. With New Controller: Set NewController = .Initialize(ViewAdapter): End With
  87. End Function
  88.  
  89. Option Compare Database
  90. Option Explicit
  91.  
  92. Private Const mModuleName As String = "MyApplication"
  93.  
  94. Private mController As Controller
  95.  
  96. Public Function LaunchApp() As Long
  97. Dim frm As IViewCommands
  98. ' Open and assign frm here as instance of a Form implementing
  99. ' IViewCommands and raising events through the callback interface
  100. ' IViewEvents. It requires an initialization method (or property
  101. ' setter) that accepts an IViewEvents argument.
  102. Set mController = NewController(NewViewAdapter(frm))
  103. End Function
  104.  
  105. Option Explicit
  106. Public Sub DoSomething()
  107. End Sub
  108.  
  109. Option Explicit
  110. Implements ISomething
  111.  
  112. Private Sub ISomething_DoSomething()
  113. 'the actual implementation
  114. End Sub
  115.  
  116. Dim something As ISomething
  117. Set something = New Class1
  118. something.DoSomething
  119.  
  120. '@Folder StackOverflowDemo
  121. Public Foo As String
  122. Public Event BeforeDoSomething()
  123. Public Event AfterDoSomething()
  124.  
  125. Public Sub DoSomething()
  126. End Sub
  127.  
  128. '@Folder StackOverflowDemo
  129. Implements Class1
  130.  
  131. Private Sub Class1_DoSomething()
  132. 'method implementation
  133. End Sub
  134.  
  135. Private Property Let Class1_Foo(ByVal RHS As String)
  136. 'field setter implementation
  137. End Property
  138.  
  139. Private Property Get Class1_Foo() As String
  140. 'field getter implementation
  141. End Property
  142.  
  143. '@Folder StackOverflowDemo
  144. Implements Class1
  145. Public Event BeforeDoSomething()
  146. Public Event AfterDoSomething()
  147.  
  148. Private foo As String
  149.  
  150. Private Sub Class1_DoSomething()
  151. RaiseEvent BeforeDoSomething
  152. 'do something
  153. RaiseEvent AfterDoSomething
  154. End Sub
  155.  
  156. Private Property Let Class1_Foo(ByVal RHS As String)
  157. foo = RHS
  158. End Property
  159.  
  160. Private Property Get Class1_Foo() As String
  161. Class1_Foo = foo
  162. End Property
  163.  
  164. '@Folder StackOverflowDemo
  165. Option Explicit
  166. Private WithEvents SomeClass2 As Class2 ' Class2 is a "concrete" implementation
  167.  
  168. Public Sub Test(ByVal implementation As Class1) 'Class1 is the interface
  169. Set SomeClass2 = implementation ' will not work if the "real type" isn't Class2
  170. foo.DoSomething ' runs whichever implementation of the Class1 interface was supplied
  171. End Sub
  172.  
  173. Private Sub SomeClass2_AfterDoSomething()
  174. 'handle AfterDoSomething event of Class2 implementation
  175. End Sub
  176.  
  177. Private Sub SomeClass2_BeforeDoSomething()
  178. 'handle BeforeDoSomething event of Class2 implementation
  179. End Sub
  180.  
  181. Option Explicit
  182.  
  183. Sub Main()
  184.  
  185. Dim oClient As Client
  186. Set oClient = New Client
  187.  
  188. oClient.Run
  189.  
  190.  
  191. End Sub
  192.  
  193. Option Explicit
  194.  
  195. Implements IEventListener
  196.  
  197. Private Sub IEventListener_SomethingHappened(ByVal vSomeParam As Variant)
  198. Debug.Print "IEventListener_SomethingHappened " & vSomeParam
  199. End Sub
  200.  
  201. Public Sub Run()
  202.  
  203. Dim oEventEmitter As EventEmitter
  204. Set oEventEmitter = New EventEmitter
  205.  
  206. oEventEmitter.ServerDoWork Me
  207.  
  208.  
  209. End Sub
  210.  
  211. Option Explicit
  212.  
  213. Public Sub SomethingHappened(ByVal vSomeParam As Variant)
  214.  
  215. End Sub
  216.  
  217. Option Explicit
  218.  
  219. Public Sub ServerDoWork(ByVal itfCallback As IEventListener)
  220.  
  221. Dim lLoop As Long
  222. For lLoop = 1 To 3
  223. Application.Wait Now() + CDate("00:00:01")
  224. itfCallback.SomethingHappened lLoop
  225. Next
  226.  
  227. End Sub
  228.  
  229. [
  230. uuid(0EA530DD-5B30-4278-BD28-47C4D11619BD),
  231. hidden,
  232. custom(0F21F359-AB84-41E8-9A78-36D110E6D2F9, "Microsoft.Office.Interop.Access._FormEvents")
  233.  
  234. ]
  235. dispinterface _FormEvents2 {
  236. properties:
  237. methods:
  238. [id(0x00000813), helpcontext(0x00003541)]
  239. void Load();
  240. [id(0x0000080a), helpcontext(0x00003542)]
  241. void Current();
  242. '/* omitted lots of other events for brevity */
  243. };
  244.  
  245. [
  246. uuid(7398AAFD-6527-48C7-95B7-BEABACD1CA3F),
  247. helpcontext(0x00003576)
  248. ]
  249. coclass Form {
  250. [default] interface _Form3;
  251. [source] interface _FormEvents;
  252. [default, source] dispinterface _FormEvents2;
  253. };
  254.  
  255. ' clsHUMAN
  256.  
  257. Public Property Let FirstName(strFirstName As String)
  258. End Property
  259.  
  260. ' clsEmployee
  261.  
  262. Implements clsHUMAN
  263.  
  264. Event evtNameChange()
  265.  
  266. Private Property Let clsHUMAN_FirstName(RHS As String)
  267. UpdateHRDatabase
  268. RaiseEvent evtNameChange
  269. End Property
  270.  
  271. Private WithEvents Employee As clsEmployee
  272.  
  273. Private Sub Employee_evtNameChange()
  274. Me.cmdSave.Enabled = True
  275. End Sub
Add Comment
Please, Sign In to add comment