Passing A Delegate As a Parameter - VB.NET and C#Pass a Delegate to a Separate Assembly | | Can I pass a Delegate as a parameter to another assembly? Yes; this article will show you how. In the Print_Page Event of a PrintDocument object, I need to make a CallBack to the calling program and have it return the next line to print. Raising an Event might be a bit simpler but the event handler can only return void (C#) or can only be a Sub in VB.NET. Therefore, I must use a Delegate.
In this article I am going to create a Console Application as the main program and a Class Library (DLL) to do the printing. I won't actually do any printing, but you will be able see the principle. First I will show the code for the DLL Assembly.
Public Class PrintClass
Public Delegate Function GiveMeAString() As String
Private _getStringDelegate As GiveMeAString
Public Sub New(ByVal getStringDelegate As GiveMeAString)
_getStringDelegate = getStringDelegate
End Sub
Public Sub StartPrint()
' In the print event
Dim nextLine As String = String.Empty
Do While nextLine IsNot Nothing
nextLine = _getStringDelegate()
Console.WriteLine(nextLine)
Loop
End Sub
End Class
|
In the PrintClass, shown above, I declare a Public Delegate Function so that the calling main application will be able to pass in the address of a function in it. Be aware that the CallBack Function must have the same signature as the Delegate in the DLL. If they do not have the same signature, you will receive a compiler error of "Error Method 'Private Function GemmeString(s As String) As String' does not have the same signature as delegate 'Delegate Function GiveMeAString() As String' "
When creating an instance of the PrintClass, I will pass the AddressOf a Function that has the same signature as the delegate. The constructor of the PrintClass guarantees that the signature is the same as the Delegate Function, GiveMeAString, by specifying a parameter as TypeOf the GiveMeAString Delegate.
Finally, the StartPrint Method of the PrintClass will continue to invoke the Delegate simply by referencing the _getStringDelegate variable.
Next, the code shown below is the complete code for the main console application. It will instantiate an instance of the PrintClass Class passing the parameter described above.
Imports DelegateDLL
Module Module1
Dim nbrLinesToPrint As Integer = 10
Dim lineCount As Integer = 0
Sub Main()
Dim docPrt As PrintClass = New PrintClass(AddressOf GemmeString)
docPrt.StartPrint()
End Sub
Private Function GemmeString() As String
Do While lineCount < nbrLinesToPrint<BR>
lineCount += 1
Return "My Print String " & lineCount.ToString
Loop
Return Nothing
End Function
End Module
|
To reiterate, the compiler will verify that the GemmeString Function of the main app has the same signature as the Delegate in the PrintClass because of the type of the parameter in the DLL's Constructor.
The main app simply creates an instance of PrintClass and then calls the StartPrint method of the object. Its GemmeString Function is called back from the PrintClass and returns strings to be printed until the loop is exhausted. At that point it returns Nothing telling the PrintClass that there is no more data to print. Obviously, I don't pass an empty string because, in real life, a blank line is something that we would want to be able to print.
The following output will be found in the Console Output Window when you execute the application.
My Print String 1
My Print String 2
My Print String 3
My Print String 4
My Print String 5
My Print String 6
My Print String 7
My Print String 8
My Print String 9
My Print String 10
I want to thank my friend Heath Turnage for helping me with this code.
Have you tried our newest product, Visual Class Organizer? You'll be amazed how easy it is to keep the code in your code windows organized. TRY IT FREE FOR 30 DAYS BY CLICKING HERE.
If you are developing in C# and haven't tried CSharpCompleter, you are wasting valuable time typing hundreds of braces {} daily needlessly. Try CSharpCompleter for 30 DAYS FREE.
| Ask a Question, or give your feedback on my articles or products by going to the KnowDotNet Forum or by clicking on My Blog. |  |
|