Results 1 to 13 of 13

Thread: [RESOLVED][2008]declaring global variables

  1. #1

    Thread Starter
    Lively Member
    Join Date
    Apr 2008
    Posts
    74

    Resolved [RESOLVED][2008]declaring global variables

    If i declare a variable inside form1 Public Class Form1 like see below, & then i add a module1 & want to use this same variable inside this module any idea how can i do that

    Code:
    Public Class Form1
        Dim var as string = "bingo"
    Code:
    Module Module1
        Sub _test()
            MsgBox(var)
        End Sub
    End Module
    EDIT: see post 13 for solution sample codes
    Last edited by goldenix; Apr 25th, 2008 at 12:15 AM.

    M.V.B. 2008 Express Edition

  2. #2
    PowerPoster Jenner's Avatar
    Join Date
    Jan 2008
    Location
    Mentor, OH
    Posts
    3,712

    Re: declaring global variables

    Declare the variable in the Module then.
    My CodeBank Submissions: TETRIS using VB.NET2010 and XNA4.0, Strong Encryption Class, Hardware ID Information Class, Generic .NET Data Provider Class, Lambda Function Example, Lat/Long to UTM Conversion Class, Audio Class using BASS.DLL

    Remember to RATE the people who helped you and mark your forum RESOLVED when you're done!

    "Two things are infinite: the universe and human stupidity; and I'm not sure about the universe. "
    - Albert Einstein

  3. #3
    A SQL Server fool GaryMazzone's Avatar
    Join Date
    Aug 2005
    Location
    Dover,NH
    Posts
    7,493

    Re: declaring global variables

    That is decalred as a local var (to the form) only not public
    Sometimes the Programmer
    Sometimes the DBA

    Mazz1

  4. #4

    Thread Starter
    Lively Member
    Join Date
    Apr 2008
    Posts
    74

    Re: declaring global variables

    okkey maybe you misunderstood me? this is what im trying to do:

    1) declare var in Form1.vb
    2) click the button on the GUI to call _test() function from Module1.vb
    3) result = msbox("bingo")
    Code:
    'Form1.vb
    Public Class Form1
        Sub _buttonclick...()
            Dim var As String = "bingo"
            _test()
        End Sub
    End Class
    
    'Module1.vb
    Module Module1
        Sub _test()
            MsgBox(var)
        End Sub
    End Module

    M.V.B. 2008 Express Edition

  5. #5
    Lively Member
    Join Date
    Apr 2006
    Location
    Local
    Posts
    112

    Re: declaring global variables

    Is this what you want?

    Code:
    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
            Dim var As String = "Bingo"
            Test(var)
        End Sub
    Code:
    Module Module1
        Public Sub Test(ByVal myVar As String)
            MessageBox.Show(myVar)
        End Sub
    End Module
    Cheers
    Microsoft Visual Basic 2008

  6. #6
    PowerPoster Jenner's Avatar
    Join Date
    Jan 2008
    Location
    Mentor, OH
    Posts
    3,712

    Re: declaring global variables

    Ron beat me to it. Cheers!
    My CodeBank Submissions: TETRIS using VB.NET2010 and XNA4.0, Strong Encryption Class, Hardware ID Information Class, Generic .NET Data Provider Class, Lambda Function Example, Lat/Long to UTM Conversion Class, Audio Class using BASS.DLL

    Remember to RATE the people who helped you and mark your forum RESOLVED when you're done!

    "Two things are infinite: the universe and human stupidity; and I'm not sure about the universe. "
    - Albert Einstein

  7. #7

    Thread Starter
    Lively Member
    Join Date
    Apr 2008
    Posts
    74

    Re: declaring global variables

    thanx hire is the code to declare more than 1 variable like this:

    Code:
        'to add module go Project -> add module
        'Form1.vb
        Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
            Dim var As String = "Bingo"
            Dim var2 As String = "Bingo2"
            Test(var, var2)
        End Sub
    
        'Module1.vb
        Public Sub Test(ByVal myVar As String, ByVal myVar2 As String)
            MessageBox.Show(myVar & Chr(13) & myVar2)
        End Sub

    M.V.B. 2008 Express Edition

  8. #8
    PowerPoster Jenner's Avatar
    Join Date
    Jan 2008
    Location
    Mentor, OH
    Posts
    3,712

    Re: [RESOLVED][2008]declaring global variables

    Looks good. That's how you pass variables around easily.
    My CodeBank Submissions: TETRIS using VB.NET2010 and XNA4.0, Strong Encryption Class, Hardware ID Information Class, Generic .NET Data Provider Class, Lambda Function Example, Lat/Long to UTM Conversion Class, Audio Class using BASS.DLL

    Remember to RATE the people who helped you and mark your forum RESOLVED when you're done!

    "Two things are infinite: the universe and human stupidity; and I'm not sure about the universe. "
    - Albert Einstein

  9. #9

    Thread Starter
    Lively Member
    Join Date
    Apr 2008
    Posts
    74

    Re: [2008]declaring global variables

    okey but what about this?

    1) we declare var1 in Form1.vb
    2) then we call a function from Module1.vb to show var value in messagebox

    all good so far, but now we do this:

    3) we declare var2 in Module1.vb & call function from Form1.vb to show value of the var2

    basically we do the opposite but now I it errors & says that the test2 is not declared, now thats wired.
    Code:
        'Form1.vb
        Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
            Dim var As String = "Bingo"
            Test(var)
        End Sub
    
        Public Sub Test2(ByVal myVar2 As String)
            MessageBox.Show(myVar2)
        End Sub
    
    
        'Module1.vb
        Public Sub Test(ByVal myVar As String)
            MessageBox.Show(myVar)
    
            Dim var2 As String = "Bingo2"
            Test2(var2)
        End Sub

    M.V.B. 2008 Express Edition

  10. #10
    Super Moderator Shaggy Hiker's Avatar
    Join Date
    Aug 2002
    Location
    Idaho
    Posts
    40,109

    Re: [2008]declaring global variables

    Form1.Test2 is declared.

    What you are running into is general scoping issues. Test2 is a method of the Form1 class. As with any other class method, shared or not shared, you need to indicate the instance of the class where the sub is found. For the same reason, you can't call Show, but have to call MessageBox.Show.
    My usual boring signature: Nothing

  11. #11

    Thread Starter
    Lively Member
    Join Date
    Apr 2008
    Posts
    74

    Re: [2008]declaring global variables

    Quote Originally Posted by Shaggy Hiker
    Form1.Test2 is declared.

    What you are running into is general scoping issues. Test2 is a method of the Form1 class. As with any other class method, shared or not shared, you need to indicate the instance of the class where the sub is found. For the same reason, you can't call Show, but have to call MessageBox.Show.
    so how can i indicate it to declare var2 in Module1 & use it in Form1.vb? can you make an example?

    M.V.B. 2008 Express Edition

  12. #12
    Powered By Medtronic dbasnett's Avatar
    Join Date
    Dec 2007
    Location
    Jefferson City, MO
    Posts
    9,897

    Re: [2008]declaring global variables

    'Public Class Form1
    ' Public myVar As Integer
    '
    'Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
    ' aModule()
    ' modVar = 0
    'End Sub
    'End Class
    Module Module1
    Public modVar As Integer
    Public Sub aModule()
    Form1.myVar = 0
    End Sub
    End Module
    My First Computer -- Documentation Link (RT?M) -- Using the Debugger -- Prime Number Sieve
    Counting Bits -- Subnet Calculator -- UI Guidelines -- >> SerialPort Answer <<

    "Those who use Application.DoEvents have no idea what it does and those who know what it does never use it." John Wein

  13. #13

    Thread Starter
    Lively Member
    Join Date
    Apr 2008
    Posts
    74

    Re: [2008]declaring global variables

    Quote Originally Posted by dbasnett
    ...
    Thanx, thats exactly what i wanted to know. & to summarize all this:

    Code:
        'to add module go Project -> add module
        'Form1.vb
        Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
            Dim var As String = "Bingo"
            Dim var2 As String = "Bingo2"
            Test(var, var2)
        End Sub
    
        'Module1.vb
        Public Sub Test(ByVal myVar As String, ByVal myVar2 As String)
            MessageBox.Show(myVar & Chr(13) & myVar2)
        End Sub
    & another more simple way of diong it:
    Code:
        'Form1.vb
        Public myVar As Integer
        Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
            myVar = 1
            aModule()
    
            MsgBox(modVar)
        End Sub
    
        'Module1
        Public modVar As Integer
        Public Sub aModule()
            modVar = 2
            MsgBox(Form1.myVar)
        End Sub
    Last edited by goldenix; Apr 25th, 2008 at 12:18 AM.

    M.V.B. 2008 Express Edition

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