This article is based on something Jeff Atwood did. It reminded me of something I missed an something that could save me some time. I need a simple left and right method on a string variable.

So I went about and did it ;-).

First I googled around a bit to find out how I should do it in VB.Net (plenty of c# examples can be found). And I found this blogpost by Chris Rock which helped a lot.

So I started of by creating a consoleapplication.

Next up I create a testclass. Called testExtension (I don’t want to be over creative, it’s Monday remember). Like most people I use nUnit to do the dirty work and R# to make it more pleasant.

the first test I wrote looks like this

<Test()> _
        Public Sub Test_Left_To_See_If_It_Returns_The_Correct_Result()
            Dim s As String
            s = "areyousureyouwantotestthis?"
            Assert.AreEqual("this?", s.left(5))
        End Sub```
of course that won’t even compile because it can’t find method Left in there.

So let’s give it what it wants.

```vbnet
Imports System.Runtime.CompilerServices

Module Module1
    Public Sub main()

    End Sub

    <Extension()> _
    Public Function Left(ByVal Input As String, ByVal Length As Integer) As String

    End Function


End Module

Now it compiles and now the test goes red (:‘()

First thing to do is create some more tests.

''' <summary>
        ''' A Test
        ''' </summary>
        ''' <remarks></remarks>
        <Test()> _
        Public Sub Test_Left_When_Length_Is_0()
            Dim s As String
            s = "areyousureyouwantotestthis?"
            Assert.AreEqual("", s.Left(0))
        End Sub

        ''' <summary>
        ''' A Test
        ''' </summary>
        ''' <remarks></remarks>
        <Test()> _
        Public Sub Test_Left_When_String_Is_Empty()
            Dim s As String
            s = ""
            Assert.AreEqual("", s.Left(5))
        End Sub

        ''' <summary>
        ''' A Test
        ''' </summary>
        ''' <remarks></remarks>
        <Test()> _
        Public Sub Test_Left_When_String_is_empty_and_length_is_0()
            Dim s As String
            s = ""
            Assert.AreEqual("", s.Left(0))
        End Sub

        ''' <summary>
        ''' A Test
        ''' </summary>
        ''' <remarks></remarks>
        <Test()> _
        Public Sub Test_Left_When_length_is_smaller_then_the_length_of_the_string()
            Dim s As String
            s = "his?"
            Assert.AreEqual("his?", s.Left(7))
        End Sub```
I think I now coverd most things. Except one, but who needs nullreferenceexceptions anyway.

Lets run it and again everything is red.

So now lets get some code in there.

```vbnet
<Extension()> _
    Public Function Left(ByVal Input As String, ByVal Length As Integer) As String
        Dim ReturnString As String = ""
        If (Length = 0 Or Input.Length = 0) Then
            ReturnString = ""
        ElseIf (Input.Length <= Length) Then
            ReturnString = Input
        Else
            ReturnString = Input.Substring(0, Length)
        End If
        Return ReturnString
    End Function

Look at the parameters. The first one is will actually be used for the type and for the variable you are using it on. When calling the method you never see this parameter again. The second is the only parameter that will be visible to the user.

And lets run the test again.

Nearly all passed this time, except the first one. But that was my fault.

NUnit.Framework.AssertionException: String lengths are both 5. Strings differ at index 0.

Expected: “this?”

But was: “areyo”

———–^

I confused left with right So thats what I get. I have to change the unittest.

When I change it to this it passes.

vbnet ''' <summary> ''' A Test ''' </summary> ''' <remarks></remarks> <Test()> _ Public Sub Test_Left_To_See_If_It_Returns_The_Correct_Result() Dim s As String s = "areyousureyouwantotestthis?" Assert.AreEqual("areyo", s.Left(5)) End Sub So now our string has a left method.

Let’s add a right method shall we.

Here are the tests.

```vbnet “’ <summary> “’ A Test “’ </summary> “’ <remarks></remarks> <Test()> _ Public Sub Test_Right_To_See_If_It_Returns_The_Correct_Result() Dim s As String s = “areyousureyouwantotestthis?” Assert.AreEqual(“this?”, s.Right(5)) End Sub

    ''' &lt;summary&gt;
    ''' A Test
    ''' &lt;/summary&gt;
    ''' &lt;remarks&gt;&lt;/remarks&gt;
    &lt;Test()&gt; _
    Public Sub Test_Right_When_Length_Is_0()
        Dim s As String
        s = "areyousureyouwantotestthis?"
        Assert.AreEqual("", s.Right(0))
    End Sub

    ''' &lt;summary&gt;
    ''' A Test
    ''' &lt;/summary&gt;
    ''' &lt;remarks&gt;&lt;/remarks&gt;
    &lt;Test()&gt; _
    Public Sub Test_Right_When_String_Is_Empty()
        Dim s As String
        s = ""
        Assert.AreEqual("", s.Right(5))
    End Sub

    ''' &lt;summary&gt;
    ''' A Test
    ''' &lt;/summary&gt;
    ''' &lt;remarks&gt;&lt;/remarks&gt;
    &lt;Test()&gt; _
    Public Sub Test_Right_When_String_is_empty_and_length_is_0()
        Dim s As String
        s = ""
        Assert.AreEqual("", s.Right(0))
    End Sub

    ''' &lt;summary&gt;
    ''' A Test
    ''' &lt;/summary&gt;
    ''' &lt;remarks&gt;&lt;/remarks&gt;
    &lt;Test()&gt; _
    Public Sub Test_Right_When_length_is_smaller_then_the_length_of_the_string()
        Dim s As String
        s = "his?"
        Assert.AreEqual("his?", s.Right(7))
    End Sub```

create the empty method and run it. See how they fail.

and then create our logic.

vbnet &lt;Extension()&gt; _ Public Function Right(ByVal Input As String, ByVal Length As Integer) As String Dim ReturnString As String = "" If (Length = 0 Or Input.Length = 0) Then ReturnString = "" ElseIf (Input.Length &lt;= Length) Then ReturnString = Input Else ReturnString = Input.Substring(Input.Length) End If Return ReturnString End Function Which was wrong again, silly me forgot to substract the length form input.length when doing the substring. Luckily I had the unittests to tell me that.

NUnit.Framework.AssertionException: Expected string length 5 but was 0. Strings differ at index 0.

Expected: “this?”

But was: string.Empty

———–^

So here is the new and all improved version.

vbnet &lt;Extension()&gt; _ Public Function Right(ByVal Input As String, ByVal Length As Integer) As String Dim ReturnString As String = "" If (Length = 0 Or Input.Length = 0) Then ReturnString = "" ElseIf (Input.Length &lt;= Length) Then ReturnString = Input Else ReturnString = Input.Substring(Input.Length - Length) End If Return ReturnString End Function And that’s it. Have fun.

Just in case you don’t believe me.

Need help with VB.Net? Come and ask a question in our VB.Net Forum