views:

91

answers:

1

I have this function in my generator.

    Private Sub AddBoundedValue(ByVal boundedValue As Object, ByVal type As CodeTypeDeclaration, ByVal numericType As Type, name As String)

        If boundedValue IsNot Nothing Then

            Dim constant As New CodeMemberField(numericType, name)
            constant.Attributes = MemberAttributes.Const Or MemberAttributes.Public
            constant.InitExpression = New CodePrimitiveExpression(Convert.ChangeType(boundedValue, numericType))
            type.Members.Add(constant)

        End If

    End Sub

If a developer passes a decimal in for the "boundedValue" parameter and the decimal type for the "numericType" parameter the following code gets generated.

Public Const DollarAmountMaximumValue As Decimal = 100000

Despite the data type being passed into the constructor of the CodePrimitiveExpression object being a decimal, the code generated is an integer that gets implicitly converted and stored in a decimal variable. Is there any way to get it to generate with the "D" after the number as in:

Public Const DollarAmountMaximumValue As Decimal = 100000D

Thanks.

A: 

Well, I'm not happy about this solution but unless someone has a better one I'll have to go with it.

Private Sub AddBoundedValue(ByVal boundedValue As Object, ByVal type As CodeTypeDeclaration, ByVal numericType As Type, name As String)

    If boundedValue IsNot Nothing Then

        Dim constant As New CodeMemberField(numericType, name)
        constant.Attributes = MemberAttributes.Const Or MemberAttributes.Public
        If numericType Is GetType(Decimal) AndAlso [I detect if the language is VB.NET here] Then
            constant.InitExpression = New CodeSnippetExpression(boundedValue.ToString & "D")
        Else
            constant.InitExpression = New CodePrimitiveExpression(Convert.ChangeType(boundedValue, numericType))
        End If
        type.Members.Add(constant)

    End If

End Sub
adam0101