2011-12-15 8 views
1

のC#からイベント処理コードの変換:私はVBにC#で書かれたこのコードを変換しようとしていますVB.NET

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

これは私が現在持っているものですが、私が実行したときには、常にエラーをスローしますそれ:

' 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 

私が間違っていることはありますか?

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 
+0

[このサイト](http://www.developerfusion.com/tools/convert/csharp-to-vb/)は、.net言語を変換するための優れたツールです。 – Jim

答えて

3

これを試してみてください:

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

を、私は通常、私

+0

ここに同じ..それは私がすぐに答えを得た方法です。LOL – MethodMan

+0

私はあなたにUpVote – MethodMan

+0

を与えました。偉大な頭脳.. –

0

これを試してみて、それが動作するかどうかを確認するためにうまく機能http://converter.telerik.com/を使用ここで

は、私のコードの残りの部分であります

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

まず、C#コードが正しくありません。

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

それは、

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

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

すべきであり、VBは次のようになり、

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

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

asTryCast最初のそれは、キャストタイプと一致するかどうかを確認するために、インスタンスの種類を確認してください。そうでなければ、nullが返され、NullReferenceExceptionが発生します。代わりに、(Type)instanceまたはCType(instance, Type)を使用して型を直接キャストする必要があります。論理的に違いはありませんが、その違いをまだ理解しておく必要があります。 :)

関連する問題