Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/vb.net/15.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Vb.net 无效转换-强制转换异常:';系统。无效卡斯特例外';_Vb.net_Visual Studio 2010 - Fatal编程技术网

Vb.net 无效转换-强制转换异常:';系统。无效卡斯特例外';

Vb.net 无效转换-强制转换异常:';系统。无效卡斯特例外';,vb.net,visual-studio-2010,Vb.net,Visual Studio 2010,我已经编写了一个代码,只要用户正确输入信息,它就可以完美地工作。如果用户在文本框中输入了一个数字,我会收到一条错误消息,告知必须输入用户数据。当这种情况发生时,我会得到一个强制转换异常: 引发异常:Microsoft.VisualBasic.dll中的“System.InvalidCastException” 其他信息:从字符串“”到类型“整型”的转换无效 我是新手,如果有任何建议,我将不胜感激 此行出现异常: intDetermineSubTotalCost(intSiteSelected,

我已经编写了一个代码,只要用户正确输入信息,它就可以完美地工作。如果用户在文本框中输入了一个数字,我会收到一条错误消息,告知必须输入用户数据。当这种情况发生时,我会得到一个强制转换异常:

引发异常:Microsoft.VisualBasic.dll中的“System.InvalidCastException”

其他信息:从字符串“”到类型“整型”的转换无效

我是新手,如果有任何建议,我将不胜感激

此行出现异常:

intDetermineSubTotalCost(intSiteSelected, CInt(txtNumberOfNights.Text))
如果我删除strict on选项,它将与预期的对话一起工作,但是项目要求它处于启用状态

Dim intSiteSelected As Double
Dim intNumberOfNights As Integer
Dim blnNightsStayingIsVaild As Boolean = False
Dim blnDiscountIsSelected As Boolean = False
Dim intDiscountChoice As Integer
Dim strDiscountSelected As String = ""

'   Call a function to confirm if the Number of Nights staying is valid.
blnNightsStayingIsVaild = ValidateNightsStaying()

'   call a function to confirm a discount has been seected.
intDiscountChoice = ValidateSelectedDiscount(blnDiscountIsSelected, strDiscountSelected)

'   If number of nights staying and available discount has been selected,
'   calculate the subtotal, tax, and final cost.
If (blnNightsStayingIsVaild And blnDiscountIsSelected) Then
    intNumberOfNights = Convert.ToInt32(txtNumberOfNights.Text)
    intSiteSelected = Me.cmbSelectASite.SelectedIndex
End If


If cmbSelectASite.SelectedIndex = 0 Then
    intSiteSelected = 20D
ElseIf cmbSelectASite.SelectedIndex = 1 Then
    intSiteSelected = 35D
ElseIf cmbSelectASite.SelectedIndex = 2 Then
    intSiteSelected = 55D
End If

intDetermineSubTotalCost(intSiteSelected, CInt(txtNumberOfNights.Text))


Private Function ValidateNightsStaying() As Boolean

'   This function validate the value entered for the number of nights staying in campground.
Dim intNightsStaying As Integer
Dim blnValidityCheck As Boolean = False
Dim StrNumberOfNightsErrorMessage As String =
    "Please Enter The Number Of Nights You Are Staying (1-99)"
Dim strMessageBoxTitle As String = " You Must Choose Number Of Nights"

Try
    intNightsStaying = Convert.ToInt32(txtNumberOfNights.Text)
    If intNightsStaying > 0 And intNightsStaying < 100 Then
        blnValidityCheck = True
    Else
        MsgBox(StrNumberOfNightsErrorMessage, , strMessageBoxTitle)
        blnValidityCheck = True
    End If
Catch Exception As FormatException
    MsgBox(StrNumberOfNightsErrorMessage, , strMessageBoxTitle)
    txtNumberOfNights.Focus()
    txtNumberOfNights.Clear()
Catch execption As OverflowException
    MsgBox(StrNumberOfNightsErrorMessage, , strMessageBoxTitle)
    txtNumberOfNights.Focus()
    txtNumberOfNights.Clear()
Catch execption As SystemException
    MsgBox(StrNumberOfNightsErrorMessage, , strMessageBoxTitle)
    txtNumberOfNights.Focus()
    txtNumberOfNights.Clear()
End Try
Return blnValidityCheck

End Function
Dim INTSITE选择为双精度
Dim intNumberOfNights作为整数
Dim BLNNIGHTSSTAYINGISAVILED为布尔值=False
尺寸BLNDiscountissected为布尔值=False
Dim intDiscountChoice作为整数
Dim STRDISCONTRECTSELECTED As String=“”
'调用函数以确认入住天数是否有效。
blnNightsStayingIsVaild=validatenightstaying()
'调用函数以确认已看到折扣。
intDiscountChoice=ValidateSelectedDiscount(BLNDiscountSelected,strDiscountSelected)
'如果选择了住宿天数和可用折扣,
'计算小计、税金和最终成本。
如果(BlnLightsStaying可用且未选择BlnLightsStaying),则
intNumberOfNights=Convert.ToInt32(txtNumberOfNights.Text)
intSiteSelected=Me.cmbSelectASite.SelectedIndex
如果结束
如果cmbSelectASite.SelectedIndex=0,则
intSiteSelected=20D
如果cmbSelectASite.SelectedIndex=1,则
intSiteSelected=35D
如果cmbSelectASite.SelectedIndex=2,则
intSiteSelected=55D
如果结束
IntDeterminesSubtotalCost(intSiteSelected,CInt(txtNumberOfNights.Text))
私有函数validateNightsTaying()作为布尔值
'此函数用于验证为在营地停留的夜数输入的值。
Dim INTNIGHTSTAING为整数
Dim blnValidityCheck为布尔值=假
Dim StrNumberOfNightsErrorMessage作为字符串=
“请输入您入住的夜数(1-99)”
Dim strMessageBoxTitle As String=“您必须选择夜数”
尝试
intNightsStaying=Convert.ToInt32(txtNumberOfNights.Text)
如果IntNightsTaying>0且IntNightsTaying<100,则
blnValidityCheck=True
其他的
MsgBox(strNumber of NightsErrorMessage,strMessageBoxTitle)
blnValidityCheck=True
如果结束
捕获异常作为FormatException
MsgBox(strNumber of NightsErrorMessage,strMessageBoxTitle)
txtNumberOfNights.Focus()
txtNumberOfNights.Clear()
捕获执行项作为OverflowException
MsgBox(strNumber of NightsErrorMessage,strMessageBoxTitle)
txtNumberOfNights.Focus()
txtNumberOfNights.Clear()
捕获执行选项作为系统异常
MsgBox(strNumber of NightsErrorMessage,strMessageBoxTitle)
txtNumberOfNights.Focus()
txtNumberOfNights.Clear()
结束尝试
返回blnValidityCheck
端函数

有几种方法可以解决这个问题

  • 您可以检查字符串是否为空,并对此进行验证
  • 将转换更改为Triparse,IIRC将在空时返回0
  • 如果可能,将输入从文本框更改为数字上/下控件,最小值和最大值分别为1和99

  • 有几种方法可以解决这个问题

  • 您可以检查字符串是否为空,并对此进行验证
  • 将转换更改为Triparse,IIRC将在空时返回0
  • 如果可能,将输入从文本框更改为数字上/下控件,最小值和最大值分别为1和99

  • 你可以看看这样的东西,限制他们输入非数字按键

    Private Sub TextBox1_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles TextBox1.KeyPress
    
    '97 - 122 = Ascii codes for simple letters
    '65 - 90  = Ascii codes for capital letters
    '48 - 57  = Ascii codes for numbers
    
    If Asc(e.KeyChar) <> 8 Then
        If Asc(e.KeyChar) < 48 Or Asc(e.KeyChar) > 57 Then
            e.Handled = True
        End If
    End If
    
    End Sub
    
    Private Sub TextBox1\u KeyPress(ByVal sender作为对象,ByVal e作为System.Windows.Forms.KeyPressEventArgs)处理TextBox1.KeyPress
    '97-122=简单字母的Ascii码
    '65-90=大写字母的Ascii码
    '48-57=数字的Ascii码
    如果Asc(e.KeyChar)8,则
    如果Asc(e.KeyChar)<48或Asc(e.KeyChar)>57,则
    e、 已处理=真
    如果结束
    如果结束
    端接头
    
    你可以看看这样的东西,限制他们输入非数字按键

    Private Sub TextBox1_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles TextBox1.KeyPress
    
    '97 - 122 = Ascii codes for simple letters
    '65 - 90  = Ascii codes for capital letters
    '48 - 57  = Ascii codes for numbers
    
    If Asc(e.KeyChar) <> 8 Then
        If Asc(e.KeyChar) < 48 Or Asc(e.KeyChar) > 57 Then
            e.Handled = True
        End If
    End If
    
    End Sub
    
    Private Sub TextBox1\u KeyPress(ByVal sender作为对象,ByVal e作为System.Windows.Forms.KeyPressEventArgs)处理TextBox1.KeyPress
    '97-122=简单字母的Ascii码
    '65-90=大写字母的Ascii码
    '48-57=数字的Ascii码
    如果Asc(e.KeyChar)8,则
    如果Asc(e.KeyChar)<48或Asc(e.KeyChar)>57,则
    e、 已处理=真
    如果结束
    如果结束
    端接头
    
    只需在执行计算之前进行一些输入检查。 例如,需要输入的文本框:

            If txtInput.text.Contains("whatever you don't want there") Then
               MessageBox.Show("Please Enter The Correct Info")
               txtInput.Focus()
               Return
            End if
    


    如果出现错误,这将停止代码崩溃,并为用户提供修复其输入的机会。检查完所有输入后,执行计算。

    只需在执行计算之前执行一些输入检查。 例如,需要输入的文本框:

            If txtInput.text.Contains("whatever you don't want there") Then
               MessageBox.Show("Please Enter The Correct Info")
               txtInput.Focus()
               Return
            End if
    


    如果出现错误,这将停止代码崩溃,并为用户提供修复其输入的机会。检查完所有输入后,执行计算。

    将文本框更改为屏蔽文本框。掩码中只允许数字


    将文本框更改为屏蔽文本框。掩码中只允许数字


    试试这个什么是
    intdeterminatesubtotalcost
    ?错误本身是因为不能将空字符串(
    )转换为整数。对于所有用户输入,请使用Integer.TryParse,以便您可以处理他们键入的错误。请尝试以下内容
    intDetermineSubTotalCost
    ?错误本身是因为不能将空字符串(
    )转换为整数。对于所有用户输入,请使用Integer.TryParse,以便您可以处理他们键入的愚蠢内容