-
Jan 20th, 2025, 11:32 AM
#1
Thread Starter
Fanatic Member
[RESOLVED] instigating an array of label
Hello I would like to instigate an array of label. I've tried this
Code:
Dim label() As Label = New Label() {Label1, Label2, Label3, Label4, Label5}
while it does not generate an error it also does not work.
Thanks in advance
-
Jan 20th, 2025, 11:52 AM
#2
Re: instigating an array of label
Drop the New Label() after the assignment:
Code:
Dim label() As Label = {Label1, Label2, Label3, Label4, Label5}
-
Jan 20th, 2025, 12:00 PM
#3
Thread Starter
Fanatic Member
Re: instigating an array of label
Yes thank you it works now.
-
Jan 20th, 2025, 12:14 PM
#4
Thread Starter
Fanatic Member
Re: instigating an array of label
@ dday I get and error stating I need instigate the label1.backcolor
-
Jan 20th, 2025, 02:43 PM
#5
Re: instigating an array of label
FordPerfect's solution creates an array of new labels that are created at runtime whereas my solution creates an array of labels that are defined at design time (though ultimately created at runtime when the form is created).
I suspect the error you're getting in post #5 is not related to defining the array I posted unless you're trying to reference a label that doesn't yet exist. Regardless of that's the case, could you post the line and exact error message you're getting?
-
Jan 20th, 2025, 03:22 PM
#6
Thread Starter
Fanatic Member
Re: instigating an array of label
Code:
For x = 0 To 4
label(x).BackColor = Color.FromKnownColor(KnownColor.ControlLight)
Next
nullreferenceexception was unhandled
Object reference not set to an instance of an object.
use the "new" keyword to create an object instance
-
Jan 20th, 2025, 03:34 PM
#7
Re: instigating an array of label
That is because you aren't iterating over your label array. Instead, you're iterating 5 times (0 - 4) and trying to access a property of the Label class definition.
Instead, loop over the array using a For/Each loop:
Code:
Dim labels() As Label = {Label1, Label2, Label3, Label4, Label5}
For Each item As Label In labels
item.BackColor = Color.FromKnownColor(KnownColor.ControlLight)
Next
Edit - Actually, I don't know what label(x) would try to do under the hood if you aren't accessing a reference of an object. Since Label is a class name and VB.NET is case insensitive, label would resolve to Label, and then try to call a method on a class definition resulting in a compilation error.
-
Jan 20th, 2025, 04:08 PM
#8
Thread Starter
Fanatic Member
Re: instigating an array of label
Well I've seen how fordperfect did it and it works.
I've seen how dday did it and it works
here is how I did it
Code:
Dim label() As Label = {Label1, Label2, Label3, Label4, Label5}
label(0) = Label1
label(1) = Label2
label(2) = Label3
label(3) = Label4
label(4) = Label5
For x = 0 To 4
label(x).BackColor = Color.FromKnownColor(KnownColor.ControlLight)
Next
and tis also works
-
Jan 20th, 2025, 04:13 PM
#9
Thread Starter
Fanatic Member
Re: instigating an array of label
the dday for each loop does not work
Last edited by georgesutfin; Jan 20th, 2025 at 04:27 PM.
-
Jan 20th, 2025, 04:41 PM
#10
Re: instigating an array of label
You REALLY shouldn't be naming that array label. VB shouldn't even be allowing it, but VB was designed to allow some things that it really should not allow. That's one of them. I would have thought this would cause an error, since a label is an object type, so you have a variable with the same name as a type. VB allows this in the case of forms, as that's how default instances work. I didn't realize VB would ALSO allow that for other types, though I guess it does resolve some interesting name collision problems. Still, you shouldn't knowingly try for a name collision. That's just playing chicken with the compiler in the hopes that it will do the right thing.
My usual boring signature: Nothing
 
-
Jan 20th, 2025, 04:45 PM
#11
Re: instigating an array of label
 Originally Posted by georgesutfin
the dday for each loop does not work
A minimum reproducible example I created suggests otherwise.
Attached is a picture of me copying and pasting the code in a barebones project, only changing the backcolor to aquamarine for higher contrast in the screenshot.
-
Jan 20th, 2025, 04:46 PM
#12
Thread Starter
Fanatic Member
Re: instigating an array of label
I've changed the name. No change in results
-
Jan 20th, 2025, 05:27 PM
#13
Thread Starter
Fanatic Member
Re: instigating an array of label
yes your code works! Why doesn't this
Code:
Public Class Form1
dim thislabel() As Label = {Label1, Label2, Label3, Label4, Label5}
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
For Each item As Label In thislabel
item.BackColor = Color.Green
Next
End Sub
End Class
-
Jan 20th, 2025, 05:52 PM
#14
Re: instigating an array of label
My guess is because you've assigned reference to the array before the form has been initialized, in other words the controls have not been created yet. Because if you assign the variable after the form has been initialized, it works.

Edit - Notice that in my original example I'm using the Form's Load event. So, the difference between my original example and your follow up is that in my example the controls have been created whereas in your example they haven't been.
Last edited by dday9; Jan 20th, 2025 at 06:03 PM.
-
Jan 20th, 2025, 06:03 PM
#15
Thread Starter
Fanatic Member
Re: instigating an array of label
dday9 thank you for all your help and time.
-
Jan 20th, 2025, 09:45 PM
#16
Re: [RESOLVED] instigating an array of label
That's correct. When you put the creation of the array outside of the methods in the form, when does that happen?
If you put a breakpoint in the first line of the constructor you will see that the line that creates and populates the array happens before the first line of the constructor is run. However, the first line of the constructor is going to be a call to InializeComponents, and it is THAT method that creates the actual labels. In other words, you are creating the array before the controls you are putting into the array have been created. What are reference type variables? They are not the actual objects (in this case, they are not the labels themselves), they are just references to the actual objects, which will be sitting somewhere out in memory. Since the objects have not been created yet, the variables hold Nothing, so Nothing is what you are putting into the array.
You might be thinking that the variable is an empty vessel that will later be filled with a reference to an actual control, and that putting the empty vessels into the array is all you are doing, but that wouldn't be correct. Such a thing COULD be done, but that would mean having a reference to the variable, and that's not what is happening...nor should it, because that would get mighty confusing. What ends up in the array is what the variable holds, and what the variable holds is Nothing at the time you create the array, so you have an array of Nothing.
By populating the array of controls AFTER the call to InitializeComponents, the objects will have been created, so the variables will hold the references of the actual labels and therefore it will be the references that will be put into the array.
My usual boring signature: Nothing
 
-
Jan 20th, 2025, 11:43 PM
#17
Re: [RESOLVED] instigating an array of label
If you want your array to be available throughout any Sub (or Function) in Form1...
Code:
Public Class Form1
Dim labels() As Label
Private Sub Form1_Load(ByVal sender As Object, ByVal e As EventArgs) Handles MyBase.Load
labels = New Label() {Label1, Label2, Label3, Label4, Label5}
End Sub
End Class
- Coding Examples:
- Features:
- Online Games:
- Compiled Games:
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
|