我是VB.net的新手,需要你的帮助。 我有两个vb.net Structures,Quotation和FareAsPerVehicleType,Quotation依赖于FareAsPerVehicleType。我试图通过使用以下内容在报价中添加VehicleType:
Dim Quot As New Quotation
Dim vT As FareAsPerVehicleType
vT.TypeOfVehicle = "S"
vT.Fare = _raw_Price * vF.Saloon_Factor
Quot.VehicleType.Add(vT)
Public Structure FareAsPerVehicleType
Dim TypeOfVehicle As String
Dim Fare As Decimal
End Structure
Public Structure Quotation
Dim VehicleType As List(Of FareAsPerVehicleType)
Dim Mileage As Decimal
Dim TimeToTravel As Decimal
Dim Pickup As String
Dim Dropoff As String
End Structure
这样做我收到以下错误。
<"System.NullReferenceException was unhandled">
<" Message=Object reference not set to an instance of an object.">
<" Source=WindowsApplication1">
请帮忙 此致
答案 0 :(得分:0)
您需要先实例化该集合,然后才能使用它。当您声明新的Quotation对象时,VehicleType列表将设置为nothing。将声明行更改为
Dim VehicleType As New List(Of FareAsPerVehicleType)
甚至更好,更改为声明以删除Dim并替换为Public以显示该字段的可访问性。
Public VehicleType As New List(Of FareAsPerVehicleType)
要真正使代码闪耀,您可以使用auto属性替换该字段:
Public Property VehicleType() As New List(Of FareAsPerVehicleType)
其中任何一个都可以解决您的错误。