views:

58

answers:

2

Hi,

I have no of forms in VB 6. I want to access the value of one variable throughout the application.
What is the way to create global variable in VB 6.

EDIT: I want to create only one global variable. I am new to VB,So Please give me some code snippet

Thanks.

+3  A: 

Create a module file (a file with the extension .bas) and in the (Declarations) section place something like the following in it:-

 Public MyVariable As String
AnthonyWJones
+1. An equivalent syntax is `Global MyVariable As String`. Some people find that clearer, especially in COM-visible components, since the use of `Public` in a BAS module does not make the variable available outside the component, unlike in a class file.
MarkJ
@MarkJ: Yes `Global` would work but I find it a little too retro. I prefer to explicit mark members as `Private`, `Friend` or `Public`, if such a member is in the declarations section either in a .cls file or a .bas file both a are equally unavailable outside of the component. Hence IMO `Global` is a keyword surplass to requirements, had it not been in older forms of the language before the set of `Public`, `Friend` or `Private` were included I doubt would exist at all. (Hmm... showing my age a bit here.)
AnthonyWJones
I agree `Global` is retro, but I find the varied meanings of `Public` confusing though. In the declarations section of a .cls it has differing meanings depending on the instancing property of the class (the member can be available outside the component). I find using `Global` helps me a little to remember what is truly public and what isn't.
MarkJ
Global is so obsolete in VB6 that you won't even find it in the manual. It is only there to aid in porting very old source, such as VB3 programs.
Bob Riemersma
A: 

create a module and declare the variable there. Actually,you dont need to specify the Public keyword,things in module are by default treated as Public

Dim var1 as string

appusajeev
But then you've got to remember that the default is public __in a module__ its private in a __Class__. Its much better to be explicit about what your real accessibility intentions are. I never use `Dim` in the declaration section, I only use `Dim` for local variables.
AnthonyWJones
I agree with @AnthonyWJones here. Better to just be explicit, instead of trying to memorize VB6's default scoping rules. I also only use `Dim` for local variables.
Mike Spross