VB.NET:
Imports System.Reflection
Imports System.ComponentModel
Partial Public Class Form1
Inherits Form
Public Sub New()
InitializeComponent()
AddHandler button1.Click, AddressOf button1_Click
AddHandler button1.Click, AddressOf button1_Click2
AddHandler button2.Click, AddressOf button2_Click
End Sub
Private Sub button1_Click(sender As Object, e As EventArgs)
MessageBox.Show("Hello")
End Sub
Private Sub button1_Click2(sender As Object, e As EventArgs)
MessageBox.Show("World")
End Sub
Private Sub button2_Click(sender As Object, e As EventArgs)
RemoveClickEvent(button1)
End Sub
Private Sub RemoveClickEvent(b As Button)
Dim f1 As FieldInfo = GetType(Control).GetField("EventClick", BindingFlags.Static Or BindingFlags.NonPublic)
Dim obj As Object = f1.GetValue(b)
Dim pi As PropertyInfo = b.GetType().GetProperty("Events", BindingFlags.NonPublic Or BindingFlags.Instance)
Dim list As EventHandlerList = DirectCast(pi.GetValue(b, Nothing), EventHandlerList)
list.RemoveHandler(obj, list(obj))
End Sub
End Class
I found this code on the internet. I wasn't interesed in the code but rather the comment included:
What bothers me is the statement:'In the code snippet I above there is both instance And static methods for event handlers.
'Instance Event Handlers :
'When you use an instance method as an event handler, it is associated with a specific object instance.
'Each object of a class that wants to receive an event notification must register individually.
'Most Event handlers() are instance methods because they allow customization based on the specific instance.
'For example, if you have multiple buttons in a form, each button can have its own unique click event handler.
'Instance methods are called on a specific Object, and they can access instance-specific data.
'Static Event Handlers(also called Shared) are called directly using the class name :
'When a static method is used as an event handler, it applies to the entire class, not just a specific instance.
'Static Event handlers() are shared across all instances Of the Class.
'You don't need to create an instance of the class to handle the event; the method is associated with the class itself.
'Use Static Event handlers() when the logic doesn't depend on instance-specific data.
'For example, if you have a global event (e.g., application startup), a static event handler can handle it.
'Static methods are called on the Class type itself, without requiring an instance.
'In the provided code, the RemoveClickEvent method is a static method because it operates on the class level.
'It removes the click event handler from the specified button, regardless of the button's instance.
'The static method is used here because it doesn’t rely on any specific instance data.
'On the other hand, the button1_Click And button1_Click2 methods are instance methods because they handle the Click event for a
'specific button instance (button1). These methods can access instance-specific properties Or perform actions related to that specific button.
If that statement is correct then I'm not understanding "Static method"In the provided code, the RemoveClickEvent method is a static method because it operates on the class level.
1) So I'm asking: Is that statement correct?
2) If not, if I added the word Shared to the method would that make the statement correct?
Last edited: