无缘无故地获得未发现的错误(Getting uncasted error for no reason)

编程入门 行业动态 更新时间:2024-10-17 00:27:16
无缘无故地获得未发现的错误(Getting uncasted error for no reason)

我对visual basic相当新,所以它可能很明显,但我认为没有理由抛出异常错误。 这是问题代码:

Public Sub textBoxFilePath_TextChanged(sender As Object, e As EventArgs) Handles textboxFilePath.TextChanged If textboxFilePath.Text.Trim.Length > 0 Then If My.Computer.FileSystem.FileExists(textboxFilePath.Text) Then Dim checkType As String checkType = System.IO.Path.GetExtension(textboxFilePath.Text) If checkType = ".xlsx" Or ".xls" Or ".xlsm" Then requiredPath = True Else requiredPath = False End If End If End If

我得到的错误是

Microsoft.VisualBasic.dll中发生未处理的“System.InvalidCastException”类型异常

附加信息:从字符串“.xls”到“布尔”类型的转换无效。

有任何想法吗?

I am fairly new to visual basic, so it may be obvious, but I see no reason for this to throw and uncasted exception error. Here is the problem code:

Public Sub textBoxFilePath_TextChanged(sender As Object, e As EventArgs) Handles textboxFilePath.TextChanged If textboxFilePath.Text.Trim.Length > 0 Then If My.Computer.FileSystem.FileExists(textboxFilePath.Text) Then Dim checkType As String checkType = System.IO.Path.GetExtension(textboxFilePath.Text) If checkType = ".xlsx" Or ".xls" Or ".xlsm" Then requiredPath = True Else requiredPath = False End If End If End If

The error I am getting is

An unhandled exception of type 'System.InvalidCastException' occurred in Microsoft.VisualBasic.dll

Additional information: Conversion from string ".xls" to type 'Boolean' is not valid.

Any ideas?

最满意答案

尝试这个:

If checkType = ".xlsx" Or checkType = ".xls" Or checkType = ".xlsm" Then requiredPath = True Else requiredPath = False End If

您的代码试图评估“.xls”(这是一个字符串),它不能很好地转换为布尔值true / false。

你也可以这样做来应用“短路”,所以只评估第一个是真的:

If checkType = ".xlsx" OrElse checkType = ".xls" OrElse checkType = ".xlsm" Then requiredPath = True Else requiredPath = False End If

Try this:

If checkType = ".xlsx" Or checkType = ".xls" Or checkType = ".xlsm" Then requiredPath = True Else requiredPath = False End If

Your code was trying to evaluate ".xls" (which is a string), which does not convert nicely to a boolean true/false.

You could also do this to apply "short circuiting", so only the first one true is evaluated:

If checkType = ".xlsx" OrElse checkType = ".xls" OrElse checkType = ".xlsm" Then requiredPath = True Else requiredPath = False End If

更多推荐

本文发布于:2023-08-07 13:07:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1464496.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:无缘无故   错误   发现   reason   uncasted

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!