1

I am trying to convert this code written in C# to VB:

// 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);

This is what I have currently, but it always throws an error when I run it:

' 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

Is there something that I am doing wrong?

Here is the rest of my 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
Cody Gray - on strike
  • 239,200
  • 50
  • 490
  • 574
adam bond
  • 19
  • 2
  • [This site](http://www.developerfusion.com/tools/convert/csharp-to-vb/) is a good tool for converting .net languages. – Jim Dec 16 '11 at 14:40

3 Answers3

3

Try this:

TryCast(Application.Current, App).MessageBroker.MessageReceived += New MessageReceivedEventHandler(MessageBroker_MessageReceived)
 TryCast(Application.Current, App).MessageBroker.MessageReceived += New  MessageReceivedEventHandler(MessageBroker_SpecialMessageReceived)

I used http://converter.telerik.com/ which usually works well for me

Leopold Stotch
  • 1,452
  • 2
  • 13
  • 24
  • same here.. that's how I got the answer quickly.. LOL – MethodMan Dec 15 '11 at 20:46
  • No that didn't work. I also use that site and it is pretty good, I agree. But what I am getting with your code is that I cannot call the event directly. That is why I tried to add a handler instead. – adam bond Dec 15 '11 at 20:49
  • 1
    can you post the exact code that you are using.. perhaps we are not seeing some Encapsulated property's or Class Access levels – MethodMan Dec 15 '11 at 20:50
0

try this and see if it will work

TryCast(Application.Current, App).MessageBroker.MessageReceived += New MessageReceivedEventHandler(MessageBroker_MessageReceived)
TryCast(Application.Current, App).MessageBroker.MessageReceived += New MessageReceivedEventHandler(MessageBroker_SpecialMessageReceived)
MethodMan
  • 18,625
  • 6
  • 34
  • 52
-1

First of all, the C# code isn't correct.

// 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);

it should be,

// Initialize the Message Broker Events
((App)Application.Current).MessageBroker.MessageReceived +=
    new MessageReceivedEventHandler(MessageBroker_MessageReceived);

((App)Application.Current).MessageBroker.MessageReceived +=
    new MessageReceivedEventHandler(MessageBroker_SpecialMessageReceived);

and the VB would be,

CType(Application.Current, App).MessageBroker.MessageReceived +=
    New MessageReceivedEventHandler(MessageBroker_MessageReceived)

CType(Application.Current, App).MessageBroker.MessageReceived +=
    New MessageReceivedEventHandler(MessageBroker_SpecialMessageReceived)

as and TryCast first check the type of the instance to see if it matches the cast type. If it doesn't, it'll return null, and you'll get a NullReferenceException. Instead, you should be casting the type directly, either using (Type)instance or CType(instance, Type). Not that's going to make a difference, logically, but you should still understand the difference. :)

cwharris
  • 17,835
  • 4
  • 44
  • 64