Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

C# delegate and event

Posted on 2011-02-18
8
Medium Priority
?
351 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
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 

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 2000 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

Visualize your virtual and backup environments

Create well-organized and polished visualizations of your virtual and backup environments when planning VMware vSphere, Microsoft Hyper-V or Veeam deployments. It helps you to gain better visibility and valuable business insights.

Question has a verified solution.

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

Entity Framework is a powerful tool to help you interact with the DataBase but still doesn't help much when we have a Stored Procedure that returns more than one resultset. The solution takes some of out-of-the-box thinking; read on!
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…
The goal of this video is to provide viewers with basic examples to understand recursion in the C programming language.
Video by: Grant
The goal of this video is to provide viewers with basic examples to understand and use while-loops in the C programming language.
Suggested Courses

782 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