Can I have an Event & Method with the same name?

  • Thread starter Thread starter Terry Olsen
  • Start date Start date
T

Terry Olsen

Can I have an Event & Method with the same name in the same class? I want
to have a "Close" method to close a socket and a "Close" event to be raised
when the socket is closed.
 
Terry Olsen said:
Can I have an Event & Method with the same name in the same class? I want
to have a "Close" method to close a socket and a "Close" event to be raised
when the socket is closed.

Even if you could, it's not a good idea. Why chance the unneccessary
confusion when you can use a similar meaningful name instead - SocketClosed,
CloseSocket, etc?
 
Why not call them something like


MyMethodName


MyMethodNameEvent


--
OHM ( Terry Burns ) * Use the following to email me *

Dim ch() As Char = "ufssz/cvsotAhsfbuTpmvujpotXjui/OFU".ToCharArray()
For i As Int32 = 0 To ch.Length - 1
ch(i) = Convert.ToChar(Convert.ToInt16(ch(i)) - 1)
Next
Process.Start("mailto:" & New String(ch))
 
The reason I asked is because I am trying to write a "WinSock" class with
the same properties, methods, and events as the VB6 WinSock Control, which
has the same name for the Close Event & Method.
 
The answer is no. You cannothave the same identifier for two different
types. With functions and subs though you can overload the identifier as
long as the signatyures are different.



--
OHM ( Terry Burns ) * Use the following to email me *

Dim ch() As Char = "ufssz/cvsotAhsfbuTpmvujpotXjui/OFU".ToCharArray()
For i As Int32 = 0 To ch.Length - 1
ch(i) = Convert.ToChar(Convert.ToInt16(ch(i)) - 1)
Next
Process.Start("mailto:" & New String(ch))
 
Terry,
Can I have an Event & Method with the same name in the same class?
No as they are both at the same scope.
I want to have a "Close" method to close a socket and a "Close" event to
be raised when the socket is closed.
I would call the event Closed, while calling the method Close. (which btw is
what you stated :-))

If I needed an event that occurred when the connection was about to close I
would call it Closing.

Which is what the "Design Guidelines for Class Library Developers"
recommends:

http://msdn.microsoft.com/library/d.../cpgenref/html/cpconEventNamingGuidelines.asp

http://msdn.microsoft.com/library/d...s/cpgenref/html/cpconEventUsageGuidelines.asp

Hope this helps
Jay
 

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

Back
Top