0

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

Chris Allison
  • 73
  • 1
  • 10

2 Answers2

0

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.)

Aasmund Eldhuset
  • 37,289
  • 4
  • 68
  • 81
  • Thanks @Aasmund @JasonG there was a big aaaah thats what it is! The only issue I have is with VB not been able to use interfaces without creating explicit implementations of them ... just going to implement in c# – Chris Allison Jun 24 '11 at 20:42
0

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.

JasonG
  • 922
  • 6
  • 8