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