Solved

C# delegate and event

Posted on 2011-02-18
8
341 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
DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

 
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Many of us here at EE write code. Many of us write exceptional code; just as many of us write exception-prone code. As we all should know, exceptions are a mechanism for handling errors which are typically out of our control. From database errors, t…
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 pointers in the C programming language.
The goal of this video is to provide viewers with basic examples to understand how to use strings and some functions related to them in the C programming language.

919 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

23 Experts available now in Live!

Get 1:1 Help Now