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. Correct Syntax; No Result?

Correct Syntax; No Result?

Scheduled Pinned Locked Moved C#
helpquestion
8 Posts 4 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.
  • Z Offline
    Z Offline
    Zachery Hysong
    wrote on last edited by
    #1

    Hey guys, I have a strange problem. I am trying to write a function that looks at a TextBox that, if it is equal to a certain value, changes the value of a string to something else. Here is my code: I define the string in Main:

    string strDoorsOption1 = "test";

    The Method is defined as:

    public void optionCheck(TextBox txt, string str)
    {
    if (txt.Text == "0")
    {
    str = "+++";
    }
    else
    {
    str = "---";
    }
    }

    I call the Method like this:

    private void chkDoorsSafetyLaminated_CheckedChanged(object sender, EventArgs e)
    {
    checkBoxClick3(chkDoorsSafetyLaminated, txtDoorsSafety1);

    optionCheck(txtDoorsSafety1, strDoorsOption1);
    

    }

    checkBoxClick3 is another Method that works as intended to change the value of a TextBox. For reference, it is defined as follows:

    public void checkBoxClick3(CheckBox check, TextBox txt)
    {
    /* Determine the CheckState of the Checkbox */
    if (check.CheckState == CheckState.Checked)
    {
    /* If the CheckState is Checked, then set the value to 1 */
    txt.Text = "1";
    }
    else
    {
    /* If the CheckState is Unchecked or Inditerminate, then set the value to 0 */
    txt.Text = "0";
    }
    }

    This works...:

    if (txtDoorsSafety1.Text == "0")
    {
    strDoorsOption1 = "+++";
    }
    else
    {
    strDoorsOption1 = "---";
    }

    But when I call optionCheck(txtDoorsSafety1, strDoorsOption1); it acts as if the method is empty. It completely compiles with no errors or warnings, it simply does nothing. Any help would be appreciated.

    L OriginalGriffO Richard DeemingR 3 Replies Last reply
    0
    • Z Zachery Hysong

      Hey guys, I have a strange problem. I am trying to write a function that looks at a TextBox that, if it is equal to a certain value, changes the value of a string to something else. Here is my code: I define the string in Main:

      string strDoorsOption1 = "test";

      The Method is defined as:

      public void optionCheck(TextBox txt, string str)
      {
      if (txt.Text == "0")
      {
      str = "+++";
      }
      else
      {
      str = "---";
      }
      }

      I call the Method like this:

      private void chkDoorsSafetyLaminated_CheckedChanged(object sender, EventArgs e)
      {
      checkBoxClick3(chkDoorsSafetyLaminated, txtDoorsSafety1);

      optionCheck(txtDoorsSafety1, strDoorsOption1);
      

      }

      checkBoxClick3 is another Method that works as intended to change the value of a TextBox. For reference, it is defined as follows:

      public void checkBoxClick3(CheckBox check, TextBox txt)
      {
      /* Determine the CheckState of the Checkbox */
      if (check.CheckState == CheckState.Checked)
      {
      /* If the CheckState is Checked, then set the value to 1 */
      txt.Text = "1";
      }
      else
      {
      /* If the CheckState is Unchecked or Inditerminate, then set the value to 0 */
      txt.Text = "0";
      }
      }

      This works...:

      if (txtDoorsSafety1.Text == "0")
      {
      strDoorsOption1 = "+++";
      }
      else
      {
      strDoorsOption1 = "---";
      }

      But when I call optionCheck(txtDoorsSafety1, strDoorsOption1); it acts as if the method is empty. It completely compiles with no errors or warnings, it simply does nothing. Any help would be appreciated.

      L Offline
      L Offline
      Lost User
      wrote on last edited by
      #2

      Aight, let's call it, using your constant and the textbox.

      public void optionCheck(TextBox txt, string str)
      {
      if (txt.Text == "0")
      {
      str = "+++";
      }
      else
      {
      str = "---";
      }
      }

      optionCheck(textBox1, "test");
      if (textBox1.Text == "0")
      {
      //something
      }
      else
      {
      str = "...";
      }

      Means that "str" gets set to "---", and then goes out of scope. Did you mean to set anything else, like a textbox or a checkbox?

      Bastard Programmer from Hell :suss: If you can't read my code, try converting it here[^]

      Z 1 Reply Last reply
      0
      • Z Zachery Hysong

        Hey guys, I have a strange problem. I am trying to write a function that looks at a TextBox that, if it is equal to a certain value, changes the value of a string to something else. Here is my code: I define the string in Main:

        string strDoorsOption1 = "test";

        The Method is defined as:

        public void optionCheck(TextBox txt, string str)
        {
        if (txt.Text == "0")
        {
        str = "+++";
        }
        else
        {
        str = "---";
        }
        }

        I call the Method like this:

        private void chkDoorsSafetyLaminated_CheckedChanged(object sender, EventArgs e)
        {
        checkBoxClick3(chkDoorsSafetyLaminated, txtDoorsSafety1);

        optionCheck(txtDoorsSafety1, strDoorsOption1);
        

        }

        checkBoxClick3 is another Method that works as intended to change the value of a TextBox. For reference, it is defined as follows:

        public void checkBoxClick3(CheckBox check, TextBox txt)
        {
        /* Determine the CheckState of the Checkbox */
        if (check.CheckState == CheckState.Checked)
        {
        /* If the CheckState is Checked, then set the value to 1 */
        txt.Text = "1";
        }
        else
        {
        /* If the CheckState is Unchecked or Inditerminate, then set the value to 0 */
        txt.Text = "0";
        }
        }

        This works...:

        if (txtDoorsSafety1.Text == "0")
        {
        strDoorsOption1 = "+++";
        }
        else
        {
        strDoorsOption1 = "---";
        }

        But when I call optionCheck(txtDoorsSafety1, strDoorsOption1); it acts as if the method is empty. It completely compiles with no errors or warnings, it simply does nothing. Any help would be appreciated.

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

        Look at the code:

        public void optionCheck(TextBox txt, string str)

        That means that both txt and str are Value parameters - the default - which means that the value of the variable is copied and the copy passed to the method when it is called - so any changes to the value of the variable affect the copy, and not the original. When the method ends, the modified value is thrown away. This is exactly the normal behaviour - and it is exactly what you want to happen! Take an example, and think about what would happen if what you expected to happen did:

        public void Change(int i)
        {
        i = i + 2;
        }

        Now, that's fine when you call it like this:

        int iOriginal = 6;
        Change(iOriginal);
        Console.WriteLine(iOriginal);

        You will be expecting to get "8" printed. But...what happens if you do this:

        Change(6);
        Console.WriteLine(6);

        The last thing you want is to get "8" printed! :laugh: But that is exactly what "should" happen - you passed in a value, the method changed it and tit affected the outside world. The method doesn't know - and can't check - that you are passing in a value that can be variable! You can do what you want, but you have to tell teh system that that is exactly what you want to happen:

        public void optionCheck(TextBox txt, ref string str)
        {
        if (txt.Text == "0")
        {
        str = "+++";
        }
        else
        {
        str = "---";
        }
        }

        And call it like this:

        optionCheck(txtDoorsSafety1, ref strDoorsOption1);

        But then you can't call it with a string constant because will get a compilation error, and the system now knows you want to change a constant, and you can't do that! Make sense?

        Those who fail to learn history are doomed to repeat it. --- George Santayana (December 16, 1863 – September 26, 1952) Those who fail to clear history are doomed to explain it. --- OriginalGriff (February 24, 1959 – ∞)

        "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

        Z 1 Reply Last reply
        0
        • L Lost User

          Aight, let's call it, using your constant and the textbox.

          public void optionCheck(TextBox txt, string str)
          {
          if (txt.Text == "0")
          {
          str = "+++";
          }
          else
          {
          str = "---";
          }
          }

          optionCheck(textBox1, "test");
          if (textBox1.Text == "0")
          {
          //something
          }
          else
          {
          str = "...";
          }

          Means that "str" gets set to "---", and then goes out of scope. Did you mean to set anything else, like a textbox or a checkbox?

          Bastard Programmer from Hell :suss: If you can't read my code, try converting it here[^]

          Z Offline
          Z Offline
          Zachery Hysong
          wrote on last edited by
          #4

          Actually, I was able to finally figure this out, I was missing a `ref` in the Method Definition:

          public void optionCheck(TextBox txt, ref string str)

          Then when I call it:

          optionCheck(txtDoorsSafety1, ref strDoorsOption1)

          That works... Though I have to say... the concept of having to write `ref` twice seems counterintuitive to me.

          L 1 Reply Last reply
          0
          • OriginalGriffO OriginalGriff

            Look at the code:

            public void optionCheck(TextBox txt, string str)

            That means that both txt and str are Value parameters - the default - which means that the value of the variable is copied and the copy passed to the method when it is called - so any changes to the value of the variable affect the copy, and not the original. When the method ends, the modified value is thrown away. This is exactly the normal behaviour - and it is exactly what you want to happen! Take an example, and think about what would happen if what you expected to happen did:

            public void Change(int i)
            {
            i = i + 2;
            }

            Now, that's fine when you call it like this:

            int iOriginal = 6;
            Change(iOriginal);
            Console.WriteLine(iOriginal);

            You will be expecting to get "8" printed. But...what happens if you do this:

            Change(6);
            Console.WriteLine(6);

            The last thing you want is to get "8" printed! :laugh: But that is exactly what "should" happen - you passed in a value, the method changed it and tit affected the outside world. The method doesn't know - and can't check - that you are passing in a value that can be variable! You can do what you want, but you have to tell teh system that that is exactly what you want to happen:

            public void optionCheck(TextBox txt, ref string str)
            {
            if (txt.Text == "0")
            {
            str = "+++";
            }
            else
            {
            str = "---";
            }
            }

            And call it like this:

            optionCheck(txtDoorsSafety1, ref strDoorsOption1);

            But then you can't call it with a string constant because will get a compilation error, and the system now knows you want to change a constant, and you can't do that! Make sense?

            Those who fail to learn history are doomed to repeat it. --- George Santayana (December 16, 1863 – September 26, 1952) Those who fail to clear history are doomed to explain it. --- OriginalGriff (February 24, 1959 – ∞)

            Z Offline
            Z Offline
            Zachery Hysong
            wrote on last edited by
            #5

            Yes, and this is one of the best explanations ever. Thank you.

            OriginalGriffO 1 Reply Last reply
            0
            • Z Zachery Hysong

              Hey guys, I have a strange problem. I am trying to write a function that looks at a TextBox that, if it is equal to a certain value, changes the value of a string to something else. Here is my code: I define the string in Main:

              string strDoorsOption1 = "test";

              The Method is defined as:

              public void optionCheck(TextBox txt, string str)
              {
              if (txt.Text == "0")
              {
              str = "+++";
              }
              else
              {
              str = "---";
              }
              }

              I call the Method like this:

              private void chkDoorsSafetyLaminated_CheckedChanged(object sender, EventArgs e)
              {
              checkBoxClick3(chkDoorsSafetyLaminated, txtDoorsSafety1);

              optionCheck(txtDoorsSafety1, strDoorsOption1);
              

              }

              checkBoxClick3 is another Method that works as intended to change the value of a TextBox. For reference, it is defined as follows:

              public void checkBoxClick3(CheckBox check, TextBox txt)
              {
              /* Determine the CheckState of the Checkbox */
              if (check.CheckState == CheckState.Checked)
              {
              /* If the CheckState is Checked, then set the value to 1 */
              txt.Text = "1";
              }
              else
              {
              /* If the CheckState is Unchecked or Inditerminate, then set the value to 0 */
              txt.Text = "0";
              }
              }

              This works...:

              if (txtDoorsSafety1.Text == "0")
              {
              strDoorsOption1 = "+++";
              }
              else
              {
              strDoorsOption1 = "---";
              }

              But when I call optionCheck(txtDoorsSafety1, strDoorsOption1); it acts as if the method is empty. It completely compiles with no errors or warnings, it simply does nothing. Any help would be appreciated.

              Richard DeemingR Offline
              Richard DeemingR Offline
              Richard Deeming
              wrote on last edited by
              #6

              You're passing the parameter by value, which means that any changes you make to the parameter within the method will be discarded when the method returns. I suspect you meant to pass it by reference, so that the changes would be copied back. Passing Parameters (C# Programming Guide)[^] In this case, since you're not using the value of the parameter within the method, and you're always assigning a value to it before the method returns, you should declare it as an out parameter.

              public void optionCheck(TextBox txt, out string str)
              {
              if (txt.Text == "0")
              {
              str = "+++";
              }
              else
              {
              str = "---";
              }
              }

              optionCheck(txtDoorsSafety1, out strDoorsOption1);


              "These people looked deep within my soul and assigned me a number based on the order in which I joined." - Homer

              "These people looked deep within my soul and assigned me a number based on the order in which I joined" - Homer

              1 Reply Last reply
              0
              • Z Zachery Hysong

                Yes, and this is one of the best explanations ever. Thank you.

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

                You're welcome!

                Those who fail to learn history are doomed to repeat it. --- George Santayana (December 16, 1863 – September 26, 1952) Those who fail to clear history are doomed to explain it. --- OriginalGriff (February 24, 1959 – ∞)

                "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
                • Z Zachery Hysong

                  Actually, I was able to finally figure this out, I was missing a `ref` in the Method Definition:

                  public void optionCheck(TextBox txt, ref string str)

                  Then when I call it:

                  optionCheck(txtDoorsSafety1, ref strDoorsOption1)

                  That works... Though I have to say... the concept of having to write `ref` twice seems counterintuitive to me.

                  L Offline
                  L Offline
                  Lost User
                  wrote on last edited by
                  #8

                  Zachery Hysong wrote:

                  the concept of having to write `ref` twice seems counterintuitive to me.

                  The first time (in the method declaration) you use it to explain to the compiler that you're passing the string by reference, not as a value. The second could be omitted if the language would allow it; it doesn't because it helps to determine what is being passed. If you read the call to the method, then without the additional "ref" keyword it'd be impossible to say whether it is passed as a value or a reference without looking up the method-signature. It also prevents mistakes; you can never pass in a value by "mistake" :)

                  Bastard Programmer from Hell :suss: If you can't read my code, try converting it here[^]

                  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