Solved

Scope of Public variables?

Posted on 2011-03-07
7
496 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
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 
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 74

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

Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
Code enhancement 5 13
Form design in vb.net 7 20
VB.Net - CSV to Oracle table 4 30
Adoquery sql  left join does not work 25 49
Entering a date in Microsoft Access can be tricky. A typo can cause month and day to be shuffled, entering the day only causes an error, as does entering, say, day 31 in June. This article shows how an inputmask supported by code can help the user a…
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …
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…

762 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

Need Help in Real-Time?

Connect with top rated Experts

17 Experts available now in Live!

Get 1:1 Help Now