Click here to Skip to main content
       

C#

 
You must Sign In to use this message board.
Search this forum  
    Spacing  Noise  Layout  Per page   
QuestionHow to update progressBar from another classmemberMichCl15 Jan '13 - 4:17 
I have a form with a progressBar on it. I'm doing writes in another class (through my interface) and need to show my progress of doing the writes, but I'm getting an error message *. Hopefully someone has an idea how to get the error to go away and so I can update my progressBar from a different class.
 
I'm more of a C++ developer doing C#, so there might be something that an experienced C# person would know of that I'm missing.
 
It looks like this:
 
PC.cs [Design]
Form with progressBar displayed
 
PC.cs
private int writeIt(ref byte[] wDat, byte[] sDat)
{
     result = cr.ProcessTWriting(ref wDat, ref pb_Progress.Value); //this passes a ref to the progressBar value
}
 
* In the code above, I'm getting "A property, indexer or dynamic member access may not be passed as an out or ref parameter" error message.
 
My interface looks like this:
 
iCR.cs (which is in another VS project due to future CR6, etc. which will use the interface)
int ProcessTWriting(ref byte[] wDat, ref int pb_value);
 
Then, I want to pass my references (to progressBar value) to where I'm writing my bytes (i to 1024), and update the reference to the progressBar as it iterates through, writing to it's destination.
 
This would be in my CR5.cs which implements iCR.cs (and is in another VS project still...think Factory pattern):
public int ProcessTWriting(ref byte[] WDat, ref int pb_value)
{
        status = AttemptWrites(ref pb_value);
}              
 
And the method it calls:
private int AttemptWrites(ref int pb_value)
{
     for (int i = 0; i < ((wDat.Length) / 4); i++)
     {
        //as it does writes, I want to set pb_value here to % complete
     }
}
 
Does anyone have a suggestion how to get my progressBar to update as I iterate through each byte I'm writing in my other class? I've looked at a couple of similar situations and there do seem to be people passing an index of an array, but I'm having trouble figuring out how to apply them to my situation. If I assign pb_value to a temporary value, passing it as a reference, it won't update my progressBar.Value as I do my writes and update the temporary reference to the value. Here's the links I found on the topic:
http://stackoverflow.com/questions/4813289/a-property-indexer-or-dynamic-member-access-may-not-be-passed-as-an-out-or-ref[^]
http://stackoverflow.com/questions/12440013/a-property-indexer-or-dynamic-member-access-may-not-be-passed-as-an-out-or-ref[^]
http://stackoverflow.com/questions/4518956/a-property-or-indexer-may-not-be-passed-as-an-out-or-ref-parameter[^]
http://stackoverflow.com/questions/6492097/a-property-indexer-or-dynamic-member-access-may-not-be-passed-as-an-out-or-ref[^]
 
Hopefully it's not TMI, but I figure, the more info the better so people can see where I'm coming from.
SuggestionRe: How to update progressBar from another class PinmemberHuorSwords15 Jan '13 - 5:50 
Sorry in advance for my english. Possibly is not good enough yet...
 
Do you have the possibility to change your interface?
 
I'm thinking that a solution to your problem could be the use of events on your CR5 class. (Observer pattern)
 
You could create an event on CR5 class that can be subscribed by the class that contains the progress bar. Then, you can fire your event on the AttempWrite function with a parameter that indicates de current progress value to set into the progress bar.
 
Is it a possibility? Or are you constrained to this interface contract?
GeneralRe: How to update progressBar from another class PinmemberMichCl15 Jan '13 - 7:47 
That's a good idea. I'll give that a try. I can absolutely change my interface. That's why I'm showing the progressBar value in the interface now. I'm not absolutely sure how to send a value when I fire an event, but I'll take a look. Thanks!
GeneralRe: How to update progressBar from another class PinmvpDave Kreskowiak15 Jan '13 - 8:03 
MichCl wrote:
I'm not absolutely sure how to send a value when I fire an event

 
Create your own event and with that, you create your own event arguments class to carry the data you want to send in the event.

