[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 :confused:
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
Re: declaring global variables
Declare the variable in the Module then.
Re: declaring global variables
That is decalred as a local var (to the form) only not public
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
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
Re: declaring global variables
Ron beat me to it. Cheers! :)
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
Re: [RESOLVED][2008]declaring global variables
Looks good. That's how you pass variables around easily.
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
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.
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?
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
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