RaiseEvent From a Class

Spilled

Member
Joined
Nov 28, 2008
Messages
17
Location
USA
Programming Experience
Beginner
This being my first post here I would just like to say Hello to all :D The problem that I am having is I have a class (clsBuffer) and my Form(Form1.vb). What I am trying to do is Raise an Event from my clsBuffer to my Form. Can anyone help me with this?
 
In it's simplest form:
VB.NET:
Public Class clsBuffer

    Public Event SomeEvent As EventHandler

    Protected Overridable Sub OnSomeEvent(ByVal e As EventArgs)
        RaiseEvent SomeEvent(Me, e)
    End Sub

End Class
You can then raise that event in your clsBuffer class like this:
VB.NET:
Me.OnSomeEvent(EventArgs.Empty)
You can now handle that event anywhere you like, exactly as you do any other event, because it is exactly like any other event.

One off-topic piece of advice: don't use names like "clsBuffer". Are you trying to indicate that it's a class by using "cls"? What for? Your form is a class but you didn't call it "clsForm1". Does the Framework contain classes named "clsString", "clsForm", "clsButton", etc. or are they named "String", "Form", "Button"? If the Framework contains thousands of classes, none of which have names that begin with "cls", why is it that you suddenly need to be reminded that your classes are classes? I know that you probably got this idea from someone else but it's a bad idea.
 
Back
Top