Several event handlers, one handler function, how to know which event handler has been used??
-
I have a serial ports list. I declared it like this:
private List<SerialPort> lstSerialPorts; for (int i = 1; i<=cont; i++) { SerialPort tempPort = new SerialPort("COM" + cont.ToString(), 2400); lstSerialPorts.Add(tempPort); lstSerialPörts[i].DataReceived += SerialDataReceivedEventHandler(TEST_FUNCTION); }
My TEST_FUNCTION is empty so far:void TEST_FUNCTION(object sender, SerialDataReceivedEventArgs e) { throw new NotImplementedException(); }
As you can see, all serial ports have the same handler function. How could I check which serial port triggered the TEST_FUNCTION? It would be great something like this:void TEST_FUNCTION(object sender, SerialDataReceivedEventArgs e) { Console.WriteLine("This function was triggered by: {0}",anUnknownVariable); }
Parameter "e" does not have any useful property or function Any help would be apreciated and excuse me about my English if I made a mistake :) Ivan -
I have a serial ports list. I declared it like this:
private List<SerialPort> lstSerialPorts; for (int i = 1; i<=cont; i++) { SerialPort tempPort = new SerialPort("COM" + cont.ToString(), 2400); lstSerialPorts.Add(tempPort); lstSerialPörts[i].DataReceived += SerialDataReceivedEventHandler(TEST_FUNCTION); }
My TEST_FUNCTION is empty so far:void TEST_FUNCTION(object sender, SerialDataReceivedEventArgs e) { throw new NotImplementedException(); }
As you can see, all serial ports have the same handler function. How could I check which serial port triggered the TEST_FUNCTION? It would be great something like this:void TEST_FUNCTION(object sender, SerialDataReceivedEventArgs e) { Console.WriteLine("This function was triggered by: {0}",anUnknownVariable); }
Parameter "e" does not have any useful property or function Any help would be apreciated and excuse me about my English if I made a mistake :) IvanYou've looked at the e parameter - now try the other one! The clue is in the name 'sender'. Cast it to a SerialPort from object and you now have the object that raised the event.
Dave
BTW, in software, hope and pray is not a viable strategy. (Luc Pattyn)
Visual Basic is not used by normal people so we're not covering it here. (Uncyclopedia)
Why are you using VB6? Do you hate yourself? (Christian Graus) -
I have a serial ports list. I declared it like this:
private List<SerialPort> lstSerialPorts; for (int i = 1; i<=cont; i++) { SerialPort tempPort = new SerialPort("COM" + cont.ToString(), 2400); lstSerialPorts.Add(tempPort); lstSerialPörts[i].DataReceived += SerialDataReceivedEventHandler(TEST_FUNCTION); }
My TEST_FUNCTION is empty so far:void TEST_FUNCTION(object sender, SerialDataReceivedEventArgs e) { throw new NotImplementedException(); }
As you can see, all serial ports have the same handler function. How could I check which serial port triggered the TEST_FUNCTION? It would be great something like this:void TEST_FUNCTION(object sender, SerialDataReceivedEventArgs e) { Console.WriteLine("This function was triggered by: {0}",anUnknownVariable); }
Parameter "e" does not have any useful property or function Any help would be apreciated and excuse me about my English if I made a mistake :) IvanUse the sender object, cast it to SerialPort and you will get the object who triggered the function.
Sincerely Samer Abu Rabie Imagination is more important than knowledge !
-
I have a serial ports list. I declared it like this:
private List<SerialPort> lstSerialPorts; for (int i = 1; i<=cont; i++) { SerialPort tempPort = new SerialPort("COM" + cont.ToString(), 2400); lstSerialPorts.Add(tempPort); lstSerialPörts[i].DataReceived += SerialDataReceivedEventHandler(TEST_FUNCTION); }
My TEST_FUNCTION is empty so far:void TEST_FUNCTION(object sender, SerialDataReceivedEventArgs e) { throw new NotImplementedException(); }
As you can see, all serial ports have the same handler function. How could I check which serial port triggered the TEST_FUNCTION? It would be great something like this:void TEST_FUNCTION(object sender, SerialDataReceivedEventArgs e) { Console.WriteLine("This function was triggered by: {0}",anUnknownVariable); }
Parameter "e" does not have any useful property or function Any help would be apreciated and excuse me about my English if I made a mistake :) IvanIf 'e' is not a lot of use, have you considered looking at 'sender', by any chance? If you expand it in the debugger, you may find it is a SerialPort object.
No trees were harmed in the sending of this message; however, a significant number of electrons were slightly inconvenienced. This message is made of fully recyclable Zeros and Ones
-
I have a serial ports list. I declared it like this:
private List<SerialPort> lstSerialPorts; for (int i = 1; i<=cont; i++) { SerialPort tempPort = new SerialPort("COM" + cont.ToString(), 2400); lstSerialPorts.Add(tempPort); lstSerialPörts[i].DataReceived += SerialDataReceivedEventHandler(TEST_FUNCTION); }
My TEST_FUNCTION is empty so far:void TEST_FUNCTION(object sender, SerialDataReceivedEventArgs e) { throw new NotImplementedException(); }
As you can see, all serial ports have the same handler function. How could I check which serial port triggered the TEST_FUNCTION? It would be great something like this:void TEST_FUNCTION(object sender, SerialDataReceivedEventArgs e) { Console.WriteLine("This function was triggered by: {0}",anUnknownVariable); }
Parameter "e" does not have any useful property or function Any help would be apreciated and excuse me about my English if I made a mistake :) Ivan:doh: Thx a lot guys :)
-
:doh: Thx a lot guys :)
I know your proble is solved but something I noticed in the code and wondered if it was correct. Should
new SerialPort("COM" + cont.ToString()
benew SerialPort("COM" + i.ToString()
? Good of you to thank the guys. Not everyone does. :)Regards David R --------------------------------------------------------------- "Every program eventually becomes rococo, and then rubble." - Alan Perlis
-
I know your proble is solved but something I noticed in the code and wondered if it was correct. Should
new SerialPort("COM" + cont.ToString()
benew SerialPort("COM" + i.ToString()
? Good of you to thank the guys. Not everyone does. :)Regards David R --------------------------------------------------------------- "Every program eventually becomes rococo, and then rubble." - Alan Perlis
Yeah my mistake typing that... Thanks!