Dynamic Array Problem

MRRX8

Newcomer
Joined
Jan 29, 2006
Messages
7
Hi All

Im new to this forum and new to VB.NET coming from VB6. I have a problem with a dynamic array and am unsure why i get the error "Object reference not set to an instance of an object."
im trying to add the text of a textbox to the array, i have also noticed that when i initilise the array with elements instead of keeping it dynamic it works.

Anybody have any ideas???

Thank you experts

My code is as follows

Code:
        Dim ctrl As Control, lbl As Control
        Dim iCtrl, i As Integer
        Dim strSizeArray() As String

        For iCtrl = 0 To Panel1.Controls.Count - 1
            ctrl = Panel1.Controls(iCtrl)
            If ctrl.GetType Is GetType(System.Windows.Forms.TextBox) Then
                strSizeArray(iCtrl) = (CType(ctrl, TextBox).Text)
            End If
        Next
 
If that's all of your code (nothing missing), then I don't see where you initilize strSizeArray. Just like in VB6, you'll have to set the dimensions of the array. I think VB.NET support ReDim, though I think it's generally preferred to set the array once or use another structure such as an ArrayList.

Here's an example that initializes the array. Some elements will go unused (and will remain null/Nothing):
Visual Basic:
        Dim ctrl As Control, lbl As Control
        Dim iCtrl, i As Integer
        Dim strSizeArray() As String = New String(Panel1.Controls.Count)

        For iCtrl = 0 To Panel1.Controls.Count - 1
            ctrl = Panel1.Controls(iCtrl)
            If ctrl.GetType Is GetType(System.Windows.Forms.TextBox) Then
                strSizeArray(iCtrl) = (CType(ctrl, TextBox).Text)
            End If
        Next

The changed line is where I initialized strSizeArray. My VB.NET is scratchy at best, so hopefully that's the right syntax.

-ner
 
Nerseus said:
If that's all of your code (nothing missing), then I don't see where you initilize strSizeArray. Just like in VB6, you'll have to set the dimensions of the array. I think VB.NET support ReDim, though I think it's generally preferred to set the array once or use another structure such as an ArrayList.

Here's an example that initializes the array. Some elements will go unused (and will remain null/Nothing):
Visual Basic:
        Dim ctrl As Control, lbl As Control
        Dim iCtrl, i As Integer
        Dim strSizeArray() As String = New String(Panel1.Controls.Count)

        For iCtrl = 0 To Panel1.Controls.Count - 1
            ctrl = Panel1.Controls(iCtrl)
            If ctrl.GetType Is GetType(System.Windows.Forms.TextBox) Then
                strSizeArray(iCtrl) = (CType(ctrl, TextBox).Text)
            End If
        Next

The changed line is where I initialized strSizeArray. My VB.NET is scratchy at best, so hopefully that's the right syntax.

-ner

Thank you I have tried it yes it does work. Also another thing that i figured out even if i use the redim preserve statement within my code it still works.

Thank you once again
 
Back
Top