Question Global Object Arrays

hatsumi

New member
Joined
Mar 18, 2013
Messages
1
Programming Experience
1-3
Hi.
I have this array i need in many subs, but I don't know how to declare it outside, so I repeat it in almost every sub.
It looks like:
Dim kucice() As CheckBox = {CheckBox1, CheckBox2, CheckBox3, CheckBox4, CheckBox5, CheckBox6, CheckBox7, CheckBox8, CheckBox9, CheckBox10, CheckBox11, CheckBox12, CheckBox13, CheckBox14, CheckBox15, CheckBox16, CheckBox17, CheckBox18, CheckBox19, CheckBox20}


Why can't I declare it outside subs, and just use it later?
 
If you put the declaration of the variable outside all your methods then it is going to be executed when the form is created, before it is initialised. That means that none of your CheckBox controls will have been created and all those variables you use in the creation of the array will be Nothing. You can, and should, declare the variable outside all your methods so that it can be accessed from all of them. You can't actually create the array itself until later though, after the form has been initialised. The variable and the array object are two different things. In the Load event handler, create the array and assign it to the variable. By the time the Load event is raised, all the controls have been created and initialised.
 
Back
Top