Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Here's how I would rewrite it:</p> <pre><code>Class Form1 Private Network as NetworkModule Public NotifyDelegate As NetworkModule.NotifyDelegate Public Sub New() NotifyDelegate = New NetworkModule.NotifyDelegate(AddressOf Notify) End Sub Public Sub Button1_Click(Sender, e) Handles Button1.Click Network = New NetworkModule("127.0.0.1", 1234, Me) End Sub Public Sub Notify(Msg As String) txtLog.Text &amp;= Msg &amp; vbCrLf End Sub End Class 'Form1 </code></pre> <p>NetworkModule Class (partial):</p> <pre><code>Class NetworkModule Public Delegate Sub NotifyDelegate(Msg as String) Private Sub Notify(Msg as String) If m_Form.InvokeRequired Then m_Form.Invoke(Form1.NotifyDelegate, Msg) Else m_Form.Notify(Msg) End If End Sub 'Notify Private mSocket as Socket Private m_Form As Form1 Public Sub New(IP as string, Port as Integer, oForm As Form1) m_Form = oForm Dim remoteEP as New IPEndpoint(IP.Parse(IP), Port) mSocket = New Socket(internetwork, stream, tcp) mSocket.BeginConnect(remoteEP, New AsyncCallback(AddressOf onConnect), mSocket) Notify("Connection to " &amp; remoteEP.ToString) 'This one works End Sub 'New </code></pre> <p><strong>Update with Interface approach</strong></p> <p>A better mechanism than passing the form itself is to implement an interface. To do this, first create the interface definition (note that the delegate has moved to the Interface for convenience):</p> <pre><code>Public Interface INotify Sub Notify(Msg As String) Delegate Sub NotifyDelegate(Msg As String) End Interface </code></pre> <p>Then Implement the interface in the Form. Note that the form now determines whether or not Invoke is required. This allows the INotify interface to be used in non-UI scenarios, such as logging to disk or the event log.</p> <pre><code>Public Class Form1 Implements INotify Public Sub Notify(Msg As String) txtLog.Text &amp;= Msg &amp; vbCrLf End Sub Private Sub INotify_Notify(Msg As String) Implements INotify.Notify If Me.InvokeRequired Then Me.Invoke(New INotify.NotifyDelegate(AddressOf Notify), Msg) Else Me.Notify(Msg) End If End Sub 'Notify Private Network As NetworkModule Public Sub Button1_Click(Sender, e) Handles Button1.Click Network = New NetworkModule("127.0.0.1", 1234, Me) End Sub End Class 'Form1 </code></pre> <p>Finally, store a reference to the INotify interface instead of the Form in NetworkModule (note the NetworkModule no longer needs to know or care that an Invoke may be required):</p> <pre><code>Public Class NetworkModule Public Delegate Sub NotifyDelegate(Msg As String) Private m_Notifier As INotify Private Sub Notify(Msg As String) m_Notifier.Notify(Msg) End Sub 'Notify Public Sub New(IP As String, Port As Integer, oNotifier As INotify) m_Notifier = oNotifier ' The addition code here End Sub 'New End Class </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload