Note that there are some explanatory texts on larger screens.

plurals
  1. POConverting event-handling code from C# to VB.NET
    primarykey
    data
    text
    <p>I am trying to convert this code written in C# to VB:</p> <pre class="lang-cs prettyprint-override"><code>// Initialize the Message Broker Events (Application.Current as App).MessageBroker.MessageReceived += new MessageReceivedEventHandler(MessageBroker_MessageReceived); (Application.Current as App).MessageBroker.MessageReceived += new MessageReceivedEventHandler(MessageBroker_SpecialMessageReceived); </code></pre> <p>This is what I have currently, but it always throws an error when I run it:</p> <pre class="lang-vb prettyprint-override"><code>' Initialize the Message Broker Events AddHandler TryCast(Application.Current, App).MessageBroker.MessageReceived, AddressOf MessageBroker_MessageReceived AddHandler TryCast(Application.Current, App).MessageBroker.MessageReceived, AddressOf MessageBroker_SpecialMessageReceived </code></pre> <p>Is there something that I am doing wrong?</p> <p>Here is the rest of my code:</p> <pre class="lang-vb prettyprint-override"><code>Partial Public Class MainWindow Inherits Window Public Sub New() InitializeComponent() ' Initialize the Message Broker Events 'AddHandler TryCast(Application.Current, App).MessageBroker.MessageReceived, AddressOf MessageBroker_MessageReceived 'AddHandler TryCast(Application.Current, App).MessageBroker.MessageReceived, AddressOf MessageBroker_SpecialMessageReceived TryCast(Application.Current, App).MessageBroker.MessageReceived += New MessageReceivedEventHandler(MessageBroker_MessageReceived) TryCast(Application.Current, App).MessageBroker.MessageReceived += New MessageReceivedEventHandler(MessageBroker_SpecialMessageReceived) End Sub Private Sub MessageBroker_MessageReceived(ByVal sender As Object, ByVal e As MessageBrokerEventArgs) ' Use this event to receive all messages Select Case e.MessageName.ToLower() Case "message1" ' Do something with this message Exit Select Case "message2" ' Do something with this message Exit Select Case "etc." ' Do something with this message Exit Select Case Else If Not String.IsNullOrEmpty(e.MessageObject.MessageBody) Then MessageBox.Show(e.MessageObject.MessageBody) End If Exit Select End Select End Sub Private Sub MessageBroker_SpecialMessageReceived(ByVal sender As Object, ByVal e As MessageBrokerEventArgs) ' Use this event to receive any special message objects If TypeOf e.MessageObject Is MySpecialMessage Then MessageBox.Show(DirectCast(e.MessageObject, MySpecialMessage).SpecialMessage) End If End Sub </code></pre>
    singulars
    1. This table or related slice is empty.
    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.
 

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