Note that there are some explanatory texts on larger screens.

plurals
  1. POisNumeric if then and doesn't work need to simplify
    primarykey
    data
    text
    <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>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. COThe If statement in the first code is wrong (has an opened bracket which is not closed and refers to the variable txt which is wrong). Also I am not sure why you want to simplify the code below: it includes two If statements for two different situations (analysis of two textboxes); it seems fine to me. You might put the else part together (this is common to both cases) via boolean flag; for example: Dim isWrong As Boolean = False; isWrong = true in each else and a new condition If(isWrong) then ... Color.Red. But this would be more or less the same (= no simplification).
      singulars
    2. COThe problem is I have 8 text boxes I am validating. Is it ok to use 8 if then isNumeric statements to validate? It seems I could combine all of these into one because I am checking for the same validation in all boxes.
      singulars
    3. COFirst of all bear in mind that what is OK is what is more clear to you (easy to code, easy to extend/understand in the future); the fact of writing more or less code is irrelevant from the execution point of view. Regarding what you are asking, if you have to perform the same analysis and it is possible to iterate through all the elements (are contained in a collection), you should rely on a loop. For example: for each through all the textboxes in the given form. If the analysis is different every time, you would have to set as many conditions/switch statements as textboxes.
      singulars
 

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