开发者

using the same name for event and method of class

am writing a class that has the same name fo开发者_JAVA技巧r 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


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.


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


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
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