简体   繁体   中英

using nservicebus in vb.net

Im trying to convert the nServiceBus PubSub .net4 example into vb and I'm struggling at one point which I think is a language issue but I thought I would ask the experts.

The code in question is from the publisher:

var eventMessage = publishIEvent ? Bus.CreateInstance<IEvent>() : new EventMessage();

When I try and run this in vb with

 Public Property Bus As IBus
 Dim eM As New EventMessage()
            eM = Bus.CreateInstance(Of IEvent)()

I get a object refrence not set to an instance of the object error

But then I get an error saying I cant use new on an interface which iBus is

any ideas on how I get around this?

Given the similarities between c# and vb.net I cant believe this isnt possible?

Any ideas welcome

Thanks

Chris

The two parts of the conditional do not have the same type, but they are both assignable to IEvent (I believe), which is the type the C# compiler will make eventMessage have. Try this:

Dim eM as IEvent
If publishIEvent Then
    eM = Bus.CreateInstance(Of IEvent)()
Else
    eM = New EventMessage()
End If

(probably not entirely correct syntax; my VB is getting rusty...)

(By the way, I'd suggest using the name eventMessage in stead of eM .)

The C# code above is a if-then structure. I don't have the code in front of me, but the line is essentially shorthand for:

If (publishIEvent == true)
{
   var eventMessage = Bus.CreateInstance<IEvent>()
}
else
{
   var eventMessage = new EventMessage();
}

Hope this helps solve the issue.

FYI, I realize the code above is not syntactically correct, just trying to illustrate the point of the C# statement.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM