Results 1 to 4 of 4

Thread: Public Variable in 6.0

  1. #1

    Thread Starter
    Junior Member
    Join Date
    May 2000
    Posts
    23

    Angry

    How do you declare a variable and use a variable which is availabe in any form.

    Thankyou

  2. #2
    Fanatic Member
    Join Date
    Feb 2000
    Location
    Japan
    Posts
    840
    Code:
    Public MyVar As Integer ' Or Long, Variant, String etc
    But it has to be in a Module, not in a form. A public variable in a form is only accessable to that form's functions and subs.

    Public Arrays must be declared in modules too.

    Paul Dwyer
    Network Engineer
    Aussie In Tokyo

    Using Powerbasic 6 & VB6 SP4 (Please also add your VB Version to your signature!)

  3. #3
    Guest
    You can also use the Global Keyword.

  4. #4
    Frenzied Member Mark Sreeves's Avatar
    Join Date
    Nov 1999
    Location
    UK
    Posts
    1,845
    But it has to be in a Module, not in a form. A public variable in a form is only accessable to that form's functions and subs.

    Public Arrays must be declared in modules too.
    That's not strictly true because public variables in a form are accesable to other parts of the program while the form is loaded


    form1:
    Code:
    Option Explicit
    Public intNum As Integer
    
    Private Sub Command1_Click()
    Form2.Show
    End Sub
    
    Private Sub Form_Load()
    intNum = 5
    
    Form1.intNum = 8
    MsgBox Form1.intNum
    
    End Sub
    form2:
    Code:
    Option Explicit
    
    Private Sub Command1_Click()
    MsgBox Form1.intNum
    End Sub
    but yes, public variables would normally go in a module.

    Public is ususlly rather than Globalthese days.

    Global is a hanger-on from older versions of (Visual) Basic



    Mark
    -------------------

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width