GeneralRe: How to update progressBar from another class Pinmemberjibesh15 Jan '13 - 8:54 
This sample might help you to get going with event usage
public delegate void ProgressChangeHandler(int progress);
class MyClass
{
   public event ProgressChangeHandler ProgressChanged;
   
   public void Process()
   {
      RaiseProgressChange(progress); //set your progress 
   }
   private void RaiseProgressChange(int progress)
   {
     if(ProgressChanged!= null)
     {
       ProgressChanged(progress);
     }
   }
}
Jibesh V P

GeneralRe: How to update progressBar from another class PinmemberMichCl15 Jan '13 - 9:00 
I'm still not sure about the event with parameters!! If I had the delegate above, how would that be used to fire an event and notify the method waiting for the integer value? I started with this, but I'm not sure how to have an event call it. I was looking at this link: http://stackoverflow.com/questions/1488099/call-an-eventhandler-with-arguments[^]
 
private void _updateProgressBar(object sender, EventArgs e, int i)
{
//call code from here waiting for int i
}
GeneralRe: How to update progressBar from another class Pinmemberjibesh15 Jan '13 - 9:09 
The delegate you are mentioned here is one that comes with the .Net framework i.e "EventHandler" it was defined like
public delegate void EventHandler(object sender, EventArgs e);
inside one of the Framework class. likewise we can also define our own delegates that suits to our need. here instead of using the native handler I defined a custom delegate to fire an event. Read the following articles which explains more about events and delegates. Events-and-Delegates-Simplified and Step-by-Step-Event-handling-in-C#
Jibesh V P

GeneralRe: How to update progressBar from another class Pinmemberjibesh15 Jan '13 - 9:24 
Ok I will try to explain with your class name
 
define a event in your interface iCR.cs
public delegate void ProgressChangeHandler(int progress);
 
interface CRInterface
{
  event ProgressChangeHandler ProgressChanged;
  int ProcessTWriting(ref byte[] WDat, ref int pb_value)
}
Implement this interface in CRD.cs class
class MyCR: CRInterface
{
  public event ProgressChangeHandler ProgressChanged;
  
  public int ProcessTWriting(ref byte[] WDat, ref int pb_value)
  {
    status = AttemptWrites(ref pb_value);
  } 
  
  private int AttemptWrites(ref int pb_value)
  {
     for (int i = 0; i < ((wDat.Length) / 4); i++)
     {
        RaiseProgressChange(i);
        //as it does writes, I want to set pb_value here to % complete
     }
  }
  
   private void RaiseProgressChange(int progress)
   {
     if(ProgressChanged!= null)
     {
       ProgressChanged(progress);
     }
   }
}
Finally Subscribe for event change in your FormClass i.e PC.cs
// call this line just below where you create the instance of CR5 class
private FormLoad(...)
{
  cr.ProgressChanged += new ProgressChangeHandler(CR_ProgressChanged);
}
 
private void CR_ProgressChanged(int progress)
{
  progressBar.Value = progress;
}
Hope this helps.
Jibesh V P

GeneralRe: How to update progressBar from another class PinmemberMichCl15 Jan '13 - 9:55 
Thanks for the extra info, Jibesh. It's not often I have to deal with events. When I tried to do the above in my CR5.cs (implements iCR),
 
public event ProgressChangeHandler ProgressChanged; 

 
it says "The type or namespace name 'ProgressChangeHandler' could not be found (are you missing a using directive or an assembly reference?)"
 
and also, iCR
public delegate void ProgressChangeHandler(int progress);
 
I get "'ProgressChangeHandler': interfaces cannot declare types"
 
Maybe it's not going to work with an interface between PC.cs and CR5.cs (implements iCR)?
GeneralRe: How to update progressBar from another class Pinmemberjibesh15 Jan '13 - 10:06 
The above code sample is working version if you closely look at the sample you can find that delegate method is defined in the dll where iCR is located and you don't need to use the Public access specifier for Interface methods and data.
 
This will definitely work and I have done same stuff in the past.
 
ProgressChangeHandler must be defined globally so that this delegate is accessible by all members.
Jibesh V P

GeneralRe: How to update progressBar from another class PinmemberMichCl15 Jan '13 - 10:15 
Thank you very much!!!! This is very helpful. I got it building now.
GeneralRe: How to update progressBar from another class Pinmemberjibesh15 Jan '13 - 11:34 
Good Happy to hear that.
Jibesh V P

GeneralRe: How to update progressBar from another class PinmemberMichCl17 Jan '13 - 5:18 
If I could add one more little question onto the above, which I got to work. I also have a C_Comm.cs class which is referenced in my CR5.cs, the original PC.cs, and iCR.cs. It turns out that one area I need to update my progress bar (in PC.cs) from is C_Comm, which is also in a different VS project. I'm trying to get it working, and am getting an error: Cannot implicitly convert type 'ProgressChangeHandler' to C_Comm.ProgressChangeHandler'
 
Right now it looks like this:
 
PC.cs (the form code)
public event ProgressChangeHandler ProgressChanged;
 
public void GetPC(...)
{
   cr.ProgressChanged += new ProgressChangeHandler(updateProgressBar); //cr is instance of CR5 through iCR interface/factory
   cb.ProgressChanged += new ProgressChangeHandler(updateProgressBar); //cb is C_Comm 
}
 
public int updateProgressBar(int i)
{
    pb.Value = i;
}
 
iCR.cs (interface)
public delegate void ProgressChangeHandler(int progress);
public interface iCR
{
    event ProgressChangeHandler ProgressChanged;
}
 
CR5.cs (implements iCR)
public event ProgressChangeHandler ProgressChanged;
private int AttemptWrites()
{
   for(i=0...)
   {
        RaiseProgressChange(i);
   }
}
 
private void RaiseProgressChange(int progress)
{
    if (ProgressChanged != null)
    {
         ProgressChanged(progress);
    }
}
 
C_Comm.cs
public delegate void ProgressChangeHandler(int progress);
 
public class C_Comm{
    public event ProgressChangeHandler ProgressChanged;
 
    public int Read(byte[] data, int ind)
    {
        for(int i...)
        {
            RaiseProgressChange(i);
        }
    }
 
    private void RaiseProgressChange(int progress)
    {
        if (ProgressChanged != null)
        {
            ProgressChanged(progress);
        }
    }
}  
  
 
Do you know how I could get the ProgressBar in PC.cs to be updated also from C_Comm.cs?
GeneralRe: How to update progressBar from another class Pinmemberjibesh17 Jan '13 - 8:17 
if C_Comm.cs file is common to both project you can remove the delegate declaration inside the interface class and use the delegate inside C_Comm.cs in all places that should be fine
Jibesh V P

GeneralRe: How to update progressBar from another class PinmemberMichCl17 Jan '13 - 8:59 
I tried to just have the delegate in my C_Comm.cs (not in iCR), and now in my CR5.cs class, I get the error "The name 'ProgressChanged' does not exist in the current context" in my RaiseProgressChange method. Then if I try to use cb.ProgressChanged != null, I get the error "The event 'C_Comm.ProgressChanged' can only appear on the left hand side of += or -=". I'm not very clear on using events! Do you have any ideas?
GeneralRe: How to update progressBar from another class Pinmemberjibesh17 Jan '13 - 9:18 
I dont see any error in your previous code other than two declaration of the delegates ProgressChangeHandler.
 
is C_Comm.cs is defined in a common dll? can you give some more input about the project files where these .cs files are added.
 
If it was me, I keep all the common implementations/interfaces in a shared dll and refer these dll in different projects rather keeping same .cs files each project.
 
