Results 1 to 3 of 3

Thread: [RESOLVED] Where can I initialize an array that can be accessed from the whole form?

  1. #1

    Thread Starter
    Lively Member
    Join Date
    Feb 2016
    Posts
    113

    Resolved [RESOLVED] Where can I initialize an array that can be accessed from the whole form?

    I have and array with several labels:
    Code:
    Dim Lbl_Car() As Label = {LBL_Car_1, Lbl_Car_2, Lbl_Car_3, Lbl_Car_4, Lbl_Car_5, Lbl_Car_6}
    I do this so I can loop through the labels and pull information from them or change them at will. The problem is that I have to initialize the array in the same sub that it is used in. I have tried to set it up in a separate sub and call it from other subs with no success. I have tried it at the top of the program with the rest of my dim statements with no luck. Where can I initialize the array once so it is available to the rest of the form?
    Thanks
    I don't program, I beat code into submission!!!

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

    Re: Where can I initialize an array that can be accessed from the whole form?

    Just like any other variable, if you want to access your array variable in multiple methods then you need to declare outside all methods, i.e. you need to declare a member variable rather than a local variable.
    vb.net Code:
    1. Private carLabels As Label()
    Just like a local variable, you can initialise a member variable where you declare it, e.g.
    vb.net Code:
    1. Private numbers As Integer() = {1, 2, 3, 4,5}
    2. Private carLabels As Label() = {carLabel1, carLabel2, carLabel3, carLabel4, carLabel5, carLabel6}
    This works in principle, but there's a problem. In the case of the numbers variable, you get an array containing six Integer values. In the case of the carLabels variable, you get an array containing six Label references BUT each of those references is Nothing. That's because the code that declares and initialises that variable is executed before the class constructor. The constructor for your form is where the InitializeComponent method is called and it is that method that creates all the controls and components on your form. If none of your controls have been created when that array is created, the array cannot contain those controls.

    The solution is to not initialise the array where it's declared but to do that later, after the controls have been created. The obvious choice for that is the Load event handler of the form, e.g.
    vb.net Code:
    1. Private carLabels As Label()
    2.  
    3. Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
    4.     carLabels = {carLabel1, carLabel2, carLabel3, carLabel4, carLabel5, carLabel6}
    5. End Sub
    Note that I have taken the liberty of replacing your horrible variable names. It's obviously up to you whether you choose to follow suit or continue using horrible names.

  3. #3

    Thread Starter
    Lively Member
    Join Date
    Feb 2016
    Posts
    113

    Re: Where can I initialize an array that can be accessed from the whole form?

    Thank you for your help. It was exactly what I needed. (I am so blessed you haven't seen the rest of my variables!!!)
    I don't program, I beat code into submission!!!

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