Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 359
  • Last Modified:

Question on using delegate

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
mehtas
Asked:
mehtas
  • 4
  • 4
1 Solution
 
bgungorCommented:
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
 
mehtasAuthor Commented:
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
 
mehtasAuthor Commented:
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
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.

 
mehtasAuthor Commented:
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
 
AvonWyssCommented:
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
 
AvonWyssCommented:
Oops. Missed one fix...:

In Class1,

     public static bool ProgressDelegate(string, long)

should read

     public static bool UploadProgress(string, long)
0
 
mehtasAuthor Commented:
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
 
AvonWyssCommented:
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
 
AvonWyssCommented:
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

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

  • 4
  • 4
Tackle projects and never again get stuck behind a technical roadblock.
Join Now