Addy75
Addy75

Reputation: 167

Event conversion from vb.net to c#

Any guidance will be appreciated: I am converting I vb.net project I have finished to C#. I don't know how to approach the event handling in C#. I have a security alert event set up in my UserAccount class:

Public Class UserAccount

Private m_UserAccountID As Guid
Private m_Username As String
Private m_Password As String
Private m_Email As String

Public Event SecurityAlert(ByVal username As String, ByVal password As String)

which then I raise in an Authentication method:

Public Function Authenticate(ByVal U As String, ByVal P As String) As Boolean

    RaiseEvent SecurityAlert(U, P)

    If m_Username = U And m_Password = P Then
        Return True
    End If
    Return False

End Function

which then I handle in my Login Form:

Private Sub objUserAccount_SecurityAlert(ByVal username As String, ByVal password As String)     Handles objUserAccount.SecurityAlert
    If username = "sam" And password = "333" Then
        MessageBox.Show("This is a security breach! This employee has been fired!")
        End
    End If

End Sub

I have the same Class and form in my C# project but I'm confused as to where and how to set up my delagate and dealing with these parameters and not the object and EventArgs. Like I said, any guidance will be appreciated. This was a class project from the summer that I'm converting on my own because I want to advance my knowledge.

Ok so like I said have done this so far:

namespace BusinessObjects
{
public delegate void SecurityAlert(string username, string password);

public class UserAccount
{
    private Guid m_UserAccountID;
    private String m_Username; 
    private String m_Password;
    private String m_Email;

    public event SecurityAlert Alert;

then in my class Authentication method I tried to raise it:

Alert += new SecurityAlert(U,P);

I seen from Jeffery's post that that was incorrect and changed it to:

Alert += new SecurityAlert(Alert); 

which makes sense because it looks like that I am now referencing the Alert method that handles the event which I have set up in my Log In form:

private void Alert(String username, String password){
        if(username == "sam" && password == "333"){
            MessageBox.Show("This is a security breach! This employee has been fired!");
        }
    }

But my UserAccount and Login Form are two differnt classes so how can they reference eachother? I get an error:

Alert += new SecurityAlert(Alert); Delegate to an instance method cannot have null 'this'.

Upvotes: 0

Views: 775

Answers (2)

Jeffrey Wieder
Jeffrey Wieder

Reputation: 2376

You can find some very useful information here

You delegate would look like this:

public delegate void SecurityAlert(string username, string password);

This is how you would include it in your class for use:

public class UserAccount
{
    public event SecurityAlert OnSecurityAlert;
}

Here is information on raising your custom event.

Here is some additional information on events in c#

Register the event:

UserAccount userAccount = new UserAccount();
userAccount.OnSecurityAlert += new SecurityAlert(OnSecurityAlert);

Event handler Method:

private void OnSecurityAlert(string username, string password)
{

}

Upvotes: 0

Darryl
Darryl

Reputation: 6217

There are a couple of differences in the way Visual Basic and C# deal with events. First, Visual Basic allows you to specify the event handler's signature on the same line as where you declare the event; C# makes you split it into two lines (define a delegate type, then declare the event). Second, C# doesn't have an equivalent to Visual Basic's Handles clause. Instead, you have to register and de-register events manually, a lot like you would do with Visual Basic's AddHandler and RemoveHandler.

Here's an example of a class that defines an event, and raises it inside a method:

class UserAccount
{
    public delegate void SecurityAlertHandler(String username, String password);
    public event SecurityAlertHandler SecurityAlert;

    public void RaiseSecurityAlert(String u, String p)
    {
        SecurityAlert(u, p);
    }
}

Here's an example of a class that registers an event handler and handles the event:

class AlertHandler
{
    public void RegisterHandler(UserAccount toMonitor)
    {
        toMonitor.SecurityAlert += HandleSecurityAlert;
    }

    private void HandleSecurityAlert(String username, String password)
    {
        Console.WriteLine("Got event: " + username + " => " + password);
    }
}

And here's some code that you can step through in a debugger to watch it do its thing:

    static void Main(string[] args)
    {
        UserAccount ua = new UserAccount();
        AlertHandler ah = new AlertHandler();
        ah.RegisterHandler(ua);
        ua.RaiseSecurityAlert("User", "pwd");
    }

Since Visual Basic's Handles clause registers the event handlers automatically, you might be wondering where to put the event registration (toMonitor.SecurityAlert += HandleSecurityAlert in our example). In your case, that would probably be in your login form's OnLoad method. You'll also want to de-register the event using -=. You'd put that in the same place you set objUserAccount to Nothing in your Visual Basic code.

Upvotes: 1

Related Questions