从字符串(x)到类型“整数”的转换无效

时间:2018-03-22 10:38:33

标签: visual-studio-2012

为课堂做点什么,我完全陷入困境,即使老师似乎也不确定。任何回应赞赏。这是造成问题的第一条印刷线: “Microsoft.VisualBasic.dll中出现'System.InvalidCastException'类型的第一次机会异常

附加信息:从字符串“1:夜间F3,购买10张门票”到“整数”类型的转换无效。“

Dim filename As String
    Dim fridaytickets As Integer
    fridaytickets = 0
    filename = "Z:\Computing Science\S5 (Higher)\Coursework Assessment\output.txt"
    FileOpen(1, filename, OpenMode.Output)
    For x = 1 To 300
        If Mid(TicketID(x), 1, 1) = "F" Then
            PrintLine((x) & ": Night " & TicketID(x) & ", " & TicketNo(x) & " tickets purchased. £" & (TicketNo(x) * 10) & " made.")
            fridaytickets = fridaytickets + TicketNo(x)
        End If
        PrintLine(fridaytickets & " were purchased for Friday night.")
        PrintLine("£" & (fridaytickets * 10) & " was made.")

1 个答案:

答案 0 :(得分:0)

这是一行:

PrintLine(fridaytickets & " were purchased for Friday night.")

你的fridaytickets是一个整数,奇怪的是,似乎&被视为二元运算符,所以两边都是整数。它应该不是这样,但也许它是一个bug或版本依赖。或者只是正常连接运算符的重载。

在任何情况下,正确的方法是准备要连接的字符串:

PrintLine(fridaytickets.toString & " were purchased for Friday night.")