Home > Computers and Internet, Technology > Dabbling in C#

Dabbling in C#

So I’ve been strictly VB since starting at Lancaster Labs five years ago. Some of that must have worn off because I’m finding C# a little odd. What’s up with the code needed to wire up an event? VB is definitely easier for this.

 

VB:

Public Event SomeEvent(sender as Object, e as EventArgs)

Public Sub DoSomething()

RaiseEvent SomeEvent(Me, EventArgs.Empty)

End Sub

 

C#:

public event SomeEvent(Object sender, EventArgs e);

public void DoSomething()

{

OnSomeEvent();

}

private void OnSomeEvent()

{

if (SomeEvent != null)

SomeEvent(this, EventArgs.Empty);

}

 

Same result in the end, but which is more readable? I think the VB.

Advertisements
  1. Shane
    May 15, 2011 at 3:17 PM

    The only difference I see is that you have to check for null before you raise the event in C#. I guess VB.net does that under the covers?

  2. May 16, 2011 at 9:23 AM

    You also don’t need the separate “On____” method (although I guess that could be replaced by just the null check and event call). The “RaiseEvent” keyword does everything for you. I just think the VB is much more readable. If I didn’t know either language, the VB makes it obvious that an event is being raised.

  3. Aaron
    May 16, 2011 at 12:52 PM

    I agree with the VB is more readable comment.

  1. No trackbacks yet.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: