IsNumeric, Left, Right and FormatCurrency Extensions for Windows Phone 7

Posted by Blake on 3/21/2011
)

WP7 now has VB templates if you have the full version of Visual Studio. Even though you can use the Visual Basic syntax to code against WP7’s version of Silverlight, you lose a lot of the functions that have been with Visual Basic since it’s inception (Left, Right, FormatNumber, IsNumeric, etc). Obviously, there are other ways to perform these tasks in the .Net Framework and some would argue they should be done those ways. I’m not going to go into that debate but I will say that I am a fan of Extension methods in .Net so implementing these VB commands as extensions makes sense for me. When coupled with a modern IDE (e.g. Visual Studio) the extension methods give us a nice way easily find the common code that we’re looking for off of the type that the extension is made for. Anyway, here are a few extension methods that I find handy. I’m just getting into WP7 so as I add more in my library I’ll update this post (I’m basically moving things from my own .Net Ent library into my Silverlight library as I need them).

Imports System.Runtime.CompilerServices
Imports System.Text
 
Namespace Extensions
 
    ''' <summary>
    ''' Extension methods for the String type.
    ''' </summary>
    ''' <remarks>
    ''' These string extensions are targeted against the WP7 version of Silverlight.
    ''' </remarks>
    Public Module StringExtensions
 
        '*********************************************************************************************************************
        '
        '            Module:  StringExtensions
        '      Initial Date:  01/12/2008
        '      Last Updated:  03/21/2011
        '     Programmer(s):  Blake Pell
        '
        '*********************************************************************************************************************
 
        ''' <summary>
        ''' Determines whether a string is a numeric value.  This implementation uses Decimal.TryParse to produce it's value.
        ''' </summary>
        ''' <param name="str"></param>
        ''' <returns></returns>
        ''' <remarks></remarks>
        <extension()> _
        Public Function [IsNumeric](str As String) As Boolean
            Dim result As Decimal = 0
            Return Decimal.TryParse(str, result)
        End Function
 
        ''' <summary>
        ''' Returns the specified numbers of characters from the left side of the string.
        ''' </summary>
        ''' <param name="str"></param>
        ''' <param name="length"></param>
        ''' <returns></returns>
        ''' <remarks></remarks>
        <extension()> _
        Public Function [Left](ByVal str As String, ByVal length As Integer) As String
            Return str.Substring(0, length)
        End Function
 
        ''' <summary>
        ''' Returns the specified number of characters from the right side of the string.
        ''' </summary>
        ''' <param name="str"></param>
        ''' <param name="length"></param>
        ''' <returns></returns>
        ''' <remarks></remarks>
        <extension()> _
        Public Function [Right](ByVal str As String, ByVal length As Integer) As String
            Return str.Substring(str.Length - length, length)
        End Function
 
        ''' <summary>
        ''' Formats the string as a currency value.
        ''' </summary>
        ''' <param name="str"></param>
        ''' <returns></returns>
        ''' <remarks></remarks>
        <extension()> _
        Public Function FormatCurrency(str As String) As String
            If IsNumeric(str) = False Then
                Return str
            End If
 
            Dim dec As Decimal = CDec(str)
 
            Return String.Format("{0:C}", dec)
        End Function
 
    End Module
 
End Namespace