Global Arrays in VB.Net

Permit2kill

Member
Joined
Jun 25, 2007
Messages
21
Programming Experience
Beginner
Hey guys,
I have a question regarding global arrays in VB.Net.

Basically I've got an Array that I need to use in two different Sub procedures. One handles a button click, one handles a timer tick.

I've tried setting my array up so it's global. In theory I was hoping I would be able to edit the values of this array in my button click event and use the values at a later date in the timer tick.

However, when I go to use the values for some reason "Nothing" is stored in the array when I go to use it in my Timer Tick Event.

In the past I've had the same error but have been able to solve it by declaring the array locally and passing it to the sub procedures I needed it in. As far as I know I'm not able to do that in this case :(

Any help would be much appreciated, and I hope I explained my problem clearly enough for you guys.
-Permit
 
First of all, the term "global" is usually used to refer to variables that are throughout the entire project. In VB this usually means they are declared in a module. If you just want a variable accessible in more than one method in the same class then that's not global. That is just a member variable, or you could say a class-level variable rather than a local variable.

As to your question, if you have a member variable that you set in one method and then access in another, you will get back the value in the second method that you set in the first. If that's not happening for you then you're just not doing it right. In order to determine what you're doing wrong we would need to see your code. Please: the relevant code, all the relevant code and nothing but the relevant code.
 
Sorry for the misuse of terms. That had just been the way I'd been taught to use them.

Anyways here's the code in question.

-I declare Players(1) using the structure Player
-I save the information entered by the user in two different textfiles.
-I want to use this variable in my second button click event but nothing is returned.
 
Last edited:
Rid the local "Dim Players(20) As Player", it is used locally instead of your class field "Dim Players(1) As Player"
 
Back
Top