Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. Visual Basic
  4. What am I doing wrong with Public?

What am I doing wrong with Public?

Scheduled Pinned Locked Moved Visual Basic
question
4 Posts 2 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • D Offline
    D Offline
    David Williams
    wrote on last edited by
    #1

    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

    D 1 Reply Last reply
    0
    • D David Williams

      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

      D Offline
      D Offline
      Danny Blanchard
      wrote on last edited by
      #2

      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

      D 1 Reply Last reply
      0
      • D Danny 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. Blanchard

        D Offline
        D Offline
        David Williams
        wrote on last edited by
        #3

        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."

        D 1 Reply Last reply
        0
        • D David Williams

          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."

          D Offline
          D Offline
          Danny Blanchard
          wrote on last edited by
          #4

          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

          1 Reply Last reply
          0
          Reply
          • Reply as topic
          Log in to reply
          • Oldest to Newest
          • Newest to Oldest
          • Most Votes


          • Login

          • Don't have an account? Register

          • Login or register to search.
          • First post
            Last post
          0
          • Categories
          • Recent
          • Tags
          • Popular
          • World
          • Users
          • Groups