【问题标题】:Error: A first chance exception of type 'System.InvalidCastException' occurred in Microsoft.VisualBasic.dll错误:Microsoft.VisualBasic.dll 中出现“System.InvalidCastException”类型的第一次机会异常
【发布时间】:2013-11-16 20:03:08
【问题描述】:

我正在研究一个简单的匹配算法。我对编程很陌生,不太了解我遇到的错误。

File.txt 包含这种格式的数据(每行之间没有空格):

5,Name,9,9,9,9

4,Name,4,8,0,3 

3,Name,4,7,3,5 

2,Name,3,5,6,3 

1,Name,5,8,2,9

0,Name,2,5,3,2

“在 Microsoft.VisualBasic.dll 中发生了“System.InvalidCastException”类型的第一次机会异常

附加信息:从字符串“”到类型“整数”的转换无效。”

“CustomNrOld = splits(0)”出现错误,我不明白为什么。

对任何形式的帮助都会很高兴!

Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
        Dim CustomNrNew As Integer
        Dim NameIDnew As String
        Dim Alphanew As Integer
        Dim Betanew As Integer
        Dim Gammanew As Integer
        Dim Deltanew As Integer
        Dim CustomNrOld As Integer
        Dim NameIDold As String
        Dim Alphaold As Integer
        Dim Betaold As Integer
        Dim Gammaold As Integer
        Dim Deltaold As Integer
        Dim R1 As Integer
        Dim R2 As Integer
        Dim R3 As Integer
        Dim R4 As Integer

    Using sr As New StreamReader("C:\\Users\\Paul\\Documents\\Weever\\file.txt")
        Dim splits As String() = sr.ReadLine.Split(","c)
        CustomNrNew = splits(0)
        NameIDnew = splits(1)
        Alphanew = splits(2)
        Betanew = splits(3)
        Gammanew = splits(4)
        Deltanew = splits(5)
    End Using

    Using sr As New StreamReader("C:\\Users\\Paul\\Documents\\Weever\\file.txt")
        sr.ReadLine()
        Do While Not sr.EndOfStream
            Dim splits As String() = sr.ReadLine.Split(","c)
            CustomNrOld = splits(0)
            NameIDold = splits(1)
            Alphaold = splits(2)
            Betaold = splits(3)
            Gammaold = splits(4)
            Deltaold = splits(5)

            If Alphanew >= Alphaold = True Then
                R1 = (Alphaold / Alphanew) * 100
            Else
                R1 = (Alphanew / Alphaold) * 100
            End If

            If Betanew >= Betaold = True Then
                R2 = (Betaold / Betanew) * 100
            Else
                R2 = (Betanew / Betaold) * 100
            End If
            If Gammanew >= Gammaold = True Then
                R3 = (Gammaold / Gammanew) * 100
            Else
                R3 = (Gammanew / Gammaold) * 100
            End If
            If Deltanew >= Deltaold = True Then
                R4 = (Deltaold / Deltanew) * 100
            Else
                R4 = (Deltanew / Deltaold) * 100
            End If

            Dim Result As Integer
            Result = ((R1 + R2 + R3 + R4) / 4)

            My.Computer.FileSystem.WriteAllText("C:\\Users\\Paul\\Documents\\Weever\\" & NameIDnew & ".txt",
               Result & "%" & " - " & NameIDold & " " & R1 & "% / " & R2 & "% / " & R3 & "% / " & R4 & "% " & Environment.NewLine, True)

            My.Computer.FileSystem.WriteAllText("C:\\Users\\Paul\\Documents\\Weever\\" & NameIDold & ".txt",
               Result & "%" & " - " & NameIDnew & " " & R1 & "% / " & R2 & "% / " & R3 & "% / " & R4 & "% " & Environment.NewLine, True)
        Loop
    End Using

【问题讨论】:

  • 在VB.NET中,你不需要转义反斜杠,所以"C:\\Users\\Paul\\Documents\\Weever\\file.txt"会写成"C:\Users\Paul\Documents\Weever\file.txt"

标签: vb.net


【解决方案1】:

如果您在 splits() 中引用某些内容并且希望它位于 Integer 变量中,则需要将字符串转换为整数(这就是错误告诉您的内容)。

例如:CustomNrNew = splits(0)

在这段代码中,您试图将一个字符串值(splits 是一个字符串数组)分配给一个整数 (CustomNrNew)。您不能将字符串直接转换为整数,因此会出现错误。

要将字符串值转换为整数,请尝试使用CustomNrNew = Convert.ToInt32(splits(0))(并将“0”替换为您在每一行中使用的任何值)。

【讨论】:

  • 最初的错误消失了,但现在我得到了这个错误。 mscorlib.dll 中出现“System.FormatException”类型的未处理异常附加信息:输入字符串的格式不正确。
  • 代码的哪一行?检查 Andrew Morton 对您原始帖子的评论并确保您的文件名格式正确
  • @user2957761 该错误表明您提供给 Convert.Int32 的字符串无法解析为数字。您应该看一下字符串,即使在尝试解析之前只使用MsgBox(splits(0)) 也应该足以进行简单的调试。
  • 做了所有这些,但仍然收到 CustomNrNew 行的第二个错误。真正让我感到困惑的是,我以前使用过这样的代码,它就像一个魅力。
  • @PaulEtscheit 让 Visual Studio 让您了解类型不匹配的简单方法是使用 Option Strict On
【解决方案2】:

您将拆分声明为一个字符串桶

然后尝试将字符串放入整数中

CustomNrOld = Convert.ToInt32(splits(0));  

会解决你的问题

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2012-10-29
    • 1970-01-01
    • 2012-07-18
    • 2014-06-28
    • 1970-01-01
    • 1970-01-01
    • 2013-06-21
    相关资源
    最近更新 更多