Jump to content
Xtreme .Net Talk

Recommended Posts

Posted

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

 

        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

  • *Experts*
Posted

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):

       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

"I want to stand as close to the edge as I can without going over. Out on the edge you see all the kinds of things you can't see from the center." - Kurt Vonnegut
Posted
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):

       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

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...