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?
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
Re: Global Control Array Problem
I don't understand WHY it works but at leas it does.
Can you explain what this does?
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.
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?
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