自定义对象的嵌套列表。网络

发布于 2024-12-10 11:25:13 字数 854 浏览 5 评论 0原文

我是 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

蛮可爱 2024-12-17 11:25:13

您需要先实例化该集合,然后才能使用它。实际上,当您声明新的 Quotation 对象时,VehicleType 列表将设置为空。将声明行更改为

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)

任何这些都可以消除您的错误。

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

Dim VehicleType As New List(Of FareAsPerVehicleType)

Or even better, change to declaration to remove the Dim and replace with Public to show the accessibility of the field.

Public VehicleType As New List(Of FareAsPerVehicleType)

To really make the code shine, you could replace the field with an auto property:

Public Property VehicleType() As New List(Of FareAsPerVehicleType)

Any of these will work to get rid of your error.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文