locked
Method string parameters - Trim RRS feed

  • Question

  • Just after some advice here...

    In our .NET 3.5 application, we have numerous methods that take multiple parameters that are strings, for example:

    private void ExampleMethod(string p1, string p2, string p3)
    {
    ...
    }
    

    In many of these methods, the first thing I will want to do is to Trim() one or more of these input parameters before proceeding with the logic in hand.  A common enough requirement?

    But, I want a way to do this so that there is as little code required in these methods as possible.

    One way I thought of was to have a static method that would take all my parameter(s) BY REFERENCE and trim them, something along the lines of:

    public static void ParamTrim(ref string a, ref string b)
    {
    ...
    }
    

    but of course with multiple overloads accepting 1,2,3....10 (hey, where do I stop?) of my input parameters.

    This would then allow me to trim all the input parameters with a single line of code, for example: 

    private void ExampleMethod(string p1, string p2, string p3)
    {
    ParamTrim(ref p1, ref p3);
    ...
    }
    

    However, having all these overloaded methods taking string by reference doesn't seem that great to me - surely there must be a more elegant solution...

    Any suggestions as to what that might be?

    Thanks in advance

    Griff 

    Wednesday, October 20, 2010 3:57 PM

Answers

  • public class tr
    {
      public static tr i(ref string s)
      {
        s = s.Trim();
        return new tr();
      }
      public tr m(ref string s)
      {
        s = s.Trim();
        return this;
      }
      private tr()
      {
      }
    }
    
    string s1 = " yes ", s2 = " no ";
    tr.i(ref s1).m(ref s2);
    
    string s3 = " this ", s4 = " is ", s5 = " even  ", s6 = "more", s7 = "  work";
    tr.i(ref s3).m(ref s4).m(ref s5).m(ref s6).m(ref s7);
    
    • Proposed as answer by Serguey123 Thursday, October 21, 2010 4:55 PM
    • Marked as answer by Mike Dos Zhang Wednesday, October 27, 2010 8:22 AM
    Thursday, October 21, 2010 4:36 PM

All replies

  • If the only thing that changes is the number of parameters, not the type. you can use only one signature that accepts a params parameter.

    Wednesday, October 20, 2010 4:14 PM
  • If the only thing that changes is the number of parameters, not the type. you can use only one signature that accepts a params parameter.

    http://msdn.microsoft.com/en-us/library/w5zay9db(VS.71).aspx
    Wednesday, October 20, 2010 4:14 PM
  • Thanks Me_Titus for the response regarding passing in multiple values using params.

    However, the point of passing the strings BY REFERENCE was that the method ParamTrim(ref string a, ref string b) would trim the strings and the result of this would be reflected in the calling method.

    As far as I can see (though I could be wrong), passing the parameters in using:

    private void ExampleMethod(string p1, string p2, string p3)
    {
    ParamTrim(new object[] {p1, p3});
    ....
    }
    

    would not result in their values being trimmed in the method ExampleMethod.

    Thanks

    Griff

    Wednesday, October 20, 2010 4:40 PM
  • Strings are inmutable in c# so every time you change something you create a new instance of it.

    If posible you can change it to http://msdn.microsoft.com/en-us/library/system.text.stringbuilder.aspx that is mutable.

    Regards

    • Proposed as answer by dekurver Wednesday, October 20, 2010 9:49 PM
    • Unproposed as answer by Dr Griff Thursday, October 21, 2010 11:49 AM
    Wednesday, October 20, 2010 4:45 PM
  • Hi Serguey123

    Not sure exactly what you had in mind here...

    Griff

    Wednesday, October 20, 2010 7:28 PM
  • Strings are inmutable right, is not that they work by value, is that the value can't be changed so every time you change a string you are actually creating a new one.

    StringBuilder works by reference so is kind of a mutable string so if you change the object all references will be outdated and whatever you want will work.

    Regards

    Wednesday, October 20, 2010 7:32 PM
  • Hi Serguey123

    Sorry, I understood the theory.

    What I wasn't clear on was how I could best implement it....

    My current implementation [with all those horrible overloaded methods, each taking one more string parameter (by reference) than the previous overload] does mean that in the calling method I only need a single line of code to trim every string method argument that required trimming, for example:

    private void ExampleMethod(string s1, string s2, string s3, string s4, string s5, string s6, string s7, string s8, string s9, string s10)
    {
       CommonUtility.ParamTrim(ref s1, ref s2, ref s4, ref s7, ref s9, ref s10);
       ....
       ....
    }
    

    And this would of course therefore be true of every "ExampleMethod" in my application that used this overloaded ParamTrim method.

    What I'm unclear on is how I could implement a similar technique using your StringBuilder suggestion with a single, trivial line of code in my ExampleMethod above.  If you could provide a working example then I would be most grateful.

    Thanks Serguey123

    Griff

    Thursday, October 21, 2010 7:38 AM
  • private void ExampleMethod(params StringBuilder[] list)
    {
       CommonUtility.ParamTrim(list[0], list[1], list[3], list[6], list[8], list[9]);
       ....
       ....
    }

    Regards

    Remember that you would need to change the signature of CommonUtility.ParamTrim as to take StringBuilder instead of String.

    Regards

    Thursday, October 21, 2010 12:02 PM
  • Hi

    Thanks for all your time on this.

    Unfortunately, I don't know how workable that would be, since I have many methods taking string parameters, and I can't change their signatures.

    e.g.

    private void ExampleMethod(string fname, string sname, int age, decimal weight, string ....)

    Perhaps .NET 5/6/7 will allow string params to be passed by reference:

    ParamTrim(ref params string[] s)
    {
    
    }
    

    until then, I'll keep searching.

    Thanks

    Griff

    Thursday, October 21, 2010 3:55 PM
  • public class tr
    {
      public static tr i(ref string s)
      {
        s = s.Trim();
        return new tr();
      }
      public tr m(ref string s)
      {
        s = s.Trim();
        return this;
      }
      private tr()
      {
      }
    }
    
    string s1 = " yes ", s2 = " no ";
    tr.i(ref s1).m(ref s2);
    
    string s3 = " this ", s4 = " is ", s5 = " even  ", s6 = "more", s7 = "  work";
    tr.i(ref s3).m(ref s4).m(ref s5).m(ref s6).m(ref s7);
    
    • Proposed as answer by Serguey123 Thursday, October 21, 2010 4:55 PM
    • Marked as answer by Mike Dos Zhang Wednesday, October 27, 2010 8:22 AM
    Thursday, October 21, 2010 4:36 PM
  • Hi Dr Griff,

     

    Welcome to MSDN Forums!

     

    If your question has been solved, please don’t forget mark/vote the replies which helped you, this will encourage the other community members to join in discussion and help each one.

     

    If there’s anything unclear, please feel free to let us know.

     

    Have a nice weekend!

    Mike

    *****************************************************

    [All-In-One Code Framework]

    Sample world! You will get more from this world!

    Welcome to the new world!

    Sunday, October 24, 2010 2:08 PM
  • Loius - that's great.  Thanks

    Thursday, November 4, 2010 9:09 AM