以对象数组为字段的对象数组

An array of objects with an array of objects as a field

我正在尝试创建一个过程 return 对象数组并将其分配给对象中的 属性。

shipment.item(i).stuff = GetStuffArray()

获取填充数组

    Public Function GetStuffArray() As Stuff()
        Dim stuff(5) As Stuff

         For i As Integer = 0 To stuff.Length
           stuff(i) = New Stuff
           stuff(i).things = "New Stuff"
         Next

        Return stuff

    End Function

我认为 VB.NET 足够“好”,我可以分配一个数组

这是我的 classes

Public Class Shipments
    Public Property items() As items
    Public Property bad As Boolean = True
End Class

Public Class items
    Public Property stuff() As stuff
    Public Property property As String
End Class

Public Class stuff
    Public Property things As String
End Class

我在这里错过了什么?我试图定义 shipment.item(i).stuff = New Stuff() 的大小,但我似乎无法定义大小。

ReDim 似乎不起作用,这让我觉得我的 class 的实现是错误的。我在 Visual Studio 中使用选择性粘贴来创建 class 和 JSON。所以我假设 class 结构是正确的。

ReDim shipment.item(i).stuff(length)

我访问过的资源:

https://docs.microsoft.com/en-us/dotnet/visual-basic/programming-guide/language-features/arrays/
https://www.includehelp.com/vb-net/create-an-array-of-objects-of-the-class.aspx

希望这对以后的人有所帮助。打错了。

Public Class Shipments
    Public Property items() As items()
    Public Property bad As Boolean = True
End Class

Public Class items
    Public Property stuff() As stuff()
    Public Property property As String
End Class

Public Class stuff
    Public Property things As String
End Class

到此为止,感谢所有浪费时间阅读本文的人。

这不完全是你的错。 VB.Net 允许在任何 属性 的名称后添加一个可选的 (),这不会使其本身成为一个数组。那是因为属性允许索引器。对比字段或局部变量,这会导致它成为一个数组。

以下是一些字段和属性,其中包含使用 ()

的所有声明组合
' Fields and local variables behave the same way, using only fields for example
Private stuffField As stuff
Private stuffFieldArray1 As stuff()
Private stuffFieldArray2() As stuff
' Field doesn't compile: Array modifiers cannot be specified on both a variable and its type.
Private stuffFieldArray3() As stuff()
Private Property stuffProperty1 As stuff
' This () is not an array modifier, it is optional on property declarations
Private Property stuffProperty2() As stuff
' For a property to be an array the () must be after the type
Private Property stuffPropertyArray1 As stuff()
' The () after the name doesn't do anything to properties
Private Property stuffPropertyArray2() As stuff()
Private Property stuffPropertyParameter(param As Object) As stuff
    Get
    End Get
    Set(value As stuff)
    End Set
End Property
Private Property stuffPropertyArrayParameter1(param As Object) As stuff()
    Get
    End Get
    Set(value As stuff())
    End Set
End Property
' Now this doesn't compile, the () was already used: End of statement expected.
Private Property stuffPropertyArrayParameter2(param As Object)() As stuff()
    Get
    End Get
    Set(value As stuff())
    End Set
End Property

请注意,该字段在两个地方都不允许使用 (),但 属性 可以,而且它只是一个普通的旧数组。

您可以使用或不使用 () 访问或分配给 属性。无法使用 () 分配字段。允许使用和不使用 () 来分配属性。索引属性必须在 ().

中分配索引
stuffField = New stuff() '                                 yes
stuffField() = New stuff() '                               no
stuffFieldArray1 = {New stuff()} '                         yes
stuffFieldArray1() = {New stuff()} '                       no
stuffFieldArray2 = {New stuff()} '                         yes
stuffFieldArray2() = {New stuff()} '                       no
' stuffFieldArray3                                         didn't compile

stuffProperty1 = New stuff() '                             yes
stuffProperty1() = New stuff() '                           yes
stuffProperty2 = New stuff() '                             yes
stuffProperty2() = New stuff() '                           yes
stuffPropertyArray1 = {New stuff()} '                      yes
stuffPropertyArray1() = {New stuff()} '                    yes
stuffPropertyArray2 = {New stuff()} '                      yes
stuffPropertyArray2() = {New stuff()} '                    yes

stuffPropertyParameter() = New stuff() '                   no
stuffPropertyParameter(New Object) = New stuff() '         yes
stuffPropertyArrayParameter1() = {New stuff()} '           no
stuffPropertyArrayParameter1(New Object) = {New stuff()} ' yes
' stuffPropertyArrayParameter2                             didn't compile

关于在两个地方都带有 () 的字段声明的一个有趣的事情是编译器 想要 将它视为锯齿状数组,因为多个 ()(我猜)