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

  • Thread starter Thread starter Cedric B
  • Start date Start date
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.
 
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.
 
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...
 
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.
 
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.
 
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.
 
Back
Top