Click here to Skip to main content
15,893,644 members
Articles / Programming Languages / Visual Basic

Money DataType

Rate me:
Please Sign up or sign in to vote.
4.56/5 (12 votes)
4 Sep 200510 min read 115.8K   1.1K   49  
An article on creating a Money datatype with localized formatting and plugin conversion support.
''' <summary>
''' An convertor used to convert money instances from one currency to another.
''' </summary>
''' <remarks></remarks>
Public Class WebserviceXConvertor
    Implements Hybrid.IMoneyConversionProvider

    ''' <summary>
    ''' Converts a money instance to another instance of the specified region.
    ''' </summary>
    ''' <param name="moneyFrom">Money instance that needs to be converted.</param>
    ''' <param name="toRegion">Region that the money needs to be converted to.</param>
    ''' <returns>New money instance containing the toRegion and the converted value.</returns>
    ''' <remarks>Uses the following WebService to perform the conversion.  http://www.webservicex.net/CurrencyConvertor.asmx?WSDL</remarks>
    ''' <exception cref="ConversionFailedException">Thrown whenever the Web Service fails to return a converted value.  See the inner exception for full details of cause.</exception>
    Public Function Convert(ByVal moneyFrom As Money, ByVal toRegion As System.Globalization.RegionInfo) As Money Implements IMoneyConversionProvider.Convert
        Dim x As New Convertor.CurrencyConvertor
        Try
            Dim e1 As Convertor.Currency
            Dim e2 As Convertor.Currency

            If [Enum].IsDefined(GetType(Convertor.Currency), moneyFrom.Region.ISOCurrencySymbol) Then
                e1 = [Enum].Parse(GetType(Convertor.Currency), moneyFrom.Region.ISOCurrencySymbol)
            End If
            If [Enum].IsDefined(GetType(Convertor.Currency), toRegion.ISOCurrencySymbol) Then
                e2 = [Enum].Parse(GetType(Convertor.Currency), toRegion.ISOCurrencySymbol)
            End If

            Dim rate As Double = x.ConversionRate(e1, e2)
            Return New Hybrid.Money(moneyFrom.Value * rate, toRegion)
        Catch ex As Exception
            Throw New ConversionFailedException(ex)
        End Try
    End Function
End Class


''' <summary>
''' Represents errors that occur during conversion of Money instances using a WebService.
''' </summary>
''' <remarks></remarks>
Public Class ConversionFailedException
    Inherits ApplicationException

    Public Sub New(ByVal innerException As Exception)
        MyBase.New("Failed to convert money value.", innerException)
    End Sub
End Class

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here


Written By
Software Developer (Senior)
Australia Australia
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions