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
privateint writeIt(refbyte 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(refbyte wDat, refint 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):
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?
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!
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#
class MyCR: CRInterface
publicevent ProgressChangeHandler ProgressChanged;
publicint ProcessTWriting(refbyte WDat, refint pb_value)
status = AttemptWrites(ref pb_value);
privateint AttemptWrites(refint pb_value)
for (int i = 0; i < ((wDat.Length) / 4); i++)
//as it does writes, I want to set pb_value here to % complete
privatevoid RaiseProgressChange(int 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 classprivate FormLoad(...)
cr.ProgressChanged += new ProgressChangeHandler(CR_ProgressChanged);
privatevoid CR_ProgressChanged(int progress)
progressBar.Value = progress;
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
Last Visit: 31-Dec-99 19:00 Last Update: 20-Jan-17 4:17