C# code works but VB.NET shows a precompile error

C

Cedric B

I have the following code in C#, and it works fine. After porting the code
to VB.NET, I get a precompile error in the IDE. Does anyone know why this
could be happening?

protected delegate void MulticastDelegate(object sender, EventArgs e);

virtual public void Subscribe(object tag, EventHandler _eventHandler, bool
loose)
{

//... other code above omitted
// create the delegate for this event handler
MulticastDelegate newDelegate=new MulticastDelegate(_eventHandler);

//...other code below omitted
}



VB.NET Translation:

Protected Delegate Sub MulticastDelegate(ByVal sender As Object, ByVal e As
EventArgs)

Public Overridable Overloads Sub Subscribe(ByVal tag As Object, ByVal
mEventHandler As EventHandler, ByVal loose As Boolean)

'the line below is giving the pre-compile error
Dim newDelegate As MulticastDelegate = New
MulticastDelegate(mEventHandler)
End Sub 'Subscribe

The precompile error is as follows:

'MulticastDelegate' is a delegate type. Delegate construction permits only
a single AddressOf expression as an argument list. Often an AddressOf
expression can be used instead of a delegate construction.
 
G

Guest

VB.NET is not my strongest side but I think you have to use the AddressOf operator:

Dim newDelegate As MulticastDelegate
= New MulticastDelegate(AddressOf mEventHandler)

Regards, Jakob.
 
P

prasad_chandi

there is c# is case sensitive but i compile in vb.net


**********************************************************************
Sent via Fuzzy Software @ http://www.fuzzysoftware.com/
Comprehensive, categorised, searchable collection of links to ASP & ASP.NET resources...
 
C

Cedric Bertolasio

If you do re-write the code in VB.NET as shown below, you get a different
pre compile error. mEventHandler is a delegate type. The C# code is
basically creating a new instance of the delegate type MulticastDelegate and
assigning it to the delegate passed in mEventHandler.

I just cant figure out how to do the same thing in VB.NET.
 
G

Guest

Maybe this will work:

Dim newDelegate As MulticastDelegate = MulticastDelegate.CreateDelegate(GetType(MulticastDelegate), mEventHandler.Method)

But I do think that it will only work, if mEventHandler.Method represents a static method.

Regards, Jakob.
 
C

Cedric Bertolasio

That did the trick.

Thanks a lot.

Cheers


Jakob Christensen said:
Maybe this will work:

Dim newDelegate As MulticastDelegate = MulticastDelegate.CreateDelegate(GetType(MulticastDelegate),
mEventHandler.Method)

But I do think that it will only work, if mEventHandler.Method represents a static method.

Regards, Jakob.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top