Newbie Question: Variables 
Author Message
 Newbie Question: Variables

This may be a simple question, but I can't seem to find the answer anywhere.
How do I make a variable declared in one form be available in another? I
checked one website and it said to do the following:
DIM Public VariableName As Integer
When I type this in, it gives me an error and requires an identifier. I need
to do this for my project at school. Does anyone know the answer?
George


Tue, 11 May 2004 01:30:00 GMT  
 Newbie Question: Variables
Add a module to a project and use either of these two lines to declare it
(in the module only):

Public VariableName As Integer
Global VariableName As Integer

        -Andy (CEO, Dynamic Bits)
         http://dynamicbits.com


Quote:
> This may be a simple question, but I can't seem to find the answer
anywhere.
> How do I make a variable declared in one form be available in another? I
> checked one website and it said to do the following:
> DIM Public VariableName As Integer
> When I type this in, it gives me an error and requires an identifier. I
need
> to do this for my project at school. Does anyone know the answer?



Tue, 11 May 2004 02:21:36 GMT  
 Newbie Question: Variables
Thanks for the help. Is there a difference between Public and Global?
George


Quote:
> Add a module to a project and use either of these two lines to declare it
> (in the module only):

> Public VariableName As Integer
> Global VariableName As Integer

>         -Andy (CEO, Dynamic Bits)
>          http://dynamicbits.com



> > This may be a simple question, but I can't seem to find the answer
> anywhere.
> > How do I make a variable declared in one form be available in another? I
> > checked one website and it said to do the following:
> > DIM Public VariableName As Integer
> > When I type this in, it gives me an error and requires an identifier. I
> need
> > to do this for my project at school. Does anyone know the answer?



Tue, 11 May 2004 02:26:41 GMT  
 Newbie Question: Variables
"George" wrote

 > This may be a simple question, but I can't seem to find the answer
anywhere.
 > How do I make a variable declared in one form be available in another? I
 > checked one website and it said to do the following:
 > DIM Public VariableName As Integer
 > When I type this in, it gives me an error and requires an identifier. I
need
 > to do this for my project at school. Does anyone know the answer?

In the Form's module (it is, in fact, a class module, but that's not
important here), define your variable in the General, Declarations section
as:

    Public VariableName as Integer

Note that when you declare a Public Variable, you do not need the Dim. Note
also that multiple forms can have a Public Variable of the same name; that
is because when you refer to them from outside the form, you refer to
FormName.VariableName, so there is no confusion.



Tue, 11 May 2004 02:40:26 GMT  
 Newbie Question: Variables


Quote:
> Thanks for the help. Is there a difference between Public and Global?
> George


> > Add a module to a project and use either of these two lines to declare
it
> > (in the module only):

> > Public VariableName As Integer
> > Global VariableName As Integer

> >         -Andy (CEO, Dynamic Bits)
> >          http://dynamicbits.com

<snipped />

No, Global is still valid from older versions of VB,
and is there for compatibility-reasons. YOu should use
Public.

BUT! To Andy's answer: You do not need to add a module to your
project, it is enough to declare it Public in your form.

When you refer to it from your second form, you prefix it
with the first forms name, like this (in Form2)

    ...
    Form1.VariableName = 15
    ' or
    myLocalVar = Form1.VareiableName

The OO-way (and in my opinion proper way) of doing it is:

In Form1:

Private m_VariableName As Integer
...
Public Property Let VariableName(Byval n As Integer)
    m_VariableName = n
End Property

Public Property Get VariableName As Integer
    VariableName = m_VariableName
End Property

In Form2:

Just like in the first example.

Dag.



Tue, 11 May 2004 06:13:03 GMT  
 Newbie Question: Variables
You have to be a bit careful with that, Dag, because referencing a variable
in another Form will cause that Form to load if it is not already loaded
which will, of course, cause its Load event to trigger. This can often cause
problems, depending on exactly what you are doing in the Load event.

Mike


Quote:



> > Thanks for the help. Is there a difference between Public and Global?
> > George


> > > Add a module to a project and use either of these two lines to declare
> it
> > > (in the module only):

> > > Public VariableName As Integer
> > > Global VariableName As Integer

> > >         -Andy (CEO, Dynamic Bits)
> > >          http://dynamicbits.com

> <snipped />

> No, Global is still valid from older versions of VB,
> and is there for compatibility-reasons. YOu should use
> Public.

> BUT! To Andy's answer: You do not need to add a module to your
> project, it is enough to declare it Public in your form.

> When you refer to it from your second form, you prefix it
> with the first forms name, like this (in Form2)

>     ...
>     Form1.VariableName = 15
>     ' or
>     myLocalVar = Form1.VareiableName

> The OO-way (and in my opinion proper way) of doing it is:

> In Form1:

> Private m_VariableName As Integer
> ...
> Public Property Let VariableName(Byval n As Integer)
>     m_VariableName = n
> End Property

> Public Property Get VariableName As Integer
>     VariableName = m_VariableName
> End Property

> In Form2:

> Just like in the first example.

> Dag.



Tue, 11 May 2004 06:54:02 GMT  
 
 [ 6 post ] 

 Relevant Pages 

1. NEWBIE question: variable scope

2. Newbie - Variable scope question

3. please help newbie variable question

4. Newbie Question about variables

5. newbie listbox variable question

6. Newbie question on subroutine keeping variable info

7. Newbie: Environment variable question

8. Passing variables/parameters between forms - newbie question

9. Newbie Question: Sharing variables across forms

10. Newbie question about Username variable...

11. newbie question: how to convert vb variables

12. Variable in a variable question

 

 
Powered by phpBB® Forum Software