Tom
Tom

Reputation: 577

C# how to subscribe to an event that uses a delegate?

I am writing an application that uses a class from a third party. This class has the following event defined:

 public event SamplesAvailableDelegate<ByteSamplesEventArgs> FFTAvailable;

and this event is raised in in this class. I want to subscribe to this event in my Windows Forms application.

I have tried the following:

public partial class Form1 : Form
{
    Client _client;

    public Form1()
    {
        InitializeComponent();
        _client = new Client();
        _client.FFTAvailable += _fftAvailable(object sender, ByteSamplesEventArgs e);
    }

    private void _fftAvailable(object sender, EventArgs e)
    {
        //do something here
    }
}

When I do this I get an error " cannot implicitly convert type void... Can someone tell me the correct syntax to handle the event please? Thanks

Upvotes: 0

Views: 111

Answers (2)

Tom
Tom

Reputation: 577

Ok, the solution was simple. I needed the following:

_client.FFTAvailable += _FTTAvailable;

and then

private void _FFTAvailable(object sender, ByteSamplesEventArgs e)
{

}

This works. Thanks for the help. I got thrown off declaring the event handler.

Upvotes: 2

Prasanjeet Debnath
Prasanjeet Debnath

Reputation: 272

You need to share the signature of the delegate SamplesAvailableDelegate<ByteSamplesEventArgs>.

_fftAvailable is expecting to return some datatype but it is returning void.

Upvotes: 0

Related Questions