Chris Roberts
Chris Roberts

Reputation: 18802

Very Specific C# to VB.NET Conversion Problem

I am currently working on a project which uses the AutoFac Inversion of Control container.

I am attempting to convert some example code from C# into a codebase of an existing project of mine which is written in VB.NET and I've hit a problem.

The original line of code is:

EventHub.Subscribe<HandshakingEvent>(container.Resolve<HandshakeAuthenticator>().CheckHandshake);

Which I have converted to:

EventHub.Subscribe(Of HandshakingEvent)(Container.Resolve(Of HandshakeAuthenticator)().CheckHandshake)

But - this is causing an error, "Argument not specified for parameter 'ev' of CheckHandshake".

The type of the parameter for the EventHub.Subscribe(Of HandshakingEvent) procedure is System.Action (of HandshakingEvent)

I can see what the problem is, I'm just not really sure what to do about it! I've tried using 'AddressOf', but that doesn't seem to work, either.

Thanks in advance... - Chris

Upvotes: 3

Views: 203

Answers (2)

Guffa
Guffa

Reputation: 700342

The VB code is trying to call the method instead of creating a delegate for it. Use the AddresOf operator to get a deletegate:

EventHub.Subscribe(Of HandshakingEvent)(AddressOf Container.Resolve(Of HandshakeAuthenticator)().CheckHandshake)

The keyword is not needed in C#, as parentheses are always used when you call a method, but in VB you can call a method without parentheses also.

Upvotes: 3

SLaks
SLaks

Reputation: 887453

Try

EventHub.Subscribe(Of HandshakingEvent)(AddressOf Container.Resolve(Of HandshakeAuthenticator)().CheckHandshake)

(using the AddressOf keyword to get a delegate)

Upvotes: 5

Related Questions