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. Passing different variable types

Passing different variable types

Scheduled Pinned Locked Moved C#
help
13 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.
  • M Offline
    M Offline
    mprice214
    wrote on last edited by
    #1

    Hi all, I have the following code that I need to return dblRx our of DisplayText in order to use it in another function. Problem I'm having is that if I try to return a double from DisplayText (private double DisplayText), I get the EventHandler error from this.Invoke that I have the wrong data type. Any help would be appreciated.

    private void DisplayText(object sender, EventArgs e)
    {
    RxString = RxString.Remove(0, 3); //This removes the first 4 characters
    //Need to convert RxString to double for formatting
    double dblRx = double.Parse (RxString);
    //Converting back to a string will result in leading zeros removed
    RxString = dblRx.ToString();
    textBox1.Text = RxString;

        }// End of DisplayText
    
        private void serialPort1\_DataReceived(object sender, System.IO.Ports.SerialDataReceivedEventArgs e)
        {
            RxString = "";
    
    
            try
            {
                RxString = serialPort1.ReadTo("\\r");
                serialPort1.WriteLine("\*X01\\r");
            }
            catch (Exception Exception)
            {
                return;
            }
                     
            this.Invoke(new EventHandler(DisplayText));
        }//End of serialPort1\_DataReceived
    
    L K 2 Replies Last reply
    0
    • M mprice214

      Hi all, I have the following code that I need to return dblRx our of DisplayText in order to use it in another function. Problem I'm having is that if I try to return a double from DisplayText (private double DisplayText), I get the EventHandler error from this.Invoke that I have the wrong data type. Any help would be appreciated.

      private void DisplayText(object sender, EventArgs e)
      {
      RxString = RxString.Remove(0, 3); //This removes the first 4 characters
      //Need to convert RxString to double for formatting
      double dblRx = double.Parse (RxString);
      //Converting back to a string will result in leading zeros removed
      RxString = dblRx.ToString();
      textBox1.Text = RxString;

          }// End of DisplayText
      
          private void serialPort1\_DataReceived(object sender, System.IO.Ports.SerialDataReceivedEventArgs e)
          {
              RxString = "";
      
      
              try
              {
                  RxString = serialPort1.ReadTo("\\r");
                  serialPort1.WriteLine("\*X01\\r");
              }
              catch (Exception Exception)
              {
                  return;
              }
                       
              this.Invoke(new EventHandler(DisplayText));
          }//End of serialPort1\_DataReceived
      
      L Offline
      L Offline
      Luc Pattyn
      wrote on last edited by
      #2

      Not sure I understand your question very well, however here are some remarks: 1.

      mprice214 wrote:

      RxString.Remove(0, 3); //This removes the first 4 characters

      Not true. At best it removes 3 characters; and if there are fewer, expect an exception. 2.

      mprice214 wrote:

      double.Parse (RxString);

      that will fail if the data isn't really a string representation of a double. Don't do this on external data, it will bite you. Either add try-catch or better yet use TryParse(). 3. why does DisplayText() have parameters which make it look like a real event handler, you are not using those parameters, instead you are using some class member. It would seem logical to give it one parameter: the text it should display. 4.

      mprice214 wrote:

      serialPort1.WriteLine("*X01\r");

      very strange. You want a carriage return plus whatever the system uses for a newline (maybe "\r\n"). Either you like what the system gives you, or you don't; I would never mix the two. 5.

      mprice214 wrote:

      catch (Exception Exception) { return; }

      This is not acceptable; whatever goes wrong, you ignore it. So when it goes wrong, you will have an impossible job to diagnose and fix the problem. And believe me, serial communication always goes wrong sooner or later. You should NOT swallow exceptions, either catch a very specific one and add a comment as to why you really don't care about it, or log it somewhere so it leaves a trail. 6.

      mprice214 wrote:

      this.Invoke(new EventHandler(DisplayText));

      That does not work, as the parameter list does not match. First make sure which parameters the method should have, then make sure to provide them, using an overload of Invoke(). :)

      Luc Pattyn [Forum Guidelines] [Why QA sucks] [My Articles]


      Prolific encyclopedia fixture proof-reader browser patron addict?
      We all depend on the beast below.


      D M 2 Replies Last reply
      0
      • L Luc Pattyn

        Not sure I understand your question very well, however here are some remarks: 1.

        mprice214 wrote:

        RxString.Remove(0, 3); //This removes the first 4 characters

        Not true. At best it removes 3 characters; and if there are fewer, expect an exception. 2.

        mprice214 wrote:

        double.Parse (RxString);

        that will fail if the data isn't really a string representation of a double. Don't do this on external data, it will bite you. Either add try-catch or better yet use TryParse(). 3. why does DisplayText() have parameters which make it look like a real event handler, you are not using those parameters, instead you are using some class member. It would seem logical to give it one parameter: the text it should display. 4.

        mprice214 wrote:

        serialPort1.WriteLine("*X01\r");

        very strange. You want a carriage return plus whatever the system uses for a newline (maybe "\r\n"). Either you like what the system gives you, or you don't; I would never mix the two. 5.

        mprice214 wrote:

        catch (Exception Exception) { return; }

        This is not acceptable; whatever goes wrong, you ignore it. So when it goes wrong, you will have an impossible job to diagnose and fix the problem. And believe me, serial communication always goes wrong sooner or later. You should NOT swallow exceptions, either catch a very specific one and add a comment as to why you really don't care about it, or log it somewhere so it leaves a trail. 6.

        mprice214 wrote:

        this.Invoke(new EventHandler(DisplayText));

        That does not work, as the parameter list does not match. First make sure which parameters the method should have, then make sure to provide them, using an overload of Invoke(). :)

        Luc Pattyn [Forum Guidelines] [Why QA sucks] [My Articles]


        Prolific encyclopedia fixture proof-reader browser patron addict?
        We all depend on the beast below.


        D Offline
        D Offline
        dan sh
        wrote on last edited by
        #3

        Luc Pattyn wrote:

        maybe "\r\n"

        How about using Environment.NewLine?

        L D 2 Replies Last reply
        0
        • M mprice214

          Hi all, I have the following code that I need to return dblRx our of DisplayText in order to use it in another function. Problem I'm having is that if I try to return a double from DisplayText (private double DisplayText), I get the EventHandler error from this.Invoke that I have the wrong data type. Any help would be appreciated.

          private void DisplayText(object sender, EventArgs e)
          {
          RxString = RxString.Remove(0, 3); //This removes the first 4 characters
          //Need to convert RxString to double for formatting
          double dblRx = double.Parse (RxString);
          //Converting back to a string will result in leading zeros removed
          RxString = dblRx.ToString();
          textBox1.Text = RxString;

              }// End of DisplayText
          
              private void serialPort1\_DataReceived(object sender, System.IO.Ports.SerialDataReceivedEventArgs e)
              {
                  RxString = "";
          
          
                  try
                  {
                      RxString = serialPort1.ReadTo("\\r");
                      serialPort1.WriteLine("\*X01\\r");
                  }
                  catch (Exception Exception)
                  {
                      return;
                  }
                           
                  this.Invoke(new EventHandler(DisplayText));
              }//End of serialPort1\_DataReceived
          
          K Offline
          K Offline
          kevinnicol
          wrote on last edited by
          #4

          mprice214 wrote:

          this.Invoke(new EventHandler(DisplayText));

          Has some paramter issues, to get around it without changing too much code try this

          this.Invoke(new MethodInvoker(delegate{DisplayText(null, null);}));

          M 1 Reply Last reply
          0
          • D dan sh

            Luc Pattyn wrote:

            maybe "\r\n"

            How about using Environment.NewLine?

            L Offline
            L Offline
            Luc Pattyn
            wrote on last edited by
            #5

            If he wants "\r\n" then that is what he should set as SerialPort.NewLine One should not rely on Environment.NewLine for strings that go beyond the current system (files that get exported, serial communication, etc). BTW: MSDN is confusing about the default value of SerialPort.NewLine (it says it is "\n" but refers to Environment.NewLine) :)

            Luc Pattyn [Forum Guidelines] [Why QA sucks] [My Articles]


            Prolific encyclopedia fixture proof-reader browser patron addict?
            We all depend on the beast below.


            D 1 Reply Last reply
            0
            • L Luc Pattyn

              Not sure I understand your question very well, however here are some remarks: 1.

              mprice214 wrote:

              RxString.Remove(0, 3); //This removes the first 4 characters

              Not true. At best it removes 3 characters; and if there are fewer, expect an exception. 2.

              mprice214 wrote:

              double.Parse (RxString);

              that will fail if the data isn't really a string representation of a double. Don't do this on external data, it will bite you. Either add try-catch or better yet use TryParse(). 3. why does DisplayText() have parameters which make it look like a real event handler, you are not using those parameters, instead you are using some class member. It would seem logical to give it one parameter: the text it should display. 4.

              mprice214 wrote:

              serialPort1.WriteLine("*X01\r");

              very strange. You want a carriage return plus whatever the system uses for a newline (maybe "\r\n"). Either you like what the system gives you, or you don't; I would never mix the two. 5.

              mprice214 wrote:

              catch (Exception Exception) { return; }

              This is not acceptable; whatever goes wrong, you ignore it. So when it goes wrong, you will have an impossible job to diagnose and fix the problem. And believe me, serial communication always goes wrong sooner or later. You should NOT swallow exceptions, either catch a very specific one and add a comment as to why you really don't care about it, or log it somewhere so it leaves a trail. 6.

              mprice214 wrote:

              this.Invoke(new EventHandler(DisplayText));

              That does not work, as the parameter list does not match. First make sure which parameters the method should have, then make sure to provide them, using an overload of Invoke(). :)

              Luc Pattyn [Forum Guidelines] [Why QA sucks] [My Articles]


              Prolific encyclopedia fixture proof-reader browser patron addict?
              We all depend on the beast below.


              M Offline
              M Offline
              mprice214
              wrote on last edited by
              #6

              1. mprice214 wrote: RxString.Remove(0, 3); //This removes the first 4 characters Not true. At best it removes 3 characters; and if there are fewer, expect an exception. Yes, understand. I originally was removing 4. 2. mprice214 wrote: double.Parse (RxString); that will fail if the data isn't really a string representation of a double. Don't do this on external data, it will bite you. Either add try-catch or better yet use TryParse(). Understand this too 3. why does DisplayText() have parameters which make it look like a real event handler, you are not using those parameters, instead you are using some class member. It would seem logical to give it one parameter: the text it should display. see #6 4. mprice214 wrote: serialPort1.WriteLine("*X01\r"); very strange. You want a carriage return plus whatever the system uses for a newline (maybe "\r\n"). Either you like what the system gives you, or you don't; I would never mix the two. The device only requires a cr and as result, it is fine with just \r or \r and \n. Why would a newline be necessary here? 5. mprice214 wrote: catch (Exception Exception) { return; } This is not acceptable; whatever goes wrong, you ignore it. So when it goes wrong, you will have an impossible job to diagnose and fix the problem. And believe me, serial communication always goes wrong sooner or later. You should NOT swallow exceptions, either catch a very specific one and add a comment as to why you really don't care about it, or log it somewhere so it leaves a trail. Understand. I am trying to get things working first and then am going back to clean up. As I'm relatively new to c#, I'm sure I'll miss some things. 6. mprice214 wrote: this.Invoke(new EventHandler(DisplayText)); That does not work, as the parameter list does not match. First make sure which parameters the method should have, then make sure to provide them, using an overload of Invoke(). When I run this, it works. Correct me if I am wrong, but doesn't this pass RxSring back to DisplayText? Are you saying there is a better way to do this? Regarding the initial question, I need to pass RxString out of DisplayText as a double to use for graphing purposes.

              L 1 Reply Last reply
              0
              • K kevinnicol

                mprice214 wrote:

                this.Invoke(new EventHandler(DisplayText));

                Has some paramter issues, to get around it without changing too much code try this

                this.Invoke(new MethodInvoker(delegate{DisplayText(null, null);}));

                M Offline
                M Offline
                mprice214
                wrote on last edited by
                #7

                I'm not sure why this would need to change, as it seems to work fine. I'm apparently missing something here. BTW, RxString is global.

                1 Reply Last reply
                0
                • M mprice214

                  1. mprice214 wrote: RxString.Remove(0, 3); //This removes the first 4 characters Not true. At best it removes 3 characters; and if there are fewer, expect an exception. Yes, understand. I originally was removing 4. 2. mprice214 wrote: double.Parse (RxString); that will fail if the data isn't really a string representation of a double. Don't do this on external data, it will bite you. Either add try-catch or better yet use TryParse(). Understand this too 3. why does DisplayText() have parameters which make it look like a real event handler, you are not using those parameters, instead you are using some class member. It would seem logical to give it one parameter: the text it should display. see #6 4. mprice214 wrote: serialPort1.WriteLine("*X01\r"); very strange. You want a carriage return plus whatever the system uses for a newline (maybe "\r\n"). Either you like what the system gives you, or you don't; I would never mix the two. The device only requires a cr and as result, it is fine with just \r or \r and \n. Why would a newline be necessary here? 5. mprice214 wrote: catch (Exception Exception) { return; } This is not acceptable; whatever goes wrong, you ignore it. So when it goes wrong, you will have an impossible job to diagnose and fix the problem. And believe me, serial communication always goes wrong sooner or later. You should NOT swallow exceptions, either catch a very specific one and add a comment as to why you really don't care about it, or log it somewhere so it leaves a trail. Understand. I am trying to get things working first and then am going back to clean up. As I'm relatively new to c#, I'm sure I'll miss some things. 6. mprice214 wrote: this.Invoke(new EventHandler(DisplayText)); That does not work, as the parameter list does not match. First make sure which parameters the method should have, then make sure to provide them, using an overload of Invoke(). When I run this, it works. Correct me if I am wrong, but doesn't this pass RxSring back to DisplayText? Are you saying there is a better way to do this? Regarding the initial question, I need to pass RxString out of DisplayText as a double to use for graphing purposes.

                  L Offline
                  L Offline
                  Luc Pattyn
                  wrote on last edited by
                  #8

                  mprice214 wrote:

                  The device only requires a cr

                  so use serialPort1.Write("*X01\r"); instead of WriteLine() and you're in charge, now it doesn't matter what SerialPort.NewLine contains any more.

                  mprice214 wrote:

                  get things working first

                  sure, so am I. By seeing exceptions right away. As a minimum, add Console.WriteLine(exception.ToString());. Either you don't have exceptions, then it makes no difference; or you have, and now you can see them.

                  mprice214 wrote:

                  it works

                  Yes, I forgot: Invoke without parameters provides some defaults, and since you don't really use the parameters inside DisplayText, there is no problem. However, I wouldn't do it like that.

                  mprice214 wrote:

                  Regarding the initial question

                  OK, if you are not really interested in the string, why not use a class member "double Rx;" instead of "string RxString;". Have DataReceived read the string, convert it to double, and store the value in Rx; then have DisplayText and any other interested method just read the Rx variable. :)

                  Luc Pattyn [Forum Guidelines] [Why QA sucks] [My Articles]


                  Prolific encyclopedia fixture proof-reader browser patron addict?
                  We all depend on the beast below.


                  M 1 Reply Last reply
                  0
                  • L Luc Pattyn

                    If he wants "\r\n" then that is what he should set as SerialPort.NewLine One should not rely on Environment.NewLine for strings that go beyond the current system (files that get exported, serial communication, etc). BTW: MSDN is confusing about the default value of SerialPort.NewLine (it says it is "\n" but refers to Environment.NewLine) :)

                    Luc Pattyn [Forum Guidelines] [Why QA sucks] [My Articles]


                    Prolific encyclopedia fixture proof-reader browser patron addict?
                    We all depend on the beast below.


                    D Offline
                    D Offline
                    dan sh
                    wrote on last edited by
                    #9

                    Thanks. :)

                    L 1 Reply Last reply
                    0
                    • D dan sh

                      Thanks. :)

                      L Offline
                      L Offline
                      Luc Pattyn
                      wrote on last edited by
                      #10

                      you're welcome. :)

                      Luc Pattyn [Forum Guidelines] [Why QA sucks] [My Articles]


                      Prolific encyclopedia fixture proof-reader browser patron addict?
                      We all depend on the beast below.


                      1 Reply Last reply
                      0
                      • L Luc Pattyn

                        mprice214 wrote:

                        The device only requires a cr

                        so use serialPort1.Write("*X01\r"); instead of WriteLine() and you're in charge, now it doesn't matter what SerialPort.NewLine contains any more.

                        mprice214 wrote:

                        get things working first

                        sure, so am I. By seeing exceptions right away. As a minimum, add Console.WriteLine(exception.ToString());. Either you don't have exceptions, then it makes no difference; or you have, and now you can see them.

                        mprice214 wrote:

                        it works

                        Yes, I forgot: Invoke without parameters provides some defaults, and since you don't really use the parameters inside DisplayText, there is no problem. However, I wouldn't do it like that.

                        mprice214 wrote:

                        Regarding the initial question

                        OK, if you are not really interested in the string, why not use a class member "double Rx;" instead of "string RxString;". Have DataReceived read the string, convert it to double, and store the value in Rx; then have DisplayText and any other interested method just read the Rx variable. :)

                        Luc Pattyn [Forum Guidelines] [Why QA sucks] [My Articles]


                        Prolific encyclopedia fixture proof-reader browser patron addict?
                        We all depend on the beast below.


                        M Offline
                        M Offline
                        mprice214
                        wrote on last edited by
                        #11

                        Luc Pattyn wrote:

                        Yes, I forgot: Invoke without parameters provides some defaults, and since you don't really use the parameters inside DisplayText, there is no problem. However, I wouldn't do it like that.

                        Do you have any other suggestions?

                        Luc Pattyn wrote:

                        OK, if you are not really interested in the string, why not use a class member "double Rx;" instead of "string RxString;". Have DataReceived read the string, convert it to double, and store the value in Rx; then have DisplayText and any other interested method just read the Rx variable.

                        Thank you for this and also the the comment on the exceptions!

                        L 1 Reply Last reply
                        0
                        • M mprice214

                          Luc Pattyn wrote:

                          Yes, I forgot: Invoke without parameters provides some defaults, and since you don't really use the parameters inside DisplayText, there is no problem. However, I wouldn't do it like that.

                          Do you have any other suggestions?

                          Luc Pattyn wrote:

                          OK, if you are not really interested in the string, why not use a class member "double Rx;" instead of "string RxString;". Have DataReceived read the string, convert it to double, and store the value in Rx; then have DisplayText and any other interested method just read the Rx variable.

                          Thank you for this and also the the comment on the exceptions!

                          L Offline
                          L Offline
                          Luc Pattyn
                          wrote on last edited by
                          #12

                          you're welcome.

                          mprice214 wrote:

                          any other suggestions?

                          yes, if you need parameters, use the Invoke() overload that takes an array of objects. :)

                          Luc Pattyn [Forum Guidelines] [Why QA sucks] [My Articles]


                          Prolific encyclopedia fixture proof-reader browser patron addict?
                          We all depend on the beast below.


                          1 Reply Last reply
                          0
                          • D dan sh

                            Luc Pattyn wrote:

                            maybe "\r\n"

                            How about using Environment.NewLine?

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

                            I use it for most things, but when communicating externally I tend to create a constant set to whatever value needs to be defined and use that. I've recently been writing an IMAP thing for work - it's in the RFC that it uses \r\n at the end of every command so that's what I use, just in case it ever ends up being ported to a system that doesn't use it!

                            Dave

                            If this helped, please vote & accept answer!

                            Binging is like googling, it just feels dirtier. (Pete O'Hanlon)
                            BTW, in software, hope and pray is not a viable strategy. (Luc Pattyn)

                            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