好吧,所以我编辑了我的代码,现在当我试图运行它时,我收到了这个错误。
Microsoft.VisualBasic.dll中出现未处理的“System.InvalidCastException”类型异常
附加信息:从字符串“Holden 308”到“Integer”类型的转换无效。 附加信息:从字符串“JD Catepillar Track”到“Integer”类型的转换无效。
因此,在HeavyStockItem类中发生了重载New类的错误。想知道是否有人可以帮助我了解它为什么这样做。
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
提供的任何帮助都会很棒。只是认为提供完整的代码更容易,而不是仅仅放置我真正需要的小部分,如果人们询问代码的其他部分。如果您提供帮助,感谢您阅读并提供帮助。
答案 0 :(得分:0)
由于你的代码让我感到困惑,我主要依赖于例外。
它基本上说它不能将字符串转换为整数。这可能会导致同样的问题。
Dim Number As String = "10"
'Few Lines of code.
Number = 11
什么是基本上发生的是当你将一个变量声明为一个字符串时,它的值必须是双引号,就像你在声明它时所看到的那样。现在我们尝试将值更改为11而不添加双引号,因此它认为您将其更改为整数并给出了错误。
我仔细查看了代码并看到了这个,也许这就是造成它:
Me.Description = Description
< ---没有引号!!!
我可能错了,因为我并不真正理解代码。