I have an error problem in the text label. Is there another solution?
Thanks
Private Sub CalculateGrandTotal()
Dim tot As Double = 0
'error this below code
Dim cash As Double = Double.Parse(lblDisTotal.Text)
For Each item As DataGridViewRow In grid.Rows
tot = Double.Parse(item.Cells(5).Value.ToString())
Next item
lblGrandTotal.Text = (tot * (1 - cash / 100)).ToString("N2")
End Sub
Private Sub BtnRefresh_Click(sender As Object, e As EventArgs) Handles BtnRefresh.Click
lblDisTotal.Text = ""
End Sub
CodePudding user response:
when the button event is refreshed then lblDisTotal does not become blank but becomes "0" then the problem is solved
Private Sub CalculateGrandTotal()
Dim tot As Double = 0
'error this below code
Dim cash As Double = Double.Parse(lblDisTotal.Text)
For Each item As DataGridViewRow In grid.Rows
tot = Double.Parse(item.Cells(5).Value.ToString())
Next item
lblGrandTotal.Text = (tot * (1 - cash / 100)).ToString("N2")
End Sub
Private Sub BtnRefresh_Click(sender As Object, e As EventArgs) Handles BtnRefresh.Click
lblDisTotal.Text = "0"
End Sub
CodePudding user response:
The appropriate solution would be Double.TryParse to handle someone typing "hello world" into your text box if it is not sanitized.
As a rule, you should avoid .parse whereever possible and opt for try parse unless you can guarantee with absolute certainty that your value CAN be parsed into the type you want. Otherwise, utilize tryParse to prevent exceptions that can be prevented.
Private Sub CalculateGrandTotal()
Dim tot As Double = 0
'error this below code
Dim cash As Double
'tryParse returns a boolean indicating successful parsing. You can check for that, however, if it fails, it assigns 0 to your passed in double
Double.TryParse(lblDisTotal.Text, cash)
For Each item As DataGridViewRow In grid.Rows
dim val as double
Double.TryParse(item.Cells(5).Value, val)
tot = val
Next item
lblGrandTotal.Text = (tot * (1 - cash / 100)).ToString("N2")
End Sub
Private Sub BtnRefresh_Click(sender As Object, e As EventArgs) Handles BtnRefresh.Click
lblDisTotal.Text = ""
End Sub