473,396 Members | 1,916 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

What is the Difference between Global and Public, Dim and Private variables?

What is the Difference between Global and Public, Dim and Private variables? in Visual Basic 6.0. Because I am always using Public and Dim but using Global and Private is doing the same thing, is there any differences between these types of declaration scopes???????

Example:

What is the difference:
' In bas Module
Option Explicit

Global gblmyVar1 as Long ' Declared as Global
Public pblmyVar1 as Long ' Declared as Public

' In Form Module

Private prvRecSet as New ADODB.Recordset ' Declared as Private
Dim dimRecSet as New ADODB.Recordset ' Decalred as Dim

Please help, thanks in advance.
Jan 20 '07 #1
11 19701
willakawill
1,646 1GB
What is the Difference between Global and Public, Dim and Private variables? in Visual Basic 6.0. Because I am always using Public and Dim but using Global and Private is doing the same thing, is there any differences between these types of declaration scopes???????

Example:

What is the difference:
' In bas Module
Option Explicit

Global gblmyVar1 as Long ' Declared as Global
Public pblmyVar1 as Long ' Declared as Public

' In Form Module

Private prvRecSet as New ADODB.Recordset ' Declared as Private
Dim dimRecSet as New ADODB.Recordset ' Decalred as Dim

Please help, thanks in advance.
Hi
Global is not a vb keyword.
The MSDN library says:
Variables declared using the Public statement are available to all procedures in all modules in all applications unless Option Private Module is in effect; in which case, the variables are public only within the project in which they reside.

Caution The Public statement can't be used in a class module to declare a fixed-length string variable.

Use the Public statement to declare the data type of a variable. For example, the following statement declares a variable as an Integer:

Public NumberOfEmployees As Integer
Only use Public if you want your variable to have scope in your entire project.

As for dim:
Variables declared with Dim at the module level are available to all procedures within the module. At the procedure level, variables are available only within the procedure.

Use the Dim statement at module or procedure level to declare the data type of a variable. For example, the following statement declares a variable as an Integer.

Dim NumberOfEmployees As Integer
Jan 20 '07 #2
hariharanmca
1,977 1GB
What is the Difference between Global and Public, Dim and Private variables? in Visual Basic 6.0. Because I am always using Public and Dim but using Global and Private is doing the same thing, is there any differences between these types of declaration scopes???????

Example:

What is the difference:
' In bas Module
Option Explicit

Global gblmyVar1 as Long ' Declared as Global
Public pblmyVar1 as Long ' Declared as Public

' In Form Module

Private prvRecSet as New ADODB.Recordset ' Declared as Private
Dim dimRecSet as New ADODB.Recordset ' Decalred as Dim

Please help, thanks in advance.

Global???
Public - We can access that variable by Direct or Indirectly through all parts of Project
Private - Only a Pirticular part of Access
Dim - Just like Private and Initialize at the time of calling that part
Jan 20 '07 #3
Killer42
8,435 Expert 8TB
Global is not a vb keyword.
Ah, how soon we forget... :)

Global is a holdover from earlier versions, and I believe VB still accepts it for backward compatibility. I'm fairly certain it's treated the same as Public.
Jan 21 '07 #4
willakawill
1,646 1GB
Ah, how soon we forget... :)

Global is a holdover from earlier versions, and I believe VB still accepts it for backward compatibility. I'm fairly certain it's treated the same as Public.
I guess you did not try this out before posting :D
Jan 21 '07 #5
Killer42
8,435 Expert 8TB
I guess you did not try this out before posting :D
No, I didn't. But I've just tried it now, and it appears to support what I said. Why?
Jan 21 '07 #6
willakawill
1,646 1GB
No, I didn't. But I've just tried it now, and it appears to support what I said. Why?
My copy of VB6 refuses to let me use the global word. Access rejects it too. How about that.
Jan 22 '07 #7
Killer42
8,435 Expert 8TB
My copy of VB6 refuses to let me use the global word. Access rejects it too. How about that.
Hm... maybe it's some option I turned on for compatibility? Don't remember.

I assume this was in a module and not a form, right?

