Jump to content
Xtreme .Net Talk

Recommended Posts

Posted

I have a syntax problem

Say I have 3 object:

Public Class Class1
End Class

Public Class class2
End Class

Public Class class4
 Public Function myFunctionToCall(ByVal A As String, ByVal B As Class1) As String
 End Function
End Class

and two delegates

Public Delegate Function myDelegate1(ByVal A As String, ByVal B As Class1) As String
Public Delegate Function myDelegate2(ByVal A As String, ByVal B As class2) As String

so class4.myFunctionToCall signature fit the signature of myDelegate1 (and not of myDelegate2 because of the second parameter)

 

Now say I want to use those delegates in an another class:

Public Class class3

 Private m_Class4 As class4

 Public Sub New()
   m_Class4 = New class4()
 End Sub

 Public Overloads Function DelegateCall(ByVal myDelegate As myDelegate1, ByVal B As Class1) As String
   Return myDelegate.Invoke("AA", B)
 End Function

 Public Overloads Function DelegateCall(ByVal myDelegate As myDelegate2, ByVal B As class2) As String
   Return myDelegate.Invoke("AA", B)
 End Function

 Public Sub myTest()
   DelegateCall(AddressOf Me.m_Class4.myFunctionToCall, New Class1())
 End Sub

End Class

So I have in that class two overloaded functions which will call either the delegate1, or the delegate 2

However, this doesn't compile with the following error :

"Reference to a non-shared member requires an object reference"

on the addressof in myTest method

 

If I comment my second overloaded DelegateCall function, it compile.

 

But I can't see the error ! The two overloaded functions have perfectly different signature and so there is nothing ambigous here.

Any idea ?

Jarod
Posted

Ok you're right, it works like this

 

However I wonder something.

I agree that in IL all the delegates are "converted' into classes, and so it'is normal to pass new instance of those "classes" as parameter to a function.

But I have seen often (even in books) using directly addressof instead of writing new myDelegate(addressof...)

And it works most of times.

 

So I would like to know in which case we need to use one or the other syntax ? Is there one which is more performant ?

Jarod
  • *Gurus*
Posted
Where have you seen just using AddressOf on its own? The only instance I can think of is the VB AddHandler and RemoveHandler methods which perform some magic to save you the trouble of instantiating the correct event handler like C# devs have to.

MVP, Visual Developer - .NET

 

Now you see why evil will always triumph - because good is dumb.

 

My free .NET Windows Forms Controls and Articles

Posted

Just take my example and remove the second DelegateCall and it works perfectly just with the addressof !

And for a book example, "Professional VB.NET 2nd edition" at WROX uses such an example

(be sure that I also saw other books giving the "correct" syntax with New MyDelegate(..)

Jarod

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...