What am I doing wrong with Public?
-
This doesn't work. I thought declaring a variable as public in a class made it global. Public Class Form1 ... Public i as integer Private Sub Form1_Load(... i = 1 End Sub End Class Public Class Form2 ... Private Sub Form2_Load(... Dim j As Integer j = i :confused: compiler says i is undefined End Sub End Class
-
This doesn't work. I thought declaring a variable as public in a class made it global. Public Class Form1 ... Public i as integer Private Sub Form1_Load(... i = 1 End Sub End Class Public Class Form2 ... Private Sub Form2_Load(... Dim j As Integer j = i :confused: compiler says i is undefined End Sub End Class
well, i and j are in different classes so they cannot see each other. now, if your instance of Form1 is global, then you can access i through it like this. Global instance of Form1:
Public MyForm1 as Form1
Access instance of Form1 in class Form2:Public Class Form2 ... Private Sub Form2_Load(... Dim j As Integer j = MyForm1.i End Sub End Class
Daniel E. Blanchard -
well, i and j are in different classes so they cannot see each other. now, if your instance of Form1 is global, then you can access i through it like this. Global instance of Form1:
Public MyForm1 as Form1
Access instance of Form1 in class Form2:Public Class Form2 ... Private Sub Form2_Load(... Dim j As Integer j = MyForm1.i End Sub End Class
Daniel E. BlanchardYeah, but the VB doc says "The Public keyword in the Dim statement declares elements to be accessible from anywhere within the same project, from other projects that reference the project, and from an assembly built from the project."
-
Yeah, but the VB doc says "The Public keyword in the Dim statement declares elements to be accessible from anywhere within the same project, from other projects that reference the project, and from an assembly built from the project."
David Williams wrote: The Public keyword in the Dim statement declares elements to be accessible from anywhere within the same project That's true if the element is global, but you forgot about how scope works. An element declared within a class is not global, but if declared as public can be accessed outside of the class. In order to do that you must reference the class. For example: You have a global variable declared in your project:
Public Var1 As Integer 'Global, accessable anywhere in the program
Then you have a class that can use that variable:Public Class TestClass1 Public Var2 As Integer 'Accessable from outside the class Private Var3 As Integer 'Accessable only in the class Public Sub MyProc() Dim Var4 As Integer 'Accessable only in this method Var4 = Var3 'This is OK because Var3 can be accessed anywhere in the class End Sub Private Sub MyProc2() Dim Var5 As Integer 'Accessable only in this method Var5 = Var1 'This is OK because Var1 can be accessed from anywhere. End Sub End Class
If you have a global instance of that class:Public TestClassObj as TestClass1
Then you can access public data inside that class:Public Class TestClass2 Private Var6 As Integer Public Sub DoSomething() Var6 = TestClass1Obj.Var2 'This is OK because TestClass2 has indentified where Var2 can be found End Sub End Class
So, the VB doc you mentioned was correct, but you have be carefull where you declare your elements and always mind program scope. Daniel E. Blanchard