Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. Several event handlers, one handler function, how to know which event handler has been used??

Several event handlers, one handler function, how to know which event handler has been used??

Scheduled Pinned Locked Moved C#
comhelptutorialquestion
7 Posts 5 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • B Offline
    B Offline
    blackhattrick
    wrote on last edited by
    #1

    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

    D S OriginalGriffO B 4 Replies Last reply
    0
    • B blackhattrick

      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

      D Offline
      D Offline
      DaveyM69
      wrote on last edited by
      #2

      You'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)

      1 Reply Last reply
      0
      • B blackhattrick

        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

        S Offline
        S Offline
        Samer Aburabie
        wrote on last edited by
        #3

        Use 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 !

        1 Reply Last reply
        0
        • B blackhattrick

          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

          OriginalGriffO Offline
          OriginalGriffO Offline
          OriginalGriff
          wrote on last edited by
          #4

          If '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 no idea what I did, but I'm taking full credit for it." - ThisOldTony
          "Common sense is so rare these days, it should be classified as a super power" - Random T-shirt

          1 Reply Last reply
          0
          • B blackhattrick

            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

            B Offline
            B Offline
            blackhattrick
            wrote on last edited by
            #5

            :doh: Thx a lot guys :)

            R 1 Reply Last reply
            0
            • B blackhattrick

              :doh: Thx a lot guys :)

              R Offline
              R Offline
              riced
              wrote on last edited by
              #6

              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() be new 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

              B 1 Reply Last reply
              0
              • R riced

                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() be new 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

                B Offline
                B Offline
                blackhattrick
                wrote on last edited by
                #7

                Yeah my mistake typing that... Thanks!

                1 Reply Last reply
                0
                Reply
                • Reply as topic
                Log in to reply
                • Oldest to Newest
                • Newest to Oldest
                • Most Votes


                • Login

                • Don't have an account? Register

                • Login or register to search.
                • First post
                  Last post
                0
                • Categories
                • Recent
                • Tags
                • Popular
                • World
                • Users
                • Groups