1

好的,所以我已经编辑了我的代码,现在当我尝试运行它时,我收到了这个错误。

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

附加信息:从字符串“Holden 308”到类型“Integer”的转换无效。附加信息:从字符串“JD Catpillar Track”到类型“Integer”的转换无效。

因此,这两个错误都发生在重载 New 类的 HeavyStockItem 类中。想知道是否有人可以帮助我理解为什么会这样做。

Option Strict On 

Public Class Form1
Dim StockItem1 As StockItem
Dim StockItem2 As CarEngine
Dim StockItem3 As CarEngine
Dim StockItem4 As StockItem
Dim StockItem5 As HeavyStockItem

Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
    StockItem1 = New StockItem("Screwdriver Set", 42)

    StockItem2 = New CarEngine(8025, "Madza B6T", 1252, 800, "Z4537298D")

    'StockItem3 = New CarEngine("Holden 308", 958, 1104, "P74623854S")

    StockItem4 = New StockItem(8002, "Trolley Jack", 127)

    'StockItem5 = New HeavyStockItem("JD Catepillar Track", 3820, 2830)
End Sub

Private Sub btnListStock_Click(sender As Object, e As EventArgs) Handles btnListStock.Click
    txtOutput.Clear()
    ShowOutput(StockItem1)
    ShowOutput(StockItem2)
    'ShowOutput(StockItem3)
    ShowOutput(StockItem4)
    'ShowOutput(StockItem5)
End Sub

Public Sub ShowOutput(ByVal Output As StockItem)
    txtOutput.Text &= Output.Print()
    txtOutput.Text &= vbCrLf
End Sub

Private Sub Button1_Click(sender As Object, e As EventArgs) Handles btnEnd.Click
    End
End Sub 
End Class

Public Class StockItem

Friend CostPrice As Integer
Friend LastStockNumber As Integer
Friend StockNumber As Integer
Friend Description As String
Friend Shared LastStockItem As Integer = 10000

Overridable Function Print() As String
    Dim Result As String = ""
    Result &= "Stock No: " & StockNumber
    Result &= vbCrLf
    Result &= "Description: " & Description
    Result &= vbCrLf
    Result &= "Cost Price: " & CostPrice
    Result &= vbCrLf
    Return Result
End Function

Public Sub New(ByVal StockNumber As Integer, Description As String, ByVal CostPrice As Integer)
    Me.New(Description, CostPrice)
    Me.StockNumber = StockNumber
End Sub

Public Sub New(ByVal Description As String, ByVal CostPrice As Integer)
    LastStockNumber += Rnd()
    Me.StockNumber = LastStockNumber
    Me.Description = Description
    Me.CostPrice = CostPrice
End Sub

Public Sub GetCostPrice()

End Sub
End Class

Public Class HeavyStockItem
Inherits Assessment3.StockItem
Friend Weight As Integer

Public Function GetWeight() As String
    Return Me.GetWeight
End Function

Public Sub New(ByVal StockNumber As Integer, ByVal Description As String, ByVal CostPrice As Integer, ByVal Weight As Integer)
    MyBase.New(StockNumber, Description, CostPrice)
    Me.Weight = Weight
End Sub

Public Sub New(ByVal Description As String, ByVal CostPrice As Integer, ByVal Weight As Integer)
    MyBase.New(Description, CostPrice, Weight)'' Where the error is occurring
    LastStockNumber += Rnd()
    Me.StockNumber = LastStockNumber
End Sub
End Class

Public Class CarEngine
Inherits Assessment3.HeavyStockItem

Friend EngineNumber As String

Overrides Function Print() As String
    Dim Result As String = ""
    Result &= "Stock No: " & StockNumber
    Result &= vbCrLf
    Result &= "Description: " & Description
    Result &= vbCrLf
    Result &= "Cost Price: " & CostPrice
    Result &= vbCrLf
    Result &= "Weight: " & Weight
    Result &= vbCrLf
    Result &= "Engine Number: " & EngineNumber
    Result &= vbCrLf
    Return Result
End Function

Public Sub New(ByVal StockNumber As Integer, ByVal Description As String, ByVal CostPrice As Integer, ByVal Weight As Integer, ByVal EngineNumber As String)
    MyBase.New(StockNumber, Description, CostPrice, Weight)
    Me.EngineNumber = EngineNumber
End Sub

Public Sub New(ByVal Description As String, ByVal CostPrice As Integer, ByVal Weight As Integer, ByVal EngineNumber As String)
    MyBase.New(Description, CostPrice, Weight)
    LastStockNumber += Rnd()
    Me.StockNumber = LastStockNumber
    End Sub

End Class

提供的任何帮助都会很棒。只是认为提供完整的代码会更容易,而不是只放我真正需要的小部分,以防人们询问代码的其他部分。如果您提供帮助,感谢您阅读并提供帮助。

4

1 回答 1

0

由于您的代码让我感到困惑,我主要依赖于异常。

它基本上说它不能将字符串转换为整数。这可能会导致同样的问题。

Dim Number As String = "10"

'Few Lines of code.

Number = 11
基本上发生的事情是,当您将变量声明为字符串时,它的值必须用双引号括起来,正如您在声明它时看到的那样。现在我们尝试在不添加双引号的情况下将值更改为 11,因此它认为您正在将其更改为整数并给出错误。

我查看了代码并看到了这一点,也许这是导致它的原因:

Me.Description = Description<--- 没有引号!!!

我可能错了,因为我并没有真正理解代码。

于 2014-01-06T01:05:54.860 回答