Resolved Shared Methods

aaaron

Well-known member
Joined
Jan 23, 2011
Messages
216
Programming Experience
10+
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:

'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.
What bothers me is the statement:
In the provided code, the RemoveClickEvent method is a static method because it operates on the class level.
If that statement is correct then I'm not understanding "Static method"
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:
Solution
You are correct. The use of the word "static" is a little confusing here. There is a Static keyword in VB but it has nothing to do with this. The static keyword in C# has basically the same meaning as the Shared keyword in VB. It does appear that the whoever wrote that code has forgotten to apply the Shared keyword to that method as they describe.
You are correct. The use of the word "static" is a little confusing here. There is a Static keyword in VB but it has nothing to do with this. The static keyword in C# has basically the same meaning as the Shared keyword in VB. It does appear that the whoever wrote that code has forgotten to apply the Shared keyword to that method as they describe.
 
Solution
Thanks, I needed that to be sure of myself.

There is the word Static in BindingFlags.Static which I assume does what BindingFlags.Shared would be expected do if that existed
 
Last edited:
Keep in mind that pretty much all the framework is written in C#, so there are some things that will be C#-centric where there is some difference to VB. Shared members in VB and static members in C# are the same thing so, as you surmised, that Enum field will indeed search for members declared Shared in VB.

As an aside, there are static classes in C#, where the class itself is declared static and every member must also be declared static. Such classes cannot be instantiated. In VB, we have modules that do the same thing. Modules cannot be instantiated as classes can and, while module members are not declared Shared, it is implied because they behave like Shared members of classes. Once compiled, a VB module looks exactly the same as a C# static class.
 
Back
Top