Reputation: 373
I am trying to have a procedure return an array of objects and assign that to the property within a object.
shipment.item(i).stuff = GetStuffArray()
Get Stuff Array
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
I thought VB.NET was "nice" enough that I could just assign an array
Here is my 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
What am I missing here? I attempted to to define the size of shipment.item(i).stuff = New Stuff()
but I can't seem to define the size.
ReDim doesn't seem to work, which makes me thing the implementation of my class is wrong. I used Paste Special in Visual Studio to create the class with JSON. So I would assume the class structure is correct.
ReDim shipment.item(i).stuff(length)
Resources I have accessed:
Array of objects within an array of objects
https://learn.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
Upvotes: 0
Views: 1757
Reputation: 15774
It's not entirely your fault. VB.Net allows for an optional ()
after the name of any property which doesn't make it an array by itself. That is because properties allow for indexers. Contrast with a field or local variable where that would cause it to be an array.
Here are some fields and properties with all the combinations of declarations using ()
' 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
Note the field doesn't allow ()
in both places, but the property does, and it's just a plain old array.
You can access or assign to a property using the ()
or not. Fields can't be assigned with ()
. Properties are allowed to be assigned with and without the ()
. Indexed properties must be assigned with an index in the ()
.
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
One interesting thing about the field declaration with ()
in both places is that the compiler wants to treat it as a jagged array because of the multiple ()
(I guess)
Upvotes: 1
Reputation: 373
Well hopefully this will be helpful for someone in the future. It was Typo.
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
That did it, thanks for anyone who wasted their time reading this.
Upvotes: 0