Results 1 to 17 of 17

Thread: [RESOLVED] instigating an array of label

  1. #1

    Thread Starter
    Fanatic Member
    Join Date
    Jan 2013
    Posts
    534

    Resolved [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

  2. #2
    Super Moderator dday9's Avatar
    Join Date
    Mar 2011
    Location
    South Louisiana
    Posts
    12,062

    Re: instigating an array of label

    Drop the New Label() after the assignment:
    Code:
     Dim label() As Label = {Label1, Label2, Label3, Label4, Label5}
    "Code is like humor. When you have to explain it, it is bad." - Cory House
    VbLessons | Code Tags | Sword of Fury - Jameram

  3. #3

    Thread Starter
    Fanatic Member
    Join Date
    Jan 2013
    Posts
    534

    Re: instigating an array of label

    Yes thank you it works now.

  4. #4

    Thread Starter
    Fanatic Member
    Join Date
    Jan 2013
    Posts
    534

    Re: instigating an array of label

    @ dday I get and error stating I need instigate the label1.backcolor

  5. #5
    Super Moderator dday9's Avatar
    Join Date
    Mar 2011
    Location
    South Louisiana
    Posts
    12,062

    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?
    "Code is like humor. When you have to explain it, it is bad." - Cory House
    VbLessons | Code Tags | Sword of Fury - Jameram

  6. #6

    Thread Starter
    Fanatic Member
    Join Date
    Jan 2013
    Posts
    534

    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

  7. #7
    Super Moderator dday9's Avatar
    Join Date
    Mar 2011
    Location
    South Louisiana
    Posts
    12,062

    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.
    "Code is like humor. When you have to explain it, it is bad." - Cory House
    VbLessons | Code Tags | Sword of Fury - Jameram

  8. #8

    Thread Starter
    Fanatic Member
    Join Date
    Jan 2013
    Posts
    534

    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

  9. #9

    Thread Starter
    Fanatic Member
    Join Date
    Jan 2013
    Posts
    534

    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.

  10. #10
    Super Moderator Shaggy Hiker's Avatar
    Join Date
    Aug 2002
    Location
    Idaho
    Posts
    39,747

    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

  11. #11
    Super Moderator dday9's Avatar
    Join Date
    Mar 2011
    Location
    South Louisiana
    Posts
    12,062

    Re: instigating an array of label

    Quote Originally Posted by georgesutfin View Post
    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.
    Name:  20250120344.jpg
Views: 172
Size:  21.0 KB
    "Code is like humor. When you have to explain it, it is bad." - Cory House
    VbLessons | Code Tags | Sword of Fury - Jameram

  12. #12

    Thread Starter
    Fanatic Member
    Join Date
    Jan 2013
    Posts
    534

    Re: instigating an array of label

    I've changed the name. No change in results

  13. #13

    Thread Starter
    Fanatic Member
    Join Date
    Jan 2013
    Posts
    534

    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

  14. #14
    Super Moderator dday9's Avatar
    Join Date
    Mar 2011
    Location
    South Louisiana
    Posts
    12,062

    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.
    Name:  20250120452.jpg
Views: 162
Size:  29.9 KB

    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.
    "Code is like humor. When you have to explain it, it is bad." - Cory House
    VbLessons | Code Tags | Sword of Fury - Jameram

  15. #15

    Thread Starter
    Fanatic Member
    Join Date
    Jan 2013
    Posts
    534

    Re: instigating an array of label

    dday9 thank you for all your help and time.

  16. #16
    Super Moderator Shaggy Hiker's Avatar
    Join Date
    Aug 2002
    Location
    Idaho
    Posts
    39,747

    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

  17. #17
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    25,944

    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

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