Gil Matok
Gil Matok

Reputation: 21

C# and VB.NET raise event differences

I have a question.. in VB.NET I'd have this sub, for example:

Sub AcceptClient(ByRef pSocket As Socket)
'Some Code...
End Sub

And then, I'd have this, to call it whenever the event is raised:

AddHandler mAcceptor.OnAccepted, AddressOf AcceptClient

However, in C#, when I do this, using the following code:

mAcceptor.OnAccepted += acceptClient();
void acceptClient(Socket pSocket)

It says I'm missing the pSocket argument. But, in VB.NET, It works flawlessly. Why's that? I tried adding "ref", that didn't do the trick. What else can I do to solve this?.. Thanks.

Upvotes: 2

Views: 97

Answers (1)

Joey
Joey

Reputation: 354356

acceptClient()

would call a method acceptClient with no arguments and return a result. What you want is to add the method as an event handler:

mAcceptor.OnAccepted += acceptClient;

Note the lack of parentheses; you're using the method instead of calling it.

Upvotes: 6

Related Questions