[340] | 1 | Namespace DelegateTest
|
---|
| 2 |
|
---|
| 3 | Delegate Sub Callback1()
|
---|
| 4 | Delegate Function Callback2() As Long
|
---|
| 5 | Delegate Sub Callback3( a As Long )
|
---|
| 6 | Delegate Sub Callback4( a As String )
|
---|
[342] | 7 | Delegate Function Callback7( str As String ) As String
|
---|
[340] | 8 |
|
---|
| 9 |
|
---|
| 10 | Class TestClass
|
---|
| 11 | proc1Flag As Boolean
|
---|
| 12 | proc2Flag As Boolean
|
---|
| 13 | proc3Flag As Boolean
|
---|
| 14 | proc4Flag As Boolean
|
---|
| 15 | proc5Flag As Boolean
|
---|
| 16 | proc6Flag As Boolean
|
---|
[352] | 17 | proc8Flag As Boolean
|
---|
[340] | 18 |
|
---|
| 19 | Static staticProc1Flag = False As Boolean
|
---|
| 20 |
|
---|
| 21 | Public
|
---|
| 22 | Sub TestClass()
|
---|
| 23 | ClearFlags()
|
---|
| 24 | End Sub
|
---|
| 25 |
|
---|
| 26 | Sub ClearFlags()
|
---|
| 27 | proc1Flag = False
|
---|
| 28 | proc2Flag = False
|
---|
| 29 | proc3Flag = False
|
---|
| 30 | proc4Flag = False
|
---|
| 31 | proc5Flag = False
|
---|
| 32 | proc6Flag = False
|
---|
[352] | 33 | proc8Flag = True
|
---|
[340] | 34 | End Sub
|
---|
| 35 | Function CheckFlags() As Boolean
|
---|
[352] | 36 | Return proc1Flag and proc2Flag and proc3Flag and proc4Flag and proc5Flag and proc6Flag and proc8Flag
|
---|
[340] | 37 | End Function
|
---|
| 38 |
|
---|
| 39 | Sub Proc1()
|
---|
| 40 | proc1Flag = True
|
---|
| 41 | End Sub
|
---|
| 42 |
|
---|
[343] | 43 | Function Proc2() As Long
|
---|
[340] | 44 | proc2Flag = True
|
---|
[343] | 45 | End Function
|
---|
[340] | 46 |
|
---|
| 47 | Sub Proc3or4( a As Long )
|
---|
| 48 | proc3Flag = (a=100)
|
---|
| 49 | End Sub
|
---|
| 50 |
|
---|
| 51 | Sub Proc3or4( a As String )
|
---|
| 52 | proc4Flag = (a="Proc4 calling")
|
---|
| 53 | End Sub
|
---|
| 54 |
|
---|
| 55 | Sub Proc5( a As String )
|
---|
| 56 | proc5Flag = (a="Proc4 calling")
|
---|
| 57 | End Sub
|
---|
| 58 |
|
---|
| 59 | Sub Proc6( a As String )
|
---|
| 60 | proc6Flag = (a="Proc4 calling")
|
---|
| 61 | End Sub
|
---|
| 62 |
|
---|
[342] | 63 | Static Function Proc7( str As String ) As String
|
---|
| 64 | Return "Proc7 result[" + str + "]"
|
---|
[340] | 65 | End Function
|
---|
[352] | 66 |
|
---|
| 67 | Sub Proc8( a As String )
|
---|
| 68 | proc8Flag = False
|
---|
| 69 | End Sub
|
---|
[340] | 70 | End Class
|
---|
| 71 |
|
---|
| 72 | Sub TestMain()
|
---|
| 73 | Dim testObj As TestClass
|
---|
| 74 | Dim dg1 = New Callback1( AddressOf(testObj.Proc1) )
|
---|
| 75 | Dim dg2 = New Callback2( AddressOf(testObj.Proc2) )
|
---|
| 76 | Dim dg3 = New Callback3( AddressOf(testObj.Proc3or4) )
|
---|
| 77 | Dim dg4 = New Callback4( AddressOf(testObj.Proc3or4) )
|
---|
| 78 | dg4 += AddressOf(testObj.Proc5)
|
---|
| 79 | dg4 += AddressOf(testObj.Proc6)
|
---|
[352] | 80 | dg4 += AddressOf(testObj.Proc8)
|
---|
| 81 | dg4 -= AddressOf(testObj.Proc8)
|
---|
[340] | 82 |
|
---|
| 83 | dg1()
|
---|
| 84 | dg2()
|
---|
| 85 | dg3( 100 )
|
---|
| 86 | dg4( "Proc4 calling" )
|
---|
| 87 |
|
---|
| 88 | UnitTest( "Delegate1", testObj.CheckFlags() )
|
---|
[342] | 89 |
|
---|
[340] | 90 | Dim dg7 = New Callback7( AddressOf(TestClass.Proc7) )
|
---|
[342] | 91 | UnitTest( "Delegate2", dg7( "test value" ) = "Proc7 result[test value]" )
|
---|
| 92 |
|
---|
[340] | 93 | End Sub
|
---|
| 94 |
|
---|
| 95 | End Namespace
|
---|
| 96 |
|
---|
| 97 | DelegateTest.TestMain()
|
---|