Scope of Public variables?

Hi Everyone

I am having difficulty understanding the meaning of the word Public when declaring variables.

Why is it that when I declare a variable to be Public inside a Form, another Form cannot access that variable?

Thank you.
Who is Participating?
Mike TomlinsonConnect With a Mentor Middle School Assistant TeacherCommented:
I have to type ... "Form.Variable"

Be careful...that only works if you are using the "default instance" of the form.  For instance, if Form1 is the "Startup Object" then using Form1.Variable would indeed get you the right value.

But if you displayed Form2 using the "New" keyword like this:

    Dim f2 As New Form2

Then using Form2.Variable would NOT get you the right value because that would access the default instance instead of the created instance that is actually being displayed.  To get the value from the instance of Form2 on the screen, you'd have to use "f2.Variable", or somehow get a reference to the actual instance of Form2 such as iterating over the Application.OpenForms() collection:
PagodNaUtakConnect With a Mentor Commented:
You can access it... Try the code below:

Public Class Form1

Public MyVariable As Integer = 2

End Class

'Now to access the variable of form1 in form2 simply to this:

Public Class Form2

Private Sub Form1_Load(ByVal sender As Object, _

Dim frm as New Form1


End Class

'Note:Form1 and Form2 should be running at the same time in order to access the variable of form1 in form2.

gamarrojgqConnect With a Mentor Commented:

If you declare a variable as Public you should be able to access it from another form as long as you have an instance of the form that have the public variable, so i in Form1 you have a public variable called strName, in Form2 you can access it like this

Dim fm1 as new Form1

Check this link to get a detail description
Keep up with what's happening at Experts Exchange!

Sign up to receive Decoded, a new monthly digest with product updates, feature release info, continuing education opportunities, and more.

Mike TomlinsonMiddle School Assistant TeacherCommented:
Both examples above are creating a new instance of Form1 inside Form2 so when you access the public value it's going to have the default value and not the value on the actual instance of Form1 as you would expect.

To get the correct value you need a reference to the actual instance of the form.  This can be achieved by passing a reference to Form1 into Form2 when you create it.
rd707Connect With a Mentor Commented:
What language are we talking here?

If it is a public variable in an object, typically you'll need to reference the variable via the object reference
If it is a static public variable in a object, typically you'll need to reference via the type name
If it is a public variable in a code module, sometimes you can use this without any sort of prefix or by prefixing with the module name

KarlTheHopelessAuthor Commented:
Now, I'm really confused!

Let me put it this way.

It seems that when I declare a Public variable in a Module (in Visual Basic) I can simply refer to this Variable by its name. Thus, I simply need to type ...  "Variable"

But when I declare a Public variable in a Form, I have to type ... "Form.Variable"


käµfm³d 👽Connect With a Mentor Commented:
>>  But when I declare a Public variable in a Form, I have to type ... "Form.Variable"

Modules are carried over from the VB6 days, where everything had more-or-less a global scope. In .NET, modules are actually compiled into classes under the hood. The VB6-functionality is still available (for now) so that those coming from VB6 can work more comfortably. If you are one of these people, you should make a conscious effort to try to use the more .NET-centric mechanisms (basically, object oriented programming).

The reason you have to say Form.Variable is because of how object oriented programming works--you have some objects (defined by a class), and those objects have members (variables and methods). In order to use/access these members, you must have a reference to an instance of a class (an object).

There are also static members (previously mentioned above). Static members are a bit different in that you don't call them from an instance--you call them using the class' name and then refer to the static member.
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.