(I've just had a look through the options and can't see anything that looks relevant.)
Jan 22 '07 #8
I see I see, but you mentioned guys about Option Private Module, and I think this is the key to my answer, because in VB6 you are allowed to create multiple projects in just one Project File, Example: I have a project Named MultiProject, under that project I got 3 types of project like prj001,prj002 and prj003. So, what I think is if you will try to declare a variable in BAS Module with GLOBAL statement you can Access that variable in all of your 3 projects, So what do you think guys??? is that a probable answer to my question ?
Jan 22 '07 #9
About the Option Private Module, I just figured it out that If VB6 implement this kind of Option then I think that is the reason why there is a GLOBAL Statement, So if you will try to disable the Option Private module then Global statement is in effect and you can access your variables with GLOBAL statement in different types of your projects.

Thanks guys I appreciate all your help and Ideas, but if you think this is not the answer you can suggest another, please don't hesitate to post your answers...
Jan 22 '07 #10
Killer42
8,435 Expert 8TB
About the Option Private Module, I just figured it out that If VB6 implement this kind of Option then I think that is the reason why there is a GLOBAL Statement, So if you will try to disable the Option Private module then Global statement is in effect and you can access your variables with GLOBAL statement in different types of your projects.

Thanks guys I appreciate all your help and Ideas, but if you think this is not the answer you can suggest another, please don't hesitate to post your answers...
To be honest, I'm a bit lost.

I used to use Global in VB5 and earlier, but have since converted over to using Public. I still have code which says Global, because it was written in an earlier version. Both seem to work just fine, and I don't think I've heard of Option Private Module before.

The problem is, I've had very little experience with multiple projects. The only time I've worked with more than one project is when I'm creating a user control, in which case the "main" project is generally little more than a form with an instance of the control on it for testing. And I've only done this on rare occasions, as I can never find the time to play with this stuff.
Jan 22 '07 #11
Killer42 : I used to use Global in VB5 and earlier, but have since converted over to using Public. I still have code which says Global, because it was written in an earlier version. Both seem to work just fine, and I don't think I've heard of Option Private Module before.
  1. there are no more Global in after VB6.
  2. if your program is small and works well, you are right that public-private-global do the same thing. is just the range those variable will have.

here is a link. it summarize the use of global variable in VB.NET. works almost the same thant VB6. VB.NET removed the Global keyword and is replaced by a shared variable inside a class. Global Variables in Visual Basic .NET ; Everything you need to know about Global Variable for VB.NET in this single Post.

there is a simple Private-Public article i made. might give a hand: Public vs. Private
Nov 10 '12 #12

Sign in to post your reply or Sign up for a free account.

Similar topics

27
by: gabor | last post by:
hi, as far as i know in python there aren't any private (i mean not accessible from the outside of the object) methods/fields. why? in java/c++ i can make a method private, this way...
18
by: Janaka | last post by:
I'm having a discussion with my colleagues here on good programming standards. One thing we haven't agreed on is the use of properties in classes vs using member variables. Now everyone knows...
3
by: John C Kirk | last post by:
One odd thing I've come across - if you declare a private variable in a class, it is exposed to other instances of that same class. To replicate this behaviour, create a class like this: ...
1
by: Steve | last post by:
I generate C# webservices proxy code from WSDL file, it turns out the classes generated have public member variables and no getter/setter methods as follows, and I am able to get data when...
2
by: Rob Long | last post by:
Hi there Is there any way to access private variables directly from within a priviliged function? I have a situation where the priviliged function's execution context contains variables of the...
2
by: Sky | last post by:
Hello: I'm trying to make sense of snk files, when to use, under what conditions to regenerate new ones,...can someone take a look if these statemes make sense? And then the final questions at the...
6
by: t f | last post by:
Hi I have a class with a bunch of private variables in, is there an easy way to make these have public properties without having to type it all in? e.g. public class Fu { private float...
0
by: Jon | last post by:
Hello all, I recently posted a question about a tool that would create the public member variables for a selected set of private member variables - when there's 10 - 15 privates, it can be very...
13
by: PragueExpat | last post by:
I (think) that I've come up with a pattern that I haven't seen in any publications so far and I would like some feedback. Basically, I was looking for a way to inherit private functions and I came...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
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,...
0
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...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

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.