default parameters in methods

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • HaLo2FrEeEk
    Contributor
    • Feb 2007
    • 404

    default parameters in methods

    I need to have a method with parameters, but one of the parameters needs to have a default value, just in case it's not passed to the method when it's called. Here's my example:

    Code:
    private void error(string msg, string title = "Error!")
    {
        MessageBox.Show(msg, title, MessageBoxButtons.OK, MessageBoxIcon.Error);
    }
    This throws a Compiler Error CS0241. The online help documentation shows an example of a way to have a method with a default parameter by using overloading:

    http://msdn.microsoft. com/en-us/library/294000kk(VS.90) .aspx

    But I can't figure out what it's telling me to do. Basically I just want the string title to have a default value of Error! if the value doesn't get passed to the method, so I could call it like this:

    error("Some error message");

    And that would generate a messagebox with the default title of "Error!" and "Some error message" as the contents. Alternatively, I could call it like this:

    error("Some error message", "Some title");

    And I would get the same messagebox but the title would now be "Some title".

    Is there a way I can do this?
  • ThatThatGuy
    Recognized Expert Contributor
    • Jul 2009
    • 453

    #2
    Originally posted by HaLo2FrEeEk
    I need to have a method with parameters, but one of the parameters needs to have a default value, just in case it's not passed to the method when it's called. Here's my example:

    Code:
    private void error(string msg, string title = "Error!")
    {
        MessageBox.Show(msg, title, MessageBoxButtons.OK, MessageBoxIcon.Error);
    }
    This throws a Compiler Error CS0241. The online help documentation shows an example of a way to have a method with a default parameter by using overloading:



    But I can't figure out what it's telling me to do. Basically I just want the string title to have a default value of Error! if the value doesn't get passed to the method, so I could call it like this:

    error("Some error message");

    And that would generate a messagebox with the default title of "Error!" and "Some error message" as the contents. Alternatively, I could call it like this:

    error("Some error message", "Some title");

    And I would get the same messagebox but the title would now be "Some title".

    Is there a way I can do this?
    You should compile this code in .net framework 4.0

    default parameters are not supported, prior to framework 4.0

    Comment

    • HaLo2FrEeEk
      Contributor
      • Feb 2007
      • 404

      #3
      I'm trying to maintain compatibility with older systems that might not have the new framework installed. I found a way around it though. All I had to do was change my method to this:

      Code:
      private void error(string msg, string title)
              {
                  if (string.IsNullOrEmpty(title))
                  {
                      title = "Error!";
                  }
                  MessageBox.Show(msg, title, MessageBoxButtons.OK, MessageBoxIcon.Error);
              }
      And call it with this:

      error("some message", null);

      That worked perfectly.

      Comment

      • ThatThatGuy
        Recognized Expert Contributor
        • Jul 2009
        • 453

        #4
        Originally posted by HaLo2FrEeEk
        I'm trying to maintain compatibility with older systems that might not have the new framework installed. I found a way around it though. All I had to do was change my method to this:

        Code:
        private void error(string msg, string title)
                {
                    if (string.IsNullOrEmpty(title))
                    {
                        title = "Error!";
                    }
                    MessageBox.Show(msg, title, MessageBoxButtons.OK, MessageBoxIcon.Error);
                }
        And call it with this:

        error("some message", null);

        That worked perfectly.
        So you could have anyways done it.... why did you tried the new one

        Comment

        • HaLo2FrEeEk
          Contributor
          • Feb 2007
          • 404

          #5
          Because I didn't think about that until after I made the post, and by the time I did it was too late to delete it.

          Comment

          • GaryTexmo
            Recognized Expert Top Contributor
            • Jul 2009
            • 1501

            #6
            I didn't know there was default params in .NET 4.0... cool!

            Anyway, the proper way to do this previously was with overloads... for example:

            Code:
            public void MyMethod(int id, string fname, string lname) { ... }
            
            public void MyMethod(int id, string fname)
            {
              MyMethod(id, fname, "NOT ENTERED");
            }
            
            public void MyMethod(int id)
            {
              MyMethod(id, "NOT ENTERED", "NOT ENTERED");
            }
            
            // etc...
            }

            Comment

            • tlhintoq
              Recognized Expert Specialist
              • Mar 2008
              • 3532

              #7
              For earlier framework you have to send all parameters. There are no options. If you want an option to not send it then make an override without the second param that adds your default then calls the 2 param version

              Void test(string one, string two)
              {
              }

              void test(string one)
              {
              Test(one, "default");
              }

              Comment

              • HaLo2FrEeEk
                Contributor
                • Feb 2007
                • 404

                #8
                My way works too, I send a null value and my method checks if the value is null and sets it to a default.

                Comment

                Working...