好的,所以我已经编辑了我的代码,现在当我尝试运行它时,我收到了这个错误。
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
提供的任何帮助都会很棒。只是认为提供完整的代码会更容易,而不是只放我真正需要的小部分,以防人们询问代码的其他部分。如果您提供帮助,感谢您阅读并提供帮助。