Namespace DelegateTest

Delegate Sub Callback1()
Delegate Function Callback2() As Long
Delegate Sub Callback3( a As Long )
Delegate Sub Callback4( a As String )
Delegate Function Callback7( str As String ) As String


Class TestClass
	proc1Flag As Boolean
	proc2Flag As Boolean
	proc3Flag As Boolean
	proc4Flag As Boolean
	proc5Flag As Boolean
	proc6Flag As Boolean
	proc8Flag As Boolean

	Static staticProc1Flag = False As Boolean

Public
	Sub TestClass()
		ClearFlags()
	End Sub

	Sub ClearFlags()
		proc1Flag = False
		proc2Flag = False
		proc3Flag = False
		proc4Flag = False
		proc5Flag = False
		proc6Flag = False
		proc8Flag = True
	End Sub
	Function CheckFlags() As Boolean
		Return proc1Flag and proc2Flag and proc3Flag and proc4Flag and proc5Flag and proc6Flag and proc8Flag
	End Function

	Sub Proc1()
		proc1Flag = True
	End Sub

	Function Proc2() As Long
		proc2Flag = True
	End Function

	Sub Proc3or4( a As Long )
		proc3Flag = (a=100)
	End Sub

	Sub Proc3or4( a As String )
		proc4Flag = (a="Proc4 calling")
	End Sub

	Sub Proc5( a As String )
		proc5Flag = (a="Proc4 calling")
	End Sub

	Sub Proc6( a As String )
		proc6Flag = (a="Proc4 calling")
	End Sub

	Static Function Proc7( str As String ) As String
		Return "Proc7 result[" + str + "]"
	End Function

	Sub Proc8( a As String )
		proc8Flag = False
	End Sub
End Class

Sub TestMain()
	Dim testObj As TestClass
	Dim dg1 = New Callback1( AddressOf(testObj.Proc1) )
	Dim dg2 = New Callback2( AddressOf(testObj.Proc2) )
	Dim dg3 = New Callback3( AddressOf(testObj.Proc3or4) )
	Dim dg4 = New Callback4( AddressOf(testObj.Proc3or4) )
	dg4 += AddressOf(testObj.Proc5)
	dg4 += AddressOf(testObj.Proc6)
	dg4 += AddressOf(testObj.Proc8)
	dg4 -= AddressOf(testObj.Proc8)

	dg1()
	dg2()
	dg3( 100 )
	dg4( "Proc4 calling" )

	UnitTest( "Delegate1", testObj.CheckFlags() )

	Dim dg7 = New Callback7( AddressOf(TestClass.Proc7) )
	UnitTest( "Delegate2", dg7( "test value" ) = "Proc7 result[test value]" )

End Sub

End Namespace

DelegateTest.TestMain()
