raising .net events from another class?
Solution 1
In C# the "event" keyword generates a private delegate member object. So it can be accessed only for the class where is defined. But you can expose a public method that will fire the event, and this method can be accessed from outside. I think that in VB is the same.
Solution 2
I'd be creating a Friend method that raises the event. Call that method instead or raising the event externally.
Class LoginClass
Public Shared Event UserLoggedIn As EventHandler
Friend Shared Sub OnUserLoggedIn(e As EventArgs)
RaiseEvent UserLoggedIn(Me, e)
End Sub
End Class
TWood
Industrial Controls developer for a Materials Handling manufacturer and integrator. I use a little bit of all the MS technologies to develop solutions for our clients. In my spare time I work on cars. If you downvote my questions would you mind leaving a comment to let me know what you thought was lacking from my question? I always try to provide as much issue context and source code to help others understand what i'm going through and your comments will help me get better at this. Thanks and happy coding! Try not to be a Stack Exchange elitist. Belittlement is not considered helpful. Everyone's here to learn after all.
Updated on October 27, 2022Comments
-
TWood over 1 year
can I call shared events contained in one class from another class?
for example:
logonclass.vb : handles login logic and authenticates against database
logonmanager.vb: hold current user reference and some logon and timeout events
Logon.vb: A form with a submit button.
I would like to do something like this but I can't get the compiler to agree with it
If VerifyEntries() Then Try privLvl = LoginClass.AttemptLogin(txtUserName.Text, txtPassword.Text) Catch End Try If privLvl > 0 Then 'RaiseEvent LoginClass.UserLoggedIn() 'RaiseEvent LoginManager.UIdisplaychange(privLvl) Me.Close() End If End If
If this isn't the proper way to wire things together let me know so I can learn to structure differently. In my planning it seemed like I was raising a lot of extra duplicate events from my logonclass to my loginmanager class. Then the loginmanager had to raise it again for the main form to see the change event. It got me thinking that if I publicly shared the events and could raise them from wherever then it would cut down on the amount of events I needed in code and the amount of them flying around at runtime.
-
TWood over 12 yearsOk. I see now. I'll have to expose the event if I want others to see it.
-
Hand-E-Food over 12 yearsOkay, coffee hasn't kicked in yet. My answer's exactly the same as yours. :-)