Solved

C# delegate and event

Posted on 2011-02-18
8
340 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
 

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
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 
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

6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

Join & Write a Comment

In my previous two articles we discussed Binary Serialization (http://www.experts-exchange.com/A_4362.html) and XML Serialization (http://www.experts-exchange.com/A_4425.html). In this article we will try to know more about SOAP (Simple Object Acces…
Wouldn’t it be nice if you could test whether an element is contained in an array by using a Contains method just like the one available on List objects? Wouldn’t it be good if you could write code like this? (CODE) In .NET 3.5, this is possible…
The goal of this video is to provide viewers with basic examples to understand and use structures in the C programming language.
The goal of this video is to provide viewers with basic examples to understand recursion in the C programming language.

758 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

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now