自定义对象的嵌套列表。网络
我是 VB.net 新手,需要您的帮助。 我有两个 vb.net 结构,Quotation 和 FareAsPerVehicleType,Quotation 依赖于 FareAsPerVehicleType。我尝试使用以下命令在报价中添加车辆类型:
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">
请帮忙 问候
I'm new to VB.net and requires your help.
I've two vb.net Structures, Quotation and FareAsPerVehicleType, Quotation is dependent on FareAsPerVehicleType. I am trying to add VehicleType in Quotation by using the following:
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
In doing so I am getting the following error.
<"System.NullReferenceException was unhandled">
<" Message=Object reference not set to an instance of an object.">
<" Source=WindowsApplication1">
Please help
Regards
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您需要先实例化该集合,然后才能使用它。实际上,当您声明新的 Quotation 对象时,VehicleType 列表将设置为空。将声明行更改为
或者更好的是,更改为声明以删除 Dim 并替换为 Public 以显示该字段的可访问性。
为了真正让代码发光,您可以用 auto 属性替换该字段:
任何这些都可以消除您的错误。
You need to instantiate the collection before you can use it. As is, when you are declaring a new Quotation object, the VehicleType list is set to nothing. Change the declaration line to
Or even better, change to declaration to remove the Dim and replace with Public to show the accessibility of the field.
To really make the code shine, you could replace the field with an auto property:
Any of these will work to get rid of your error.