Event/Delegate for progress bar issue
-
Good start. The Cr5_Comm class is missing from that. Good idea for ProgressChangeHandler to lose the 2. If you don't have the iCR_Comm interface, it's like I never made my changes at all.
To use the other interface, the CR5 class would need to inherit from it and implement it.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;namespace ConsoleApplicationXXIV
{
enum CRType { CR5, CR6 }
public delegate void ProgressChangeHandler2(int progress, int id);
public interface iCRComm
{
event ProgressChangeHandler2 ProgressChanged;
int ReadTag(ref byte[] dataDumpWriteCheck);
}
public class CR5 : iCRComm
{
public int SlaveIndex { get; set; }
public event ProgressChangeHandler2 ProgressChanged;
public CR5(int slaveIndex)
{
SlaveIndex = slaveIndex;
}
public int ReadTag(ref byte[] dataDumpWriteCheck)
{
Console.WriteLine(String.Format("Data: {0}", dataDumpWriteCheck));
return 0;
}
private void RaiseProgressChange(int progress, int id)
{
if (ProgressChanged != null) //this is null
{
ProgressChanged(progress, id); //not getting here
}
}
}
class GenericPC
{
public iCRComm GetPC()
{
var cr = Program.GetCR(CRType.CR5, 0);
cr.ProgressChanged += new ProgressChangeHandler2(updateProgressBar);
byte[] b = null;
cr.ReadTag(ref b);
return cr;
}private void updateProgressBar(int n, int id) { Console.WriteLine(String.Format("Progress: {0}\\tid:{1}", n, id)); } } class Program { static void Main(string\[\] args) { var npc = new GenericPC(); npc.GetPC(); Console.ReadKey(); } public static iCRComm GetCR(CRType type, int slaveIndex) { iCRComm cr = null; switch (type) { case CRType.CR5: cr = new CR5(slaveIndex); break; case CRType.CR6: cr = null; //new CR6(slaveIndex); break; default: throw new ArgumentException(string.Format("A CR of type {0} cannot be found", Enum.GetName(typeof(CRType), type))); } return cr; } }
}
Bastard Programmer from Hell :suss: If you can't read my code, try converting it
-
To use the other interface, the CR5 class would need to inherit from it and implement it.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;namespace ConsoleApplicationXXIV
{
enum CRType { CR5, CR6 }
public delegate void ProgressChangeHandler2(int progress, int id);
public interface iCRComm
{
event ProgressChangeHandler2 ProgressChanged;
int ReadTag(ref byte[] dataDumpWriteCheck);
}
public class CR5 : iCRComm
{
public int SlaveIndex { get; set; }
public event ProgressChangeHandler2 ProgressChanged;
public CR5(int slaveIndex)
{
SlaveIndex = slaveIndex;
}
public int ReadTag(ref byte[] dataDumpWriteCheck)
{
Console.WriteLine(String.Format("Data: {0}", dataDumpWriteCheck));
return 0;
}
private void RaiseProgressChange(int progress, int id)
{
if (ProgressChanged != null) //this is null
{
ProgressChanged(progress, id); //not getting here
}
}
}
class GenericPC
{
public iCRComm GetPC()
{
var cr = Program.GetCR(CRType.CR5, 0);
cr.ProgressChanged += new ProgressChangeHandler2(updateProgressBar);
byte[] b = null;
cr.ReadTag(ref b);
return cr;
}private void updateProgressBar(int n, int id) { Console.WriteLine(String.Format("Progress: {0}\\tid:{1}", n, id)); } } class Program { static void Main(string\[\] args) { var npc = new GenericPC(); npc.GetPC(); Console.ReadKey(); } public static iCRComm GetCR(CRType type, int slaveIndex) { iCRComm cr = null; switch (type) { case CRType.CR5: cr = new CR5(slaveIndex); break; case CRType.CR6: cr = null; //new CR6(slaveIndex); break; default: throw new ArgumentException(string.Format("A CR of type {0} cannot be found", Enum.GetName(typeof(CRType), type))); } return cr; } }
}
Bastard Programmer from Hell :suss: If you can't read my code, try converting it
-
-
Yes, but I have two factories. One for CR5 interface and one for CRComm interface. It may not be obvious from what I posted, but they have very different uses. Generic needs to use CR5 for writes. CR5Comm is used all over the place and definitely isn't the same/can't be condensed into one with CR5. You're changing the use of everything too much for it to be helpful. Maybe we need to go back to the original VS project definitions of everything? I know it's difficult, but it's like that for a reason. Even where you put the delegate doesn't translate to anywhere in my code.
-
Yes, but I have two factories. One for CR5 interface and one for CRComm interface. It may not be obvious from what I posted, but they have very different uses. Generic needs to use CR5 for writes. CR5Comm is used all over the place and definitely isn't the same/can't be condensed into one with CR5. You're changing the use of everything too much for it to be helpful. Maybe we need to go back to the original VS project definitions of everything? I know it's difficult, but it's like that for a reason. Even where you put the delegate doesn't translate to anywhere in my code.
MichCl wrote:
It may not be obvious from what I posted, but they have very different uses.
I'd expect anything out that factory to behave similar; otherwise you'd be checking whether you're working on a CR5 or a CR6 everywhere in code, making things even more complicated.
MichCl wrote:
Even where you put the delegate doesn't translate to anywhere in my code.
As far as the compiler is concerned, it's located in "some" namespace. As long as it's referenced, it can be used.
Bastard Programmer from Hell :suss: If you can't read my code, try converting it here[^]
-
MichCl wrote:
It may not be obvious from what I posted, but they have very different uses.
I'd expect anything out that factory to behave similar; otherwise you'd be checking whether you're working on a CR5 or a CR6 everywhere in code, making things even more complicated.
MichCl wrote:
Even where you put the delegate doesn't translate to anywhere in my code.
As far as the compiler is concerned, it's located in "some" namespace. As long as it's referenced, it can be used.
Bastard Programmer from Hell :suss: If you can't read my code, try converting it here[^]
Like I said. There are two different factories. One for comm, one for crx. I get two different things from them. The fact that I don't have a check for CR5 or CR6, is because my cr factory returns an instance of iCR that is an instance of either cr5 or cr6. When I make a call from my instance of icr that is returned, it will automatically go to the implementation of cr5 or cr6, whichever I instantiated in the factory. I realize the compiler doesn't care about the delegate location and usage, but I do. The structure/fix that you are suggesting doesn't translate even slightly to my code, which is in separate vs projects. I can't put my delegate where you have it, so it's not a fix for my problem. Thanks for the help!
-
Like I said. There are two different factories. One for comm, one for crx. I get two different things from them. The fact that I don't have a check for CR5 or CR6, is because my cr factory returns an instance of iCR that is an instance of either cr5 or cr6. When I make a call from my instance of icr that is returned, it will automatically go to the implementation of cr5 or cr6, whichever I instantiated in the factory. I realize the compiler doesn't care about the delegate location and usage, but I do. The structure/fix that you are suggesting doesn't translate even slightly to my code, which is in separate vs projects. I can't put my delegate where you have it, so it's not a fix for my problem. Thanks for the help!
MichCl wrote:
The structure/fix that you are suggesting doesn't translate even slightly to my code, which is in separate vs projects.
Aight, can you add in the project names in the original post? Make 'em fake ones if required :)
Bastard Programmer from Hell :suss: If you can't read my code, try converting it here[^]
-
MichCl wrote:
The structure/fix that you are suggesting doesn't translate even slightly to my code, which is in separate vs projects.
Aight, can you add in the project names in the original post? Make 'em fake ones if required :)
Bastard Programmer from Hell :suss: If you can't read my code, try converting it here[^]
-
MichCl wrote:
The structure/fix that you are suggesting doesn't translate even slightly to my code, which is in separate vs projects.
Aight, can you add in the project names in the original post? Make 'em fake ones if required :)
Bastard Programmer from Hell :suss: If you can't read my code, try converting it here[^]
-
Guess what???! I cleaned all of my classes, detached and re-attached references, and it's working now. I guess I had the delegates/events and the interfaces set up correctly after all!!!! I was stepping through and noticed an exception was being thrown so I wasn't getting to my event setup (+/-), so that's why it was null and missing updating the progressBar. :) Thanks for the help!!
-
Guess what???! I cleaned all of my classes, detached and re-attached references, and it's working now. I guess I had the delegates/events and the interfaces set up correctly after all!!!! I was stepping through and noticed an exception was being thrown so I wasn't getting to my event setup (+/-), so that's why it was null and missing updating the progressBar. :) Thanks for the help!!