Vb.net 如何将do Until与以前的Try-Catch异常一起使用,以避免代码运行两次
我有这行代码来捕获一个异常,如果输入了一个字母,或者它作为一个数字超出了范围,但是我添加了避免捕获数字数据的时间。现在我如何使用异常错误在case语句之前使用它,以避免代码运行两次,因为一旦case代码通过,它将运行一个已由try-catch处理的清除txtbox,如果您不清楚,但我理解它。下面是部分代码Vb.net 如何将do Until与以前的Try-Catch异常一起使用,以避免代码运行两次,vb.net,Vb.net,我有这行代码来捕获一个异常,如果输入了一个字母,或者它作为一个数字超出了范围,但是我添加了避免捕获数字数据的时间。现在我如何使用异常错误在case语句之前使用它,以避免代码运行两次,因为一旦case代码通过,它将运行一个已由try-catch处理的清除txtbox,如果您不清楚,但我理解它。下面是部分代码 Try 'Integer Levels: intLvls is egual to the assigned text box, the first one from
Try
'Integer Levels: intLvls is egual to the assigned text box, the first one from
'the top, this line of code allow the user input to be captured into a variable.
intLvls = txtBoxLvl.Text
Catch ex As Exception When IsNumeric(intLvls)
ErrTypeLetterFeild1()
Finally
analysingvalues1()
End Try
我想做的是:使用循环直到引用异常错误,以避免运行以下代码部分:
Private Sub analysingvalues1()
Do Until IsNumeric (ex As Exception)<------how do i do this???
Loop
Select Case intLvls
'User is prompt with the following label: lblLvl "Level of salespersons 1 - 4"
'to make a choice from 1 to 4 as available values.
Case 1 To 4
'This line regulates the range of acceptable values, first textbox: must be egual
'or higher than 1 and lower or egual to 4. Upon such rules a validation becomes
'correct and is directed to the isValidCalculation sub.
isValidCalculation()
Case Is < 1
ErrType1NumberRangeFeild()
Case Is > 4
ErrType1NumberRangeFeild()
Case Else
If txtBoxLvl.Text = "" Then
ErrTypeClear1()
Else
If Not IsNumeric(txtBoxLvl.Text) Then
ErrType1NumberRangeFeild()
Else
ErrTypeLetterFeild1()
ErrTypeClear1()
End If
End If
End Select 'Ending choices.
End Sub
私有子分析值1()
如果启用此选项,则执行直到IsNumeric(ex As Exception):
intLvls = txtBoxLvl.Text
将不再编译。这应该告诉你你做的事情很臭
打开选项Strict
正确的解决方案是不要盲目地允许运行时将字符串转换为int,并捕获异常
当您将字符串用户输入转换为整数时,错误输入并不是一种例外情况,这是您应该预料到的事情,并且应该为其编写防御代码
我会把它改写成这样:
'Integer Levels: intLvls is egual to the assigned text box, the first one from
'the top, this line of code allow the user input to be captured into a variable.
if integer.TryParse( txtBoxLvl.Text, intLvls )
analysingvalues1()
else
ErrTypeLetterFeild1()
编辑-正如下面Chris指出的,我的意思是选项严格。我建议使用但显式和严格,并推断是否可用。如果启用“严格”选项,请执行以下操作:
intLvls = txtBoxLvl.Text
将不再编译。这应该告诉你你做的事情很臭
打开选项Strict
正确的解决方案是不要盲目地允许运行时将字符串转换为int,并捕获异常
当您将字符串用户输入转换为整数时,错误输入并不是一种例外情况,这是您应该预料到的事情,并且应该为其编写防御代码
我会把它改写成这样:
'Integer Levels: intLvls is egual to the assigned text box, the first one from
'the top, this line of code allow the user input to be captured into a variable.
if integer.TryParse( txtBoxLvl.Text, intLvls )
analysingvalues1()
else
ErrTypeLetterFeild1()
编辑-正如下面Chris指出的,我的意思是选项严格。我建议使用but Explicit和Strict,并推断(如果可用)。将“Option Strict On”放在代码顶部,或在项目的属性中设置它。intLvls的类型是什么?将“Option Strict On”放在代码的顶部或在项目的属性中设置它。什么类型的intLvls?我想你是说Option Strict?我想你是说Option Strict?