Adding an event handler in VB.NET in programming (if it does not exist)

I am trying to use a single event handler for several controls in an ASP.NET web page. I want to add an event handler at runtime, if and only if it does not exist. In In C#, I would write as follows:

if (myTextBox.OnTextChanged == null)
{
myTextBox.OnTextChanged += DoTextChangingValidation;
}

Similarly, I know that I can delete the event handler as follows:

if (myTextBox.OnTextChanged != null) 
{
myTextBox.OnTextChanged -= DoTextChangingValidation;
}

I know how to add and remove event handlers in Visual Basic… but how to determine how to check whether it has been distribute?

You cannot perform this operation from outside the object in C# or Visual Basic. An event is basically Two visitors: add and delete. If you code manually, it is immediately visible:

public event EventHandler Click
{
add { ... }
remove {... }
}

These become add_Click(EventHandler) and remove_Click(EventHandler) methods. Even if you use the default event implementation,

public event EventHandler Click;

Except for using the default implementation to generate the accessor for you, it is still no different. The default implementation uses the private event handler with the same name as the event. Broadcast delegate fields to store these handlers.

This means two things:

>For customers of this category, the only two things they can do with events are to add or Delete the handler, because only the visitor is exposed. List the currently registered handlers without access rights
>Even if you use the default implementation for the event of the provided field, the field is still private, so except for methods from the same class You can’t access it outside of the internal. If you have code permissions, you can use reflection, but see #1 to understand why it is not a general solution.

This is actually intentional. The reason is : The object may be shared, and other code may have registered its handlers for its events. If you have access to the handler list, you can call them yourself, which may violate the contract and access in a way that the class owner does not want Private method.

If you want something like this, it needs to be done in the class that provides the event-either write your own add and remove to check dupes, or expose private fields through attributes.

I am trying to use a single event handler for several controls in an ASP.NET web page. I want to add an event handler at runtime, if and only if it does not exist. In In C#, I would write as follows:

if (myTextBox.OnTextChanged == null)
{
myTextBox.OnTextChang ed += DoTextChangingValidation;
}

Similarly, I know that I can delete the event handler as follows:

if (myTextBox. OnTextChanged != null)
{
myTextBox.OnTextChanged -= DoTextChangingValidation;
}

I know how to add and remove event handlers in Visual Basic…but how to determine How to check if it has been allocated?

You cannot perform this operation from outside the object in C# or Visual Basic. An event is basically two visitors: add and delete. If you manually Encoding, it is immediately visible:

public event EventHandler Click
{
add {... }
remove {.. . }
}

These become add_Click(EventHandler) and remove_Click(EventHandler) methods. Even if you use the default event implementation,

public event EventHandler Click;

Except for using the default implementation to generate accessors for you, it is still no different. The default implementation uses a private multicast delegate field with the same name as the event to store these handlers.

This means two things:

>For customers of this category, the only two things they can do with events are to add or remove handlers, because only visitors are exposed. Columns The currently registered handler does not have access rights
>Even if you use the default implementation for the event that provides the field, the field is still private, so you cannot access it except from inside a method of the same class. If you have Code permissions, you can use reflection, but please refer to #1 to understand why it is not a general solution.

This is actually intentional. The reason is: the object may be shared, and other code may have Registered its handlers for its events. If you have access to the handler list, you can call them yourself, which may break the contract and access private methods in a way that the class owner does not want.

If you To want something like this, it needs to be done in the class that provides the event-either write your own add and remove to check dupes, or expose private fields through attributes.

Leave a Comment

Your email address will not be published.