views:

195

answers:

3

Hello SO:

I wrote an extension in VB.NET for StringBuilder to add a AppendFormattedLine method (so I would not have to use one of the arguments for a new line character):

Imports System.Runtime.CompilerServices
Public Module sbExtension
    <Extension()> _
    Public Sub AppendFormattedLine(ByVal oStr As System.Text.StringBuilder, _
                                   ByVal format As String, _
                                   ByVal arg0 As Object)
        oStr.AppendFormat(format, arg0).Append(ControlChars.NewLine)
    End Sub
    <Extension()> _
    Public Sub AppendFormattedLine(ByVal oStr As System.Text.StringBuilder, _
                                   ByVal format As String, ByVal arg0 As Object, _
                                   ByVal arg1 As Object)
        oStr.AppendFormat(format, arg0, arg1).Append(ControlChars.NewLine)
    End Sub
    <Extension()> _
    Public Sub AppendFormattedLine(ByVal oStr As System.Text.StringBuilder, _
                                   ByVal format As String, _
                                   ByVal arg0 As Object, _
                                   ByVal arg1 As Object, _
                                   ByVal arg2 As Object)
        oStr.AppendFormat(format, arg0, arg1, arg2).Append(ControlChars.NewLine)
    End Sub
    <Extension()> _
   Public Sub AppendFormattedLine(ByVal oStr As System.Text.StringBuilder, _
                                  ByVal format As String, _
                                  ByVal ParamArray args() As Object)
        oStr.AppendFormat(format, args).Append(ControlChars.NewLine)
    End Sub
End Module
+2  A: 

Here is the ported code that I came up with:

using System;
using System.Text;

public static class ExtensionLibrary
{
    public static void AppendFormattedLine(this StringBuilder sb, string format, object arg0)
    {
        sb.AppendFormat(format, arg0).Append(Environment.NewLine);
    }
    public static void AppendFormattedLine(this StringBuilder sb, string format, object arg0, object arg1)
    {
        sb.AppendFormat(format, arg0, arg1).Append(Environment.NewLine);
    }
    public static void AppendFormattedLine(this StringBuilder sb, string format, object arg0, object arg1, object arg2)
    {
        sb.AppendFormat(format, arg0, arg1, arg2).Append(Environment.NewLine);
    }
    public static void AppendFormattedLine(this StringBuilder sb, string format, params object[] args)
    {
        sb.AppendFormat(format, args).Append(Environment.NewLine);
    }
}

Hopefully this will come in useful for someone!

improved code, thanks joel, luke & jason.

Anders
The `args` parameter of the fourth method should be declared as `params object[] args`.
LukeH
Thanks luke. May I inquire as to why this should be?
Anders
@Anders: first of all, so that it matches the c# signature. Secondly, because it provides some extra syntactic sugar- you can just pass a bunch of objects to the method as their own argument rather than having to construct an array.
Joel Coehoorn
Ok, if I am understanding you right I can do this: `sb.AppendFormattedLine("{0} {1} {2} {3} {4}", "Val1", "Val2", "Val3", "Val4", "Val5")` instead of this: `sb.AppendFormattedLine("{0} {1} {2} {3} {4}", new object[] {"Val1", "Val2", "Val3", "Val4", "Val5"})`?
Anders
You can get rid of all those extra methods and just use the one with the params object[] args. The params keyword will collect all the arguments passed into the method from that point forward and pass them to the method body in the args array. then you just pass that arg array into the format method.
Jason Miesionczek
@Jason: I was just thinking that based on Joel's response. Will update code. Thanks!
Anders
No, you shouldn't get rid of all the extra overloads. You want them to handle the different IFormatProvider overload options available to the basic string.Format() function.
Joel Coehoorn
+7  A: 

I wouldn't nest the string.Format() calls like that.

Did you know that string.Format() creates a new StringBuilder behind the scenes and calls it's AppendFormat() method? Using the first method up there as an example, this should be much more efficient:

sb.AppendFormat(format, arg0).Append(Environment.NewLine);

You should make the same change to your VB code as well.

Joel Coehoorn
excellent, thank you for this. I will update my code.
Anders
that's a nice catch.
Saif Khan
A: 

I've never used Telerik's code converter before, but here is what it thinks:

public class sbExtension
{
    [Extension()]
    public void AppendFormattedLine(System.Text.StringBuilder oStr, string format, object arg0)
    {
     oStr.AppendFormat(format, arg0).Append(ControlChars.NewLine);
    }
    [Extension()]
    public void AppendFormattedLine(System.Text.StringBuilder oStr, string format, object arg0, object arg1)
    {
     oStr.AppendFormat(format, arg0, arg1).Append(ControlChars.NewLine);
    }
    [Extension()]
    public void AppendFormattedLine(System.Text.StringBuilder oStr, string format, object arg0, object arg1, object arg2)
    {
     oStr.AppendFormat(format, arg0, arg1, arg2).Append(ControlChars.NewLine);
    }
    [Extension()]
    public void AppendFormattedLine(System.Text.StringBuilder oStr, string format, params object[] args)
    {
     oStr.AppendFormat(format, args).Append(ControlChars.NewLine);
    }
}


//=======================================================
//Service provided by Telerik (www.telerik.com)
//Conversion powered by NRefactory.
//Built and maintained by Todd Anglin and Telerik
//=======================================================
Chris Dwyer
Thanks chris, but this does not work. I actually did the exact same thing (same converter too!) at first, but the syntax for a C# extension differs from the VB.NET one slightly. Notably [Extension()] is not necessary, 'this' must be prepended to the first argument (the stringbuilder object in this case). Thanks for the input, though!
Anders
No problem. I've always wanted to have a reason to test Telerik's converter. I guess it's best to do it by hand, with the help of SO. :-)
Chris Dwyer
It works most of the time, just certain things aren't converted properly. Like you said, thats when we come to SO!
Anders