Need help with C# events

S

shout

I am creating a game logon client and have an event that happens when
a packet is recived. It seems like I tried to make my cat write the
code. Could someone tell me what is wrong?
/// <summary>
/// What ever you want it to be.
/// </summary>
/// <param name="e">...</param>
protected virtual void OnRecivePacket(BnetPacketArgs e)
{
if (BnetSocket.Available != 0)
{
RecivePacket(this, e);
}
}

/// <summary>
/// Delegate
/// </summary>
public delegate void BnetPacketHandler(object sender,
BnetPacketArgs e);

/// <summary>
/// Event
/// </summary>
public event BnetPacketHandler RecivePacket;

/// <summary>
/// Sends the packet to packet storage.
/// </summary>
private void bnetSocket_RecivePacket(object sender,
BnetPacketArgs e)
{
PS.AddPacket(e.inBuffer);
}


And I also have this in the class constructor.

[code:1:b4efebec0a]this.RecivePacket += new
BnetPacketHandler(bnetSocket_RecivePacket);[/code:1:b4efebec0a]

Could someone tell me what I'm doing wrong?
 
C

Christian Heide Damm

One thing that's wrong is that you don't check if there are actual listeners
to the event.

Instead of

ReceivePacket(this,e);

you should write

if (ReceivePacket != null)
{
ReceivePacket(this,e);
}

Christian
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top