r/visualbasic • • May 06 '19

Global Variables Help

I’m working on small project for class right now, and I’m having nothing but issues getting global variables to work. What is the easiest way to do this? I have them declared on Form1 as Public and I’m trying to use them on Form2 in order to save a value, then use that value on Form1. Using VisualStudio.

Any help is greatly appreciated!

5 Upvotes

4 comments sorted by

3

u/ostreddit May 06 '19

If you want to make them in the form declare them as public at the top of your Form1 code window.

Public intScore as Integer

To access it from another Form:

Form1.intScore += 1

It would be better practice to put them in a module, but not necessary.

2

u/Allegiance10 May 06 '19

This solved my entire issue, thank you!

5

u/sa_sagan VB.Net Master May 06 '19

Put it in a module or class, not the form. I mean you can use the form, but preferred not to.

1

u/[deleted] May 16 '19

Public shared makes your variable global.