如何使用我的if语句来防止代码崩溃 [英] How do I use my if statements to keep the code from crashing

查看:65
本文介绍了如何使用我的if语句来防止代码崩溃的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我的代码





Option Strict On

Option Explicit On

'Richard Brown

'10 / 28/17

'每周工资计算器

'这是一个用来计算每周工资的程序

公共类frmPay



私有子btnCalculate_Click(发件人为对象,e为EventArgs)处理btnCalculate.Click

昏暗时间,payPerHour,加班时间,支付为十进制

输入数据(小时,payPerHour)

CalculateValues(小时,payPerHour,加班时间,支付)

DisplayData(超时,支付)

结束子

子输入数据(ByRef小时为十进制,ByRef payPerHour为十进制)

如果IsNumeric(txtHoursWorked) .Text)= False然后

MessageBox.Show(请输入值)

ElseIf IsNumeric(txtHourlyPay.Text)= False然后

MessageBox.Show(请输入A.值)

ElseIf txtHoursWorked.Text =然后

MessageBox.Show(请输入值)

ElseIf txtHourlyPay.Text =然后

MessageBox.Show(请输入值)

结束如果

小时= CDec(txtHoursWorked。文字)

payPerHour = CDec(txtHourlyPay.Text)




End Sub

Sub CalculateValues (小时为十进制,payPerHour为双倍,ByRef加速时间为十进制,ByRef支付为十进制)



overtimeHours = Math.Abs​​(40小时)

如果小时< = 40那么

pay = CDec((payPerHour *小时))

ElseIf hours> 40然后

pay = CDec((CDec(payPerHour *小时)+(payPerHour * 1.5)*(加班时间)))

结束如果

结束子



子显示数据(超时小时为十进制,支付为十进制)

txtOvertime.Text = CStr(超时小时)

txtWeeksPay.Text = pay.ToString(C)

End Sub

End Class



我尝试过的事情:



我试过如果声明但代码继续崩溃。



如果IsNumeric(txtHoursWorked.Text)= False那么

MessageBox.Show(请输入值)

ElseIf IsNumeric(txtHourlyPay) .Text)= False然后

MessageBox.Show(请输入值)

ElseIf txtHoursWorked.Text =然后

MessageBox 。显示(请输入值)

ElseIf txtHourlyPay.Text =然后

消息Box.Show(请输入价值)

结束如果

它在这里崩溃:



小时= CDec(txtHoursWorked.Text)

payPerHour = CDec(txtHourlyPay.Text)

This Is My Code


Option Strict On
Option Explicit On
'Richard Brown
'10/28/17
'Weekly Pay Calculator
'This is a program used to calculate weekly pay
Public Class frmPay

Private Sub btnCalculate_Click(sender As Object, e As EventArgs) Handles btnCalculate.Click
Dim hours, payPerHour, overtimeHours, pay As Decimal
InputData(hours, payPerHour)
CalculateValues(hours, payPerHour, overtimeHours, pay)
DisplayData(overtimeHours, pay)
End Sub
Sub InputData(ByRef hours As Decimal, ByRef payPerHour As Decimal)
If IsNumeric(txtHoursWorked.Text) = False Then
MessageBox.Show("Please Enter A Value")
ElseIf IsNumeric(txtHourlyPay.Text) = False Then
MessageBox.Show("Please Enter A Value")
ElseIf txtHoursWorked.Text = "" Then
MessageBox.Show("Please Enter A Value")
ElseIf txtHourlyPay.Text = "" Then
MessageBox.Show("Please Enter A Value")
End If
hours = CDec(txtHoursWorked.Text)
payPerHour = CDec(txtHourlyPay.Text)


End Sub
Sub CalculateValues(hours As Decimal, payPerHour As Double, ByRef overtimeHours As Decimal, ByRef pay As Decimal)

overtimeHours = Math.Abs(40 - hours)
If hours <= 40 Then
pay = CDec((payPerHour * hours))
ElseIf hours > 40 Then
pay = CDec((CDec(payPerHour * hours) + (payPerHour * 1.5) * (overtimeHours)))
End If
End Sub

Sub DisplayData(overtimeHours As Decimal, pay As Decimal)
txtOvertime.Text = CStr(overtimeHours)
txtWeeksPay.Text = pay.ToString("C")
End Sub
End Class

What I have tried:

I've tried If Statements but the code continues to crash.

If IsNumeric(txtHoursWorked.Text) = False Then
MessageBox.Show("Please Enter A Value")
ElseIf IsNumeric(txtHourlyPay.Text) = False Then
MessageBox.Show("Please Enter A Value")
ElseIf txtHoursWorked.Text = "" Then
MessageBox.Show("Please Enter A Value")
ElseIf txtHourlyPay.Text = "" Then
MessageBox.Show("Please Enter A Value")
End If
It Crashes Here:

hours = CDec(txtHoursWorked.Text)
payPerHour = CDec(txtHourlyPay.Text)

推荐答案

猜测,文本框中的数据不是有效的号码。

是的,你检查它 - 并显示一个消息框。但这就是你要做的全部。

我建议当你发现问题时从方法返回将是一个更好的方法来做事情和显示消息。毕竟,如果将工作小时数作为奶酪输入,则无论如何都没有意义......



此外,出现同样的错误每个问题的消息都无法帮助用户识别出错的地方。
At a guess, the data in your text box is not a valid number.
Yes you check for it - and display a message box. But that is all you do.
I'd suggest that Returning from the method when you find a problem would be a much better way to do things as well as displaying a message. There is after all no point in carrying on if the "hours worked" has been entered as "Cheese" for example...

In addition, having the same error message for every problem doesn't help the user to identify where he went wrong.


这篇关于如何使用我的if语句来防止代码崩溃的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