0

I am running into an issue with Array resizing in vb.net. I sort of understand why the issue is coming up but I'm not sure how to get around it. Basically, I have a class that has an array of objects being passed to it. I'm trying to have a sub resize the array and add another object to it. However, once it's done, the original object does not get updated.

Optimally I would like something like this.

Sub Main()
    Dim parent As New Parent
    Dim first As New Child()
    Dim second As New Child()
    Dim children As Child() = New Child() {first, second}
    parent.children = children
    setChildren(getChildren(parent))
End Sub

Private Function getChildren(parent As Parent) As Child()
    Return parent.children
End Function

Private Sub setChildren(ByRef testArray As Child())
    testArray = New Child(3) {}
End Sub
2
  • 1
    Stop using Arrays and switch to generic lists. Commented May 2, 2012 at 17:25
  • unfortunately it's not up to me. It's an object that's automatically generated from an xml schema Commented May 2, 2012 at 17:30

3 Answers 3

2

Because setChildren accepts its testArray parameter by ref, it must be given a variable or field, rather than a property or function return. If Parent.children is a field, rather than a property, one could call setChildren(parent.children);. Alternatively, one could make Parent.children hold a type which itself holds a reference to an array; the two built-in types which meet that criterion would be List(Of Child) and Child()(). Incidentally, I'd suggest changing your identifier names so that type names and variable names are clearly distinct (vb.net is not case-sensitive).

Sign up to request clarification or add additional context in comments.

5 Comments

Would it possible without changing the parent? I don't have access to it
If the parent exposes children as a field, then it can by passed ByRef. If it's a read/write property, you may be able to copy it to a variable, pass that ByRef, and copy it back (in some cases, the compiler will do essentially that, automatically, but it's better to be explicit). Is there any reason neither of those approaches is adequate?
The field is the issue. It's an object generated by xsd.exe which generates properties not fields. Plus I'm not sure the other team would be up for changing it.
@Leon: If the field holds a reference to an array of two elements, the only way it will ever hold a reference to an array of three elements is for such a reference to be stored there, replacing the reference to the array of two elements.
Thanks supercat, I'll have to talk to my team then.
0

Based on your requirements I think the ReDim Statement is what you are after:

Private Sub setChildren(ByRef testArray As Child())
    ReDim Preserve testArray(3)
End Sub

The Preserve Statement will copy the contents of testArray into the newly created array.

1 Comment

That still leaves the original array untouched.
0

Since you are using arrays, you need to ReDim the array to add another element.

Private Sub setChildren(ByRef testArray As Child())
   Dim arrayLength as Int = testArray.Length  'total number of elements in array
   ReDim Preserve testArray(arrayLength) 
   testArray(arrayLength) = New Child {}
End Sub

Edit: Forget the Preserve keyword

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.