question

Hekzdaddy-3952 avatar image
0 Votes"
Hekzdaddy-3952 asked cheong00 commented

Converting from lbl.Text to currecy


In the Case statement below, I am getting an error when trying to convert the results on lblTotal.Text to Currency. Any tips and suggestions would be helpful.

Case strCalculus
lblSubtotal.Text -= decCalculus
lblTax.Text -= decCalculus decTAX_RATE
lblTotal.Text = lblSubtotal.Text + lblTax.Text + lblShipping.Text
lblShipping.Text -= dblShippingCharge
Case strHistoryScotland
lblSubtotal.Text -= decHistoryScotland
lblTax.Text -= decHistoryScotland
decTAX_RATE
lblTotal.Text = lblSubtotal.Text + lblTax.Text + lblShipping.Text

             lblShipping.Text -= dblShippingCharge

     End Select
     lblTotal.Text = CDbl(lblTotal.Text).ToString("c")
dotnet-visual-basic
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

cheong00 avatar image
0 Votes"
cheong00 answered cheong00 commented

Most probably the following line caused the value of lblTotal.Text not a proper number.

 lblTotal.Text = lblSubtotal.Text + lblTax.Text + lblShipping.Text

Try use the following instead:

 lblTotal.Text = CStr(CDbl(lblSubtotal.Text) + CDbl(lblTax.Text) + CDbl(lblShipping.Text))
· 2
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

Did not work, got a new error

System.InvalidCastException: 'Conversion from string "False" to type 'Double' is not valid.'

0 Votes 0 ·

Then you should check why any of the lblSubtotal.Text/lblTax.Text/lblShipping.Text can have "False" as value when the logic in your code indicates they should be numeric.

0 Votes 0 ·
karenpayneoregon avatar image
0 Votes"
karenpayneoregon answered karenpayneoregon edited

This will work

 Dim value As Double = 0
 If Double.TryParse(lblTotal.Text, value) Then
     lblTotal.Text = $"{value.ToString("c")}"
 Else
     lblTotal.Text = "Invalid value"
 End If

Or create a currency label

 Public Class CurrencyLabel
     Inherits Label
     Public ReadOnly Property AsCurrent() As String
         Get
             Dim value As Double = 0
             If Double.TryParse(Text, value) Then
                 Text = $"{value.ToString("c")}"
             End If
    
             Return Text
    
         End Get
     End Property
 End Class


5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.