Results 1 to 6 of 6

Thread: Global Control Array Problem

  1. #1

    Thread Starter
    Hyperactive Member
    Join Date
    Aug 2002
    Location
    Fox, OK
    Posts
    381

    Global Control Array Problem

    I tried this...

    Code:
    Public Class frmMain
        Dim pc() As CardPictureBox = {picCard0, picCard1, picCard2, picCard3, picCard4, picCard5, picCard6, picCard7, picCard8, picCard9, picCard10, picCard11, picCard12, picCard13, picCard14, picCard15, picCard16, picCard17, picCard18, picCard19, picCard20, picCard21, picCard22}
    and then this...

    Code:
        Private Sub StartNewGame()
            pc(1).Image = My.Resources.b1pr
    When I write the code at design time, I have no problems. I get intellisense for all my properties. Everything is good. But when I run the program, the .Image code creates a Null Error.

    I don't understand why because the following code (with the array inside the Sub) works just fine. If I remove the Dim pc() from the MakeControllsVisibleSub, I get the null error again.

    Code:
        Private Sub MakeControlsVisible()
            Dim pc() As CardPictureBox = {picCard0, picCard1, picCard2, picCard3, picCard4, picCard5, picCard6, picCard7, picCard8, picCard9, picCard10, picCard11, picCard12, picCard13, picCard14, picCard15, picCard16, picCard17, picCard18, picCard19, picCard20, picCard21, picCard22}
            For i As Integer = 0 To 22
                pc(i).Visible = True
            Next
        End Sub
    I want to Dim the pc() array one time only. I want it to be global. What am I doing wrong?

  2. #2
    Stack Overflow mod​erator
    Join Date
    May 2008
    Location
    British Columbia, Canada
    Posts
    2,824

    Re: Global Control Array Problem

    You need to use this:
    Code:
    Public Class frmMain
        Dim pc() As CardPictureBox
        Public Sub New()
            InitializeComponent()
            pc = {picCard0, picCard1, picCard2, picCard3, picCard4, picCard5, picCard6, picCard7, picCard8, picCard9, picCard10, picCard11, picCard12, picCard13, picCard14, picCard15, picCard16, picCard17, picCard18, picCard19, picCard20, picCard21, picCard22}
        End Sub

  3. #3

    Thread Starter
    Hyperactive Member
    Join Date
    Aug 2002
    Location
    Fox, OK
    Posts
    381

    Re: Global Control Array Problem

    I don't understand WHY it works but at leas it does.

    Can you explain what this does?

  4. #4
    Stack Overflow mod​erator
    Join Date
    May 2008
    Location
    British Columbia, Canada
    Posts
    2,824

    Re: Global Control Array Problem

    The control variables (picCard0...22) aren't initialized until InitializeComponent() is called. If you assign them there, you have an array filled with Nothing.

  5. #5

    Thread Starter
    Hyperactive Member
    Join Date
    Aug 2002
    Location
    Fox, OK
    Posts
    381

    Re: Global Control Array Problem

    What happens when I try to create another array now. For example 22 timer controls? I assume they must me instanciated in the same way but I've already used the New word. Does the program get more confused or just me?

  6. #6
    Stack Overflow mod​erator
    Join Date
    May 2008
    Location
    British Columbia, Canada
    Posts
    2,824

    Re: Global Control Array Problem

    Code:
    Public Class frmMain
        Dim pc() As CardPictureBox
        Dim my_other_control_array() As Control
        Public Sub New()
            InitializeComponent()
            pc = New CardPictureBox() {picCard0, picCard1, picCard2, picCard3, picCard4, picCard5, picCard6, picCard7, picCard8, picCard9, picCard10, picCard11, picCard12, picCard13, picCard14, picCard15, picCard16, picCard17, picCard18, picCard19, picCard20, picCard21, picCard22}
            my_other_control_array = New Control() {control1,control2,control3,etc.}
        End Sub

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