Solved

Scope of Public variables?

Posted on 2011-03-07
7
500 Views
Last Modified: 2012-05-11
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.
0
Comment
Question by:KarlTheHopeless
7 Comments
 
LVL 8

Assisted Solution

by:PagodNaUtak
PagodNaUtak earned 50 total points
ID: 35063545
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

Messagebox.Show(frm.Variable.ToString())

End Class

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







0
 
LVL 13

Assisted Solution

by:gamarrojgq
gamarrojgq earned 50 total points
ID: 35063571
Hi,

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
msgbox(fm1.strName)

Check this link to get a detail description

http://www.andreavb.com/forum/viewtopic_3361.html
0
 
LVL 85

Expert Comment

by:Mike Tomlinson
ID: 35065789
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.
0
Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

 
LVL 4

Assisted Solution

by:rd707
rd707 earned 100 total points
ID: 35071587
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

0
 

Author Comment

by:KarlTheHopeless
ID: 35076114
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"

WHY?

0
 
LVL 85

Accepted Solution

by:
Mike Tomlinson earned 200 total points
ID: 35076975
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
    f2.Show()

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:
http://msdn.microsoft.com/en-us/library/system.windows.forms.application.openforms.aspx
0
 
LVL 75

Assisted Solution

by:käµfm³d 👽
käµfm³d   👽 earned 100 total points
ID: 35089104
>>  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.
0

Featured Post

Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Does the idea of dealing with bits scare or confuse you? Does it seem like a waste of time in an age where we all have terabytes of storage? If so, you're missing out on one of the core tools in every professional programmer's toolbox. Learn how to …
If you’re thinking to yourself “That description sounds a lot like two people doing the work that one could accomplish,” you’re not alone.
An introduction to basic programming syntax in Java by creating a simple program. Viewers can follow the tutorial as they create their first class in Java. Definitions and explanations about each element are given to help prepare viewers for future …
In this fourth video of the Xpdf series, we discuss and demonstrate the PDFinfo utility, which retrieves the contents of a PDF's Info Dictionary, as well as some other information, including the page count. We show how to isolate the page count in a…

803 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question