473,569 Members | 2,572 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Declaring A Variable

In the Declarations section of a form's code module, what is the difference
between the following:
Dim Flag As Boolean

Public Flag As Boolean

Private Flag As Boolean

Thanks!

Lucy
Nov 13 '05 #1
4 6288
"Lucy" <lg*****@earthl ink.net> wrote in message
news:oN******** *********@newsr ead2.news.atl.e arthlink.net...
In the Declarations section of a form's code module, what is the difference between the following: Dim Flag As Boolean
Private Flag As Boolean Either of these two declarations creates a static instance of the Boolean
variable "Flag" which is visible only to the form and not to the rest of the
application.
Public Flag As Boolean

This declaration creates a static instance of the Boolean variable "Flag"
which is visible to the entire application. For example, if you have a form
named "Form1" with the declaration statement "Public Flag As Boolean", the
"Flag" variable can be enumerated thus:
?Forms("Form1") .Flag

NOTE: The form must be open for this variable to be available.

Hope this helps.
Nov 13 '05 #2
Thanks for the quick response!

Ok, I see the difference between Public and the other two. What is the
difference between Dim Flag and Private Flag? Is there a time when to use
one and not the other?

Lucy
"ByteMyzer" <sb*@nospam.new s.chi.sbcglobal .net> wrote in message
news:Jv******** *********@newss vr33.news.prodi gy.com...
"Lucy" <lg*****@earthl ink.net> wrote in message
news:oN******** *********@newsr ead2.news.atl.e arthlink.net...
In the Declarations section of a form's code module, what is the difference
between the following:

Dim Flag As Boolean
Private Flag As Boolean

Either of these two declarations creates a static instance of the Boolean
variable "Flag" which is visible only to the form and not to the rest of

the application.
Public Flag As Boolean This declaration creates a static instance of the Boolean variable "Flag"
which is visible to the entire application. For example, if you have a

form named "Form1" with the declaration statement "Public Flag As Boolean", the
"Flag" variable can be enumerated thus:
?Forms("Form1") .Flag

NOTE: The form must be open for this variable to be available.

Hope this helps.

Nov 13 '05 #3
"Lucy" <lg*****@earthl ink.net> wrote in message
news:rY******** *********@newsr ead2.news.atl.e arthlink.net...
Thanks for the quick response!

Ok, I see the difference between Public and the other two. What is the
difference between Dim Flag and Private Flag? Is there a time when to use
one and not the other?

Lucy


As far as the declarations section of a code module are concerned, there is
significantly no difference between the Dim statement and the Private
statement. In the declarations section they serve the same purpose exactly
the same way.

However, the Private statement may only be used in the declarations section
of a module, whereas the Dim statement may be used to dynamically allocate a
variable within a procedure, in which case, the variable is only visible to
that procedure, and to no other part of the Module.
Nov 13 '05 #4
Lucy wrote:
Thanks for the quick response!

Ok, I see the difference between Public and the other two. What is the
difference between Dim Flag and Private Flag? Is there a time when to use
one and not the other?


(for short answer, skip to last para)

Back in the "good old days" Dim was used to "dimension" an array, it
still is but back then it was the only thing it was used for on BASIC,
other intrinsic variables were not declared at all.

With the advent of QuickBASIC (in the MS stable at least) we started
seeing the of "Dim" to declare variables, since "Dim" is short for
"Dimension" and intrinsic variables don't have dimensions (arrays do)
this was a pretty silly thing to use, but use it they did.

A lack of planning and/or slap happy ad-hoc development lead us down the
road of having global or shared variables in BASIC by using "Dim Shared"
in the module level to share a variable with all subs and functions,
"Shared" as a declaration in a sub/function to share it with the module
level code (entry point was at module level, no sub main back then) and
"common" to share across multiple modules although that declaration had
to exist in all modules that would see that variable. To have it in all
modules the use of an include file was quite slick.

The use of "Shared" seemed to disappear about the time of VB, probably
because of the way VB now used Sub Main or a form event as the entry
point to the program rather than the old fasioned way of module level
code and the use of "Dim" at the module level replaced "Dim Shared" and
"Global" replaced and enhanced "Common" as only one declaration was
required. (b4, "Dim" at the module level would make the variable
available to module level code only, not the subs and functions)

Later versions of VB started using "Private" and "Public" to distinguish
between module wide and global variables, this makes the module wide
declaration more explicit than "Dim", which apart from it's original use
to dimension an array, is there for backward compatability for intrinsic
variables.

--

\\\\\\
\\ \\ Windows is searching
\ \ For your sig.
\ \ Please Wait.
\__\

Nov 13 '05 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
2129
by: Oliver Corona | last post by:
I am wondering if anyone has any insights on the performance benefit (or detriment) of declaring local variables instead of referencing members. Is allocating memory for a new variable more efficient than repeatedly referencing the member in a loop? Maybe using a string isn't the best example, but hopefully you get the idea! * example...
29
4025
by: Friday | last post by:
Sorry if this is the wrong group. I tried to find the one I thought would be most relevant. I'm an old PHP guy, who knows little about asp and NOTHING about asp.net, but need to learn at least enough to convert a favorite PHP script to work on an ASP.NET site. I'm experimenting with simple example scripts that will help me learn how to...
2
53860
by: ross.oneill | last post by:
Hi, I am having trouble with a simple task of declaring a variable. Is this possible? Here is what I want to do. DECLARE start_date date; DECLARE end_date date; SET start_date = '2005-01-01'
6
1865
by: Steve Jorgensen | last post by:
Many of the regulars here have explained that declaring variables using As New .... is a bad idea, and some have given some good explanations, but I wanted add one more demonstration to the mix. This example is less practical than some, but more illustrative than most. I came up with this to show a fellow programmer why I think he's using As...
1
2860
by: Maarten Terlingen | last post by:
Sample: ClassA: ClassB ClassB: ClassC ClassA x = new Class C I want to know the declaring type of C. x.GetType() returns C x.GetType().BaseType returns B x.GetType().BaseType.BaseType returns A, but I don't know the depth in my
5
3141
by: Brett | last post by:
In a class, I have several Private subs. I declare an instance of the class such as: Dim MySelf as new Class1 within a private sub. The motive is to provide access to other subs within the same class. Is this correct? It would be nice to declare the MySelf instance in the Class public space (just under Private Class Class1). That...
6
3500
by: Mark A. Sam | last post by:
Hello, I am using Visual Web Developer 2005 Express. I want to declare a varible, using Visual Basic as the language and can't get anywhere. For example Public Test1 as String I'll get en error saying to change Public to Dim. When I do, it will say that Test1 is an unused local variable.
8
5844
by: rendle | last post by:
I have a MSIL/performance question: Is there any difference between declaring a variable once and assigning to it multiple times, and declaring and assigning multiple times? For example: // Begin sample for(int i = 0; i < 100; i++) {
8
7500
by: SM | last post by:
I've always wonder if there is diference when declaring and initializing a varible inside/outside a loop. What's a better practice? Declaring and initializing variables inside a loop routine, like this: for(var i=0; i<list; i++) { var name = list; }
6
2972
by: =?Utf-8?B?QUw=?= | last post by:
Hi I usually stick to the convention of not declaring variables in my bodies of "loops" (including foreach) ie int x; for (int i = 0; i < 10; i++) {
0
7700
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
0
7614
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language...
0
7924
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. ...
0
8125
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
1
5513
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes...
0
5219
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
0
3642
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2114
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
1
1221
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.