Initialise an VB .NET String Array
How create a array of objects (another class) in VB.NET and initialise it. Since I am not sure about the length of the array, it should be generic. I mean I should be able to add any开发者_JAVA技巧 number of objects to the array. NB: I am much familiar with Generic List, but my client has given me array of objects :(
I should be able to add any number of objects to the array
Simply, you can’t, arrays aren’t resizable. You can use Array.Resize
(or ReDim Preserve
) but this will re-allocate the whole array and has abysmal runtime.
Use the list (List(Of T)
) for your purpose. If you get the input in the form of an array, it’s a simple matter of transforming this input:
Dim lst As New List(Of YourObject)(inputArray)
Likewise for return values: if you need to return an array, use the ToArray()
method of the list.
But using arrays in public interfaces (that is, as parameters and return values of public methods) is bad design and shouldn’t be done anyway. Talk to your client about this.
精彩评论