answersLogoWhite

0

Different approaches of Event handling in vbNET?

Updated: 8/18/2019
User Avatar

Wiki User

7y ago

Best Answer

It is a bit sad that the compiler doesn't generate an error for this. You have to make it look like this:

Private Sub Form1_Load(ByVal sender As System.Object, _ ByVal e As System.EventArgs) Handles MyBase.Load

obj1 = New Class1

AddHandler Class2.TestEvent, AddressOf handleEventFromClass2

End Sub

Private Sub handleEventFromClass2(ByRef sender As Class2, _

ByVal op As Integer)

TextBox1.Text = "Wert : " & op

End Sub

Note that missing Handles keyword on the event handler and the explicit use of the AddHandler keyword. You are however going to get yourself into trouble with these Shared events. There is no mechanism that automatically unsubscribes the event, Shared makes it global so it lives for the duration of your program. Even after the user has closed the form. That's going to go bad, an ObjectDisposedException is likely be raised when you fire the event since the form object is dead. Furthermore, you have a permanent leak since the form object can't be garbage collected.

You have to explicitly unsubscribe the event:

Private Sub Form1_FormClosed(ByVal sender As Object, _

ByVal e As FormClosedEventArgs) Handles Me.FormClosed

RemoveHandler Class2.TestEvent, AddressOf handleEventFromClass2

End Sub

Event sources that outlive their listeners are troublesome.

User Avatar

Wiki User

7y ago
This answer is:
User Avatar

Add your answer:

Earn +20 pts
Q: Different approaches of Event handling in vbNET?
Write your answer...
Submit
Still have questions?
magnify glass
imp
Related questions

What is the difference between C and Vbnet?

It is just a different coding language, but C is more for business that VBNET


Which is better C and vbnet?

They are different tools for different jobs. Which is better a hammer or a screwdriver?


What are the basic difference between c sharp and vbnet?

They use different syntax.


Is java better than vbnet?

Java is not better than VBNet, nor is VBNet better than Java. Eachone has its advantages and disadvantaged over the other one.


What is datareader in VBNET?

x


What is instance in vbnet?

An instance in VBNet is the same as an instance in any other language; it is the realisation of a type. In object-oriented languages, like VBNet, we say that an object is an instance of a class, where the class defines the object's type.


What is an instance in vbnet?

An instance in VBNet is the same as an instance in any other language; it is the realisation of a type. In object-oriented languages, like VBNet, we say that an object is an instance of a class, where the class defines the object's type.


IS VBNet purely objet oriented?

no


Who is the father of vbnet?

Alan Cooper


Who is the developer of vbnet?

Microsoft Microsoft.com/VisualStudio


Is the Class type in VBNET a value type?

NO.


How do you write a VBnet program to find the prime numbers between 100 to 200?

VBnet program to find the prime numbers between 100 to 200?