Smith
Smith

Reputation: 5961

using the same name for event and method of class

am writing a class that has the same name for events and method, but visual studion won't allow me

The event

public event Close()

the method

Public Sub Close()

End Sub

how can i do this, i don't want to use different names

thanks

Upvotes: 1

Views: 721

Answers (3)

Grant Thomas
Grant Thomas

Reputation: 45083

The Microsoft Framework Design Guidelines specifies something along the lines of the following as best practice (these are from our internal document but derived from the former mentioned guidelines to which I will find a link):

  • Do name events with a verb or a verb phrase.
  • Do give event names a concept of before and after, using the present and past tense (gerunds.)
    • For example, a close event that is raised before a window is closed would be called > Closing and one that is raised after the window is closed would be called Closed.
  • Do not use Before or After prefixes or suffixes to indicate pre and post events.
  • Do name event handlers (delegates used as types of events) with the EventHandler suffix.
  • Do use two parameters named sender and e in event handler signatures.
    • The sender parameter should be of type Object, and the e parameter should be an instance of or inherit from EventArgs.
  • Do name event argument classes with the EventArgs suffix.
  • Do name event handler method with On.
    • For example, a method that handles a Closing event should be named OnClosing.

Update: Relevant Microsoft Design Guidelines Link.

Upvotes: 4

Chris Haas
Chris Haas

Reputation: 55427

If the compiler allowed this then it would probably allow instance variables and delegates to have the same name, too. Imagine this (nonsensical) code:

Public Class Class1
    Public XYZ As String
    Public Event XYZ(ByVal X As XYZ)
    Public Delegate Sub XYZ()
    Public Sub XYZ()
        RaiseEvent XYZ(AddressOf XYZ)
    End Sub
End Class

Upvotes: 1

Dennis Traub
Dennis Traub

Reputation: 51634

AFAIK the .NET compiler doesn't allow this kind of overloading. Try this instead:

public event OnClose()

Public Sub Close()
    ' implementation goes here
End Sub

Upvotes: 1

Related Questions