|
-
Aug 28th, 2007, 06:01 PM
#1
[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?
-
Aug 28th, 2007, 06:08 PM
#2
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.
-
Aug 28th, 2007, 06:12 PM
#3
Re: [2005] Arrays of controls?
Ok... you kinda lost me there... what specifically am I doing wrong, and how specifically do I fix it?
-
Aug 28th, 2007, 06:21 PM
#4
Re: [2005] Arrays of controls?
Sometimes I think maybe my posts get translated into another langauge.
 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:
Public Class Form1
Private labels As Label() = {Me.Label1, Me.Label2}
Public Sub New()
' This call is required by the Windows Form Designer.
InitializeComponent()
' Add any initialization after the InitializeComponent() call.
End Sub
Private Sub Form1_Load(ByVal sender As System.Object, _
ByVal e As System.EventArgs) Handles MyBase.Load
End Sub
End Class
when you should be doing like this:
vb.net Code:
Public Class Form1
Private labels As Label()
Public Sub New()
' This call is required by the Windows Form Designer.
InitializeComponent()
' Add any initialization after the InitializeComponent() call.
Me.labels = New Label() {Me.Label1, Me.Label2}
End Sub
Private Sub Form1_Load(ByVal sender As System.Object, _
ByVal e As System.EventArgs) Handles MyBase.Load
End Sub
End Class
or this:
vb.net Code:
Public Class Form1
Private labels As Label()
Public Sub New()
' This call is required by the Windows Form Designer.
InitializeComponent()
' Add any initialization after the InitializeComponent() call.
End Sub
Private Sub Form1_Load(ByVal sender As System.Object, _
ByVal e As System.EventArgs) Handles MyBase.Load
Me.labels = New Label() {Me.Label1, Me.Label2}
End Sub
End Class
Exactly as I quoted.
-
Aug 28th, 2007, 06:27 PM
#5
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|