473,394 Members | 1,721 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,394 software developers and data experts.

Is this right: Private method accessible to outside?

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
Nov 20 '05 #1
7 1107
The point is that the interface is not private.

"Charles Law" wrote:
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

Nov 20 '05 #2
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
"Rulin Hong" <Ru*******@discussions.microsoft.com> wrote in message
news:64**********************************@microsof t.com...
The point is that the interface is not private.

"Charles Law" wrote:
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

Nov 20 '05 #3
In article <OP**************@tk2msftngp13.phx.gbl>, bl***@nowhere.com
says...
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.

--
Patrick Steele
Microsoft .NET MVP
http://weblogs.asp.net/psteele
Nov 20 '05 #4
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
"Patrick Steele [MVP]" <pa*****@mvps.org> wrote in message
news:MP************************@msnews.microsoft.c om...
In article <OP**************@tk2msftngp13.phx.gbl>, bl***@nowhere.com
says...
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.

--
Patrick Steele
Microsoft .NET MVP
http://weblogs.asp.net/psteele

Nov 20 '05 #5
In article <uu**************@tk2msftngp13.phx.gbl>, bl***@nowhere.com
says...
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! :)

--
Patrick Steele
Microsoft .NET MVP
http://weblogs.asp.net/psteele
Nov 20 '05 #6
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!

--

Sven
http://www.adduxis.com
"Charles Law" <bl***@nowhere.com> wrote in message
news:u1**************@TK2MSFTNGP10.phx.gbl...
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

Nov 20 '05 #7
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...)

--

Sven
http://www.adduxis.com

"SA" <in*********@freemail.nl> wrote in message
news:es****************@TK2MSFTNGP10.phx.gbl...
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!

--

Sven
http://www.adduxis.com
"Charles Law" <bl***@nowhere.com> wrote in message
news:u1**************@TK2MSFTNGP10.phx.gbl...
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


Nov 20 '05 #8

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: IHateSuperman | last post by:
public class StaticField2{ public static void main(String args){ private int x, y; // <<== error 1 for ( y = 0 ; y < 100 ; y++){ x = StaticMethod(); System.out.println(" x = "+x); } } public...
27
by: gabor | last post by:
hi, as far as i know in python there aren't any private (i mean not accessible from the outside of the object) methods/fields. why? in java/c++ i can make a method private, this way...
20
by: dukeleto | last post by:
I know this is an annoying thing on some sites. I have set some images in an online gallery to have their own java po up window that is set to be the same size as the image. I would like to...
11
by: prefersgolfing | last post by:
I'm trying to find on MSDN, or someplace, that speaks to variables being public or private by default. Anyone know where? Thanks.
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.