Link to home
Start Free TrialLog in
Avatar of jtran007
jtran007

asked on

C# delegate and event

Hi,

I am not sure what this code means:

   public delegate void FirmwareDownloadProgressDelegate(object sender, int value);
   private event FirmwareDownloadProgressDelegate m_FirmwareDownloadProgress;

  public event FirmwareDownloadProgressDelegate FirmwareDownloadProgress
        {
            add    { m_FirmwareDownloadProgress += new FirmwareDownloadProgressDelegate(value); }
            remove { m_FirmwareDownloadProgress -= new FirmwareDownloadProgressDelegate(value); }
        }
These functions are defined in a class which I will instanstiate . How can I use the value?
I am new to C# ,please help
JT
Avatar of dimaj
dimaj
Flag of United States of America image

That code is basically defining an event that could be fired off by your class. Then, "clients" those who use this class, will be able to subscribe to your event and do some actions based on event's data.

You can get more info on this here: http://msdn.microsoft.com/en-us/library/aa645739(v=vs.71).aspx

That page has a good example on how to create and subscribe to an event.

Hope this helps.
Avatar of jtran007
jtran007

ASKER

Hi ,

Suppose I am a client of this class, how do I subscribe to use this class event?
Could you please give me an example?
Thanks,
JT
sure...


Let's say that you have a class MyClass that has your delegate and an event defined.
In your class MyClient you'll say something like this:

MyClass.MyEventName += <name of a function that will handle your event>;

A hint, when you subscribe to an event, after you press +=, hit Tab key couple of times. Visual Studio will automatically create an event handler function for you based on the event you are subscribing to.
Hi,

So I can use like:

      void Adt42To45_FirmwareDownloadProgress(object sender, int value)
        {
             progressBar.Value = value;
        }

Is it correct since I could not the class now for the time being.

Also since my progressbar is in other form such as Form2, how can I access the progress bar
in this Form2. Could you please help since I have  due date on Monday?

Thanks,
JT
Is this the same question as you've asked on the Background Thread?
Hi dimai,

Yes. it is correct.

JT
ASKER CERTIFIED SOLUTION
Avatar of dimaj
dimaj
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Thanks,
JT