?
Solved

Question on using delegate

Posted on 2003-02-19
9
Medium Priority
?
347 Views
Last Modified: 2010-04-15
I am trying to use a delegate in an UI application where my main thread will be responsible for showing the progress bar of the upload being carried you in the background by the worker thread. I wanted to know if I can pass a delegate from one class to another as an argument in the constructor. I am trying to pass it as an argument in the constructor of another class and as expected it gives me an error that delegate is a keyword an identifier is expected. Can anyone please help me with this.

public delegate bool ProgressDelegate(string fileName, long size);

public Class1()
{
    ProgressDelegate progressDel = new ProgressDelegate(UploadProgress);
    Class2 class2 = new Class2(arg1, arg2, progressDel);
    ...
}

public static bool ProgressDelegate(string, long)
{
   ...
   ...
}




public class Class2
{
   public Class2(arg1, arg2, delegate progressDel)
   {
       ...
       ...
   }
   ...
   ...
}

If this is not the right way to do then how can I pass a delegate from one class to another. The reason I have to pass this delegate is, I am performing some activity (i.e. uploading) in Class2 and after I am uploading each part, I want to inform the Class1 about my progress so that it could be displayed in the progress bar.

Thanks..

Siddhartha Mehta
0
Comment
Question by:mehtas
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 4
9 Comments
 
LVL 4

Expert Comment

by:bgungor
ID: 7983529
Define the delegate and an event of your delegate type in Class2.

In Class1, before you use Class2, subscribe to the event and provide a message handler.

In class 2: (setting up handler definition (delegate) and event)

public delegate MyMessageHandler(object sender, string message);
public event MyMessageHandler MyMessage();

In Class 1:
private MyHandler(object sender, string message){
 // Handler function
  MessageBox.Show(message);
}

private UsingClass2()
{
   Class2 myClass2 = new Class2();
   myClass2 += new Class2.MyMessageHandler(MyHandler);
}

Then to notify class1 from Class2:

In Class 2:
public void DoingSomething()
{
   MyMessage(this,"Hey, something happened");
}

Hope this helps,

Bg
0
 
LVL 2

Author Comment

by:mehtas
ID: 7984296
I couldn't really implement the changes that you suggested. I'd really appreciate if you can write your changes in terms of the code I have written in my previous comment. I know it could be very trivial thing but I am not able to do that. I would really appreciate that.

Thanks..

Siddhartha
0
 
LVL 2

Author Comment

by:mehtas
ID: 7984314
I couldn't really implement the changes that you suggested. I'd really appreciate if you can write your changes in terms of the code I have written in my previous comment. I know it could be very trivial thing but I am not able to do that. I would really appreciate that.

Thanks..

Siddhartha
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 2

Author Comment

by:mehtas
ID: 7984315
I couldn't really implement the changes that you suggested. I'd really appreciate if you can write your changes in terms of the code I have written in my previous comment. I know it could be very trivial thing but I am not able to do that. I would really appreciate that.

Thanks..

Siddhartha
0
 
LVL 14

Accepted Solution

by:
AvonWyss earned 75 total points
ID: 7986074
This is an edited version of your code which should do the trick. Note that you were already pretty close yourself.

public delegate bool ProgressDelegate(string fileName, long size);

public class Class1
{
     public Class1()
     {
          ProgressDelegate progressDel = new ProgressDelegate(UploadProgress);
          Class2 class2 = new Class2(arg1, arg2, progressDel);
          ...
     }

     public static bool ProgressDelegate(string, long)
     {
          ...
          ...
     }
}

public class Class2
{
     ProgressDelegate progressDel;

     public Class2(arg1, arg2, ProgressDelegate progressDel)
     {
          this.progressDel = progressDel;
          ...
          ...
     }

     public void SomethingThatUsesDelegate()
     {
          bool result = progressDel("filename", 123456);
     }
}
0
 
LVL 14

Expert Comment

by:AvonWyss
ID: 7986078
Oops. Missed one fix...:

In Class1,

     public static bool ProgressDelegate(string, long)

should read

     public static bool UploadProgress(string, long)
0
 
LVL 2

Author Comment

by:mehtas
ID: 7988864
I tried with what you said but the thing is I have Class1 and Class2 in 2 different packages. Also the package for Class1 is using the dll from Class2. I get an error message when I try to build package for Class2 that the ProgressDel namespace could not be found.

Do I need to again declare a delegate again in Class2 like below.

public delegate bool ProgressDelegate(string, long)
0
 
LVL 14

Expert Comment

by:AvonWyss
ID: 7998059
No. You have to reference the assembly (I guess that's what you are calling a package), and then you can use the delegate of the referenced assembly.

Note that if you redeclare the delegate, the compiler will treat it as a different type even if it has the exact same name and siganature.
0
 
LVL 14

Expert Comment

by:AvonWyss
ID: 10184463
Can you please tell me what you would have expected as answer to award an A, so that I can give better answers in the future? Thank you!
0

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

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

Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
This article aims to explain the working of CircularLogArchiver. This tool was designed to solve the buildup of log file in cases where systems do not support circular logging or where circular logging is not enabled
In this video we outline the Physical Segments view of NetCrunch network monitor. By following this brief how-to video, you will be able to learn how NetCrunch visualizes your network, how granular is the information collected, as well as where to f…
Add bar graphs to Access queries using Unicode block characters. Graphs appear on every record in the color you want. Give life to numbers. Hopes this gives you ideas on visualizing your data in new ways ~ Create a calculated field in a query: …
Suggested Courses

777 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