Results 1 to 5 of 5

Thread: [RESOLVED] [2005] Arrays of controls?

  1. #1

    Thread Starter
    Banned timeshifter's Avatar
    Join Date
    Mar 2004
    Location
    at my desk
    Posts
    2,465

    Resolved [RESOLVED] [2005] Arrays of controls?

    Ok, so I've never needed to do this before, and this particular project it seems like a good idea. So I'm trying it. I need an array of labels.

    Right now, I have the following:
    Code:
        Dim playerLabel() As Label = {lblP1, lblP2, lblP3, lblP4, lblP5, lblP6, lblP7, lblP8}
    where lblP1....lblP8 are all on the form already. Wonderful.

    Except when I run a For Each l As Label In playerLabel a bit later on in the code, it blows up with 'Object reference not set to an instance of an object.'

    Is there anything fancy that needs to happen that I'm unaware of with this procedure?

  2. #2
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: [2005] Arrays of controls?

    Let me guess... you don't have that code inside any method body, correct? When an object is created all the member variable declarations are executed first, before the body of the constructor. It is in the body of the constructor that a form calls InitializeComponent, which is where all the controls are created. If you have that line of code outside any method then that means that it's being executed before those Labels are created, so you're filling your array with null references. If you're declaring that array as member variable then you still need to create the array object after the InitializeComponent call, so either at the end of the constructor or in the Load event handler.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  3. #3

    Thread Starter
    Banned timeshifter's Avatar
    Join Date
    Mar 2004
    Location
    at my desk
    Posts
    2,465

    Re: [2005] Arrays of controls?

    Ok... you kinda lost me there... what specifically am I doing wrong, and how specifically do I fix it?

  4. #4
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: [2005] Arrays of controls?

    Sometimes I think maybe my posts get translated into another langauge.
    Quote Originally Posted by jmcilhinney
    create the array object after the InitializeComponent call, so either at the end of the constructor or in the Load event handler.
    Is that not plain? In case it's not, I'm guessing that you're doing like this:
    vb.net Code:
    1. Public Class Form1
    2.  
    3.     Private labels As Label() = {Me.Label1, Me.Label2}
    4.  
    5.     Public Sub New()
    6.         ' This call is required by the Windows Form Designer.
    7.         InitializeComponent()
    8.  
    9.         ' Add any initialization after the InitializeComponent() call.
    10.  
    11.     End Sub
    12.  
    13.     Private Sub Form1_Load(ByVal sender As System.Object, _
    14.                            ByVal e As System.EventArgs) Handles MyBase.Load
    15.  
    16.     End Sub
    17.  
    18. End Class
    when you should be doing like this:
    vb.net Code:
    1. Public Class Form1
    2.  
    3.     Private labels As Label()
    4.  
    5.     Public Sub New()
    6.         ' This call is required by the Windows Form Designer.
    7.         InitializeComponent()
    8.  
    9.         ' Add any initialization after the InitializeComponent() call.
    10.  
    11.         Me.labels = New Label() {Me.Label1, Me.Label2}
    12.     End Sub
    13.  
    14.     Private Sub Form1_Load(ByVal sender As System.Object, _
    15.                            ByVal e As System.EventArgs) Handles MyBase.Load
    16.  
    17.     End Sub
    18.  
    19. End Class
    or this:
    vb.net Code:
    1. Public Class Form1
    2.  
    3.     Private labels As Label()
    4.  
    5.     Public Sub New()
    6.         ' This call is required by the Windows Form Designer.
    7.         InitializeComponent()
    8.  
    9.         ' Add any initialization after the InitializeComponent() call.
    10.  
    11.     End Sub
    12.  
    13.     Private Sub Form1_Load(ByVal sender As System.Object, _
    14.                            ByVal e As System.EventArgs) Handles MyBase.Load
    15.         Me.labels = New Label() {Me.Label1, Me.Label2}
    16.     End Sub
    17.  
    18. End Class
    Exactly as I quoted.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  5. #5

    Thread Starter
    Banned timeshifter's Avatar
    Join Date
    Mar 2004
    Location
    at my desk
    Posts
    2,465

    Re: [2005] Arrays of controls?

    And suddenly it seems to work correctly. Again, pardon my ignorance. There seem to be a lot of "basics" i kinda skipped past as I learned. Thanks for tolerating it, though!

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width