Solved

C# delegate and event

Posted on 2011-02-18
8
345 Views
Last Modified: 2013-12-17
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
0
Comment
Question by:jtran007
  • 4
  • 4
8 Comments
 
LVL 7

Expert Comment

by:dimaj
ID: 34929521
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.
0
 

Author Comment

by:jtran007
ID: 34929742
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
0
 
LVL 7

Expert Comment

by:dimaj
ID: 34929777
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.
0
Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

 

Author Comment

by:jtran007
ID: 34930156
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
0
 
LVL 7

Expert Comment

by:dimaj
ID: 34934163
Is this the same question as you've asked on the Background Thread?
0
 

Author Comment

by:jtran007
ID: 34935633
Hi dimai,

Yes. it is correct.

JT
0
 
LVL 7

Accepted Solution

by:
dimaj earned 500 total points
ID: 34935691
Ok, then I have posted done code there for you
0
 

Author Closing Comment

by:jtran007
ID: 34942982
Thanks,
JT
0

Featured Post

Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

This article shows how to deploy dynamic backgrounds to computers depending on the aspect ratio of display
Examines three attack vectors, specifically, the different types of malware used in malicious attacks, web application attacks, and finally, network based attacks.  Concludes by examining the means of securing and protecting critical systems and inf…
Video by: Grant
The goal of this video is to provide viewers with basic examples to understand and use for-loops in the C programming language.
The goal of this video is to provide viewers with basic examples to understand how to create, access, and change arrays in the C programming language.

809 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question