I have a vb.net class (I'll call Class1) that includes two sub routines. The first sub is called Start, and it does the general work of the class. As it does its thing, if a variable called DoDebug is set to true, it periodically calls the other sub routine called WriteDebug. WriteDebug accepts a string as a parameter and appends the string to a file.
This class normally runs as part of a windows service. For testing purposes though, I created a windows forms project with one form (Form1) and added Class1 to it. I created a button on Form1 that calls the start routine of Class1. I then created a textbox on Form1. What I would like to do is have the text box display the strings that are passed to the WriteDebug method of class1. I don't want Class1 to reference anything on the form though because Form1 won't exist when Class1 runs as part of my windows service.
I assume I would do this with delegates? Can I create a delegate sub in Class1 and invoke it in the WriteDebug routine? I have put some code below that does not work. Somebody set me straight, please! How should this be done?
Public Class Class1
'The delegate stuff in class 1:
Public Delegate Sub GetMessage(ByVal strData As String)
Private _GetMessage As GetMessage
Public Sub GetMessages(ByVal del As GetMessage)
_GetMessage = del
'The main routine in Class1
Public Sub Start()
'code here to do stuff
'The routine to invoke the delegate and pass the string to Form1
Private Sub WriteDebug(strData as String)
'code here to write to file
Public Class Form1
Private Sub Form1_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
Dim c as new Class1
Dim delegateGetMessages As New Class1.GetMessage(AddressOf ShowMessage)
Private Sub ShowMessage(ByVal strData As String)
TextBox1.Text += strData