【问题标题】:Conversion Type Error: Type String cannot be converted to Integer转换类型错误:无法将类型字符串转换为整数
【发布时间】:2018-07-04 04:36:33
【问题描述】:

嗨,伙计们,我不断收到一个异常错误,即字符串无法转换为整数并且不太清楚为什么...我尝试将 toString() 方法添加到许多不同的地方以尝试解决,但没有任何效果...可以请你看看这个,看看我在哪里愚蠢。我基本上是想看看何时找到一个新项目,看看它是否以历史记录中的所有行开头,将其拆分为单词“”“URL”“”

我在抛出异常的 if 语句上方发表了评论。

确切的错误是:

System.InvalidCastException: '从字符串 "{ "Title": "Item 1" 到类型 'Integer' 的转换无效。'

内部异常

FormatException: 输入字符串的格式不正确。

Dim History = File.ReadAllLines("history.json")
Dim NewestItems = File.ReadAllLines("items.json")
Dim InitialItems = File.ReadAllLines("Init-items.json")

For x As Integer = 0 To InitialItems.Length - 1
        Dim current As String = InitialItems(0)
        Dim splitAt As String = """Url"""
        Dim index As Integer = current.IndexOf(splitAt)
        Dim output As String = current.Substring(0, index + splitAt.Length)
        Dim recurringItemFound = 0
        If Not NewestItems(x).StartsWith(output) And Not History.Contains(NewestItems(x)) Then

            If cbxRecurringItemsOff.Checked = True Then
                For Each RecurringItem In History
                    Dim indexHistory As Integer = NewestItems(x).IndexOf(splitAt)
                    Dim outputHistory As String = NewestItemss(x).Substring(0, indexHistory + splitAt.Length)
                    '********the below if statement throws an Exception error*********
                    If NewestItems(RecurringItem).StartsWith(outputHistory) Then
                        recurringItemFound = 1
                    End If
                Next
            End If
Next

【问题讨论】:

    标签: vb.net


    【解决方案1】:

    使用Option StrictOption Explicit - 这会标记一些问题。

    在这种情况下,您使用RecurringItem 作为数组NewestItems 中的索引,因此它试图将RecurringItem 转换为Integer。但是,RecurringItem 来自History,我假设它是String 的数组。因此,您正在尝试使用从文件中读取的字符串作为整数,而程序现在正在“大吃一惊”。

    你真的是说NewestItems(x).StartsWith(outputHistory)吗?

    【讨论】:

    • 是的,最终它找到了我
    猜你喜欢
    • 2021-02-14
    • 1970-01-01
    • 2012-12-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-06-18
    相关资源
    最近更新 更多