Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

Scope of Public variables?

Posted on 2011-03-07
7
Medium Priority
?
507 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
7 Comments
 
LVL 8

Assisted Solution

by:PagodNaUtak
PagodNaUtak earned 200 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 200 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 86

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
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 4

Assisted Solution

by:rd707
rd707 earned 400 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 86

Accepted Solution

by:
Mike Tomlinson earned 800 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 400 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

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

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.
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
In this fifth video of the Xpdf series, we discuss and demonstrate the PDFdetach utility, which is able to list and, more importantly, extract attachments that are embedded in PDF files. It does this via a command line interface, making it suitable …
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…
Suggested Courses

610 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