Is this right: Private method accessible to outside?

C

Charles Law

Sorry, but I am on a bit of a roll today.

I have just found that I can make a method private to my class, but then use
AddHandler in an external class to hook it up. Is that what people would
expect?

For example

<code>
Public Interface IMyClass
Sub MyMethod(ByVal sender As Object, e As EventArgs)
End Interface

Public Class MyClass

Implements IMyClass

' NOTE: this is declared Private
Private Sub MyMethod(ByVal sender As Object, e As EventArgs) Implements
IMyClass.MyMethod
' Do stuff
End Sub
End Class

Public Class MainClass

Public Event MyEvent(ByVal sender As Object, e As EventArgs)

Public Sub SetHandler()

Dim imc as IMyClass

imc = New MyClass

'*** This works. Should it? ***
AddHandler MyEvent, AddressOf imc.MyMethod

End Sub

End Class
</code>

Charles
 
C

Charles Law

Yes, but ...

Although the interface is not private, it seems incongruous that I should be
able to declare the method as private but still call it, albeit from an
event. Perhaps the compiler should insist that it be declared public before
it can be accessed publicly?

Charles
 
P

Patrick Steele [MVP]

Although the interface is not private, it seems incongruous that I should be
able to declare the method as private but still call it

But I can't see any logical reason to define the scope of a class method
that implements an interface method as private. That goes against the
whole concept of an interface.
 
C

Charles Law

Hi Patrick

I'm with you on that one. And until today I had declared them all as public
and was happy. Then I ran FxCop, and it got upset at them being public and
suggested I make them private. So I did, not expecting it to work. Well, the
rest you know.

Is this just a little vagary of the compiler?

Charles
 
P

Patrick Steele [MVP]

Hi Patrick

I'm with you on that one. And until today I had declared them all as public
and was happy. Then I ran FxCop, and it got upset at them being public and
suggested I make them private. So I did, not expecting it to work. Well, the
rest you know.

My guess is it works because the .NET runtime is the one that will
handle the calling of all of the registered delegates (event handlers)
-- not your actual class. And the runtime has access to everything! :)
 
S

SA

Try not to declare the object as a type of the interface, but a type of the
actual class. That should fix your concern. The compiler will then correctly
recognize that doing this is not possible.

Of course, it doesn't fix the oddity with the runtime. Apparently, it
assumes because MyClass implements IMyClass, it should be allright to call
the private method.

In a way, it may be a fault-tolerance mechanism: because an variable
declared from an interface can call any method in that interface, at
runtime, errors could occur if the implementation was of such a method was
private.

Interesting point though.

Also, in my tests (http://www.adduxis.com/play/module1.txt), it works even
when not using it as a handler, but simply calling the method!
 
S

SA

Charles:

Also remember that an interface is a contract, the class implementing the
method has specifically agreed to implementing all methods. Apparently, the
sentence for implementing it private is intrusion in the class' privacy (to
put it in legal terms...)
 

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