what are you trying to do here with this 'cb.ProgressChanged != null' checking for null right??
Jibesh V P

GeneralRe: How to update progressBar from another class [modified] PinmemberMichCl17 Jan '13 - 9:44 
C_Comm.cs is a separate VS project, separate dll, commonly referenced by everything (CR5.cs, PC.cs). Do you think I still need everything except the delegate part in my iCR? The CR5 implements iCR and that's the only way I can see getting the ProgressChanged defined in CR5 from PC.cs, but when I take out the delegate part, I get "The type or namespace name 'ProgressChangeHandler' could not be found (are you missing a using directive or an assembly reference?)". I can't keep my iCR.cs and C_Comm.cs in the same dll because of the whole layout. The VS project layout is:
 
Project 1 has iCR.cs
 
Project 2 has PC.cs and a couple of other things. (update - this has form with progress bar)
 
Project 3 has CR5 (implements iCR) and a couple of other things.
 
Project 4 has CR_Factory which will return a CR5 through the iCR interface to my PC.cs
 
Project 5 has C_Comm.cs. It is referenced in PC.cs, CR5, and iCR.
 
In the future I'll have a CR6 project like the CR5.
 
As far as cb.ProgressChanged != null, I'm just trying to get what you suggested originally to work with my delegate in my C_Comm. Yes, checking for null before I send the update.

modified 17 Jan '13 - 16:18.

GeneralRe: How to update progressBar from another class Pinmemberjibesh17 Jan '13 - 10:24 
I dont see any problems here. did you refer Project5 in iCRS
Jibesh V P

GeneralRe: How to update progressBar from another class PinmemberMichCl18 Jan '13 - 2:07 
I can't reference CR5 in iCR because that would be an incorrect implementation for factory design pattern. Plus, CR5 is an iCR and if iCR referenced CR5, it would be circular.
GeneralRe: How to update progressBar from another class PinmemberMichCl18 Jan '13 - 7:16 
I wound up leaving the original delegate in my iCR and everything you originally recommended above. Then I created a separate delegate and handler in my C_Comm.cs class. I couldn't see any other way to update my progressBar and also share the same delegate and event. Thanks for the help!
GeneralRe: How to update progressBar from another class Pinmemberjibesh18 Jan '13 - 13:55 
I dont see any error in your declaration, i.e the delegate method is declared in Comm.cs file which is referred in all the other 3(or more project) iCR,CR5 and form Class. so the delegate should be available in all other classes and you need to include the proper namespaces in other project files.
Jibesh V P

GeneralRe: How to update progressBar from another class PinmemberMichCl21 Jan '13 - 4:53 
Thank you so much for the help. We have a very small work group here, and they are not programmers and don't understand this stuff (not to mention me having a tough time with events and this architecture). It turned out I wasn't "using" C_Comm in my interface. Once I did that and fixed a couple of declarations to use the C_Comm version of the Handler, all worked. Smile | :)
GeneralRe: How to update progressBar from another class Pinmemberjibesh21 Jan '13 - 5:00 
Glad to hear this. Good Luck
Jibesh
Jibesh V P

GeneralRe: How to update progressBar from another class PinmemberMichCl21 Jan '13 - 5:01 
Thank you! And thanks for sticking with this thread.
GeneralRe: How to update progressBar from another class Pinmemberjibesh21 Jan '13 - 5:02 
You are welcome!! Cheers!
Jibesh V P

GeneralRe: How to update progressBar from another class PinmemberMichCl6 May '13 - 3:44 
@jibesh - I added an interface (see my recent question), and my event is null when I try to update my progress bar. I know you have a good idea about events and interfaces. If you could take a look at my newly posted question(Thursday post maybe), I'd appreciate it. Thanks!

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   


Advertise | Privacy | Mobile
Web03 | 2.6.130523.1 | Last Updated 26 May 2013
Copyright © CodeProject, 1999-2013
All Rights Reserved. Terms of Use
Layout: fixed | fluid