AddressOf and "me" problem

M

mono

Hi,

Target.ParametricMethod(AddressOf Me.MyTest)

doesn't compile, producing the error:
"reference to a non-shared member requires an object reference" whereas

Target.ParametricMethod(AddressOf MyTest)

does compile. In both case that call is made from a non-shared method.
MyTest is not shared. If I make MyTest shared (not workable for me) then
both forms compile.

Target is an instance of a class with these members:

Public Delegate Function KeyTest(ByVal sKey As String) As Boolean

Public Sub ParametricMethod(ByVal oKeyTest As KeyTest)
...
End Sub

and Me is an instance of a class with member:

Public Function MyTest(ByVal oString As String) As Boolean

Isn't Me.MyTest synonymous with MyTest?? Is there something special about
the AddressOf context?

michael
 
M

Marina

No. This is because VB has very poor syntax because it has inherited all the
crap from VB6.

When you say Me.MyTest - you typically want to call that function. And yes,
when calling the function, you can also just say MyTest

However, with AddressOf - that just expects the name of a function.
Me.MyTest is not a name of a function.

The reason this is confusing is, as I said before, because VB syntax is very
awkward and clumsy.
 
M

Mattias Sjögren

Michael,

I can't seem to reproduce the behavior you describe. The following
code compiles correctly with all versions of the VB.NET compiler I
tried. Am I missing something?


Option Strict On

Class Foo
Public Delegate Function KeyTest(ByVal sKey As String) As Boolean
Public Sub ParametricMethod(ByVal oKeyTest As KeyTest)
End Sub
End Class

Class Bar
Public Function MyTest(ByVal oString As String) As Boolean
End Function

Sub Test()
Dim Target As New Foo
Target.ParametricMethod(AddressOf Me.MyTest)
End Sub
End Class




Mattias
 

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