Note that there are some explanatory texts on larger screens.

plurals
  1. POisNumeric if then and doesn't work need to simplify
    text
    copied!<p>I want to simplify this if then statement but when I use if isNumeric (test) and (test) it gives me an error because of the datatype. I am very new to VB and would appreciate some guidance. I have several of these text boxes I want to validate with isNumberic. </p> <pre><code> Private Sub btnCalculate_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCalculate.Click 'Declare variables for pay Dim decConneryPay As Decimal = 0 Dim decLazenbyPay As Decimal = 0 Dim decMoorePay As Decimal = 0 Dim decDaltonPay As Decimal = 0 Dim decBrosnanPay As Decimal = 0 Dim decCraigPay As Decimal = 0 'Initial clear lblPayError on each calculation lblPayError.Text = String.Empty 'Convert pay rate text boxes to decimals and * with hours 'Check txtConneryHours for validation If IsNumeric(txtConneryHours.Text) Then decConneryPay = CDec(txtRateSean.Text) * CDec(txtConneryHours.Text) lblConneryPay.Text = decConneryPay.ToString("c") Else lblPayError.ForeColor = Color.Red lblPayError.Text = "Hours worked can only contain positive integers." End If 'Check txtLazenbyHours for validation If IsNumeric(txtLazenbyHours.Text) Then decLazenbyPay = CDec(txtRateLazenby.Text) * CDec(txtLazenbyHours.Text) lblLazenbyPay.Text = decLazenbyPay.ToString("c") Else lblPayError.ForeColor = Color.Red lblPayError.Text = "Hours worked can only contain positive integers." End If 'Check txtMooreHours for validation If IsNumeric(txtMooreHours.Text) Then decMoorePay = CDec(txtRateMoore.Text) * CDec(txtMooreHours.Text) lblMoorePay.Text = decMoorePay.ToString("c") Else lblPayError.ForeColor = Color.Red lblPayError.Text = "Hours worked can only contain positive integers." End If 'Check txtDaltonHours for validation If IsNumeric(txtDaltonHours.Text) Then decDaltonPay = CDec(txtRateDalton.Text) * CDec(txtDaltonHours.Text) lblDaltonPay.Text = decDaltonPay.ToString("c") Else lblPayError.ForeColor = Color.Red lblPayError.Text = "Hours worked can only contain positive integers." End If 'Check txtBrosnanHours for validation If IsNumeric(txtBrosnanHours.Text) Then decBrosnanPay = CDec(txtRateBrosnan.Text) * CDec(txtBrosnanHours.Text) lblBrosnanPay.Text = decBrosnanPay.ToString("c") Else lblPayError.ForeColor = Color.Red lblPayError.Text = "Hours worked can only contain positive integers." End If 'Check txtCraigHours for validation If IsNumeric(txtCraigHours.Text) Then decCraigPay = CDec(txtRateCraig.Text) * CDec(txtCraigHours.Text) lblCraigPay.Text = decCraigPay.ToString("c") Else lblPayError.ForeColor = Color.Red lblPayError.Text = "Hours worked can only contain positive integers." End If End Sub </code></pre> <p>End Class</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload