Wednesday, November 7, 2012

Public vs. Private

Public vs. Private

A when something is public that means that something is available outside and inside the class. A Private something is only available inside a class. Nothing more, nothing less.
Many people still ask question about the difference about a private or a public. When a use the word something, this take effect to variable, object, class, sub function, thread …
This is also true for a Module.  So a private member in a Module will only be available inside the Module and a public will be available inside and outside that Module.

Take a look that the sample code. I use a module to put my private and my public integer.

Module Module1
    Public iNumber As Integer
    Private iCount As Integer
End Module

When you type Module1, Visual Studio will try to suggest choice. You will see the public Integer iNumber and not the private iCount .

When you try to use the private member, well …. You will get an error.

Please note, private and public is a concept and is true for C++, Java Visual Basic, C# and may other stuff. There is of course one exception: See Electrical Expert™ from IGE-XAO.

In their VBA (Visual Basic for Application), their private or public don’t work. In Fact everything is public witch is a flood and is a bug. That bug it exist since See Electrical Expert™ 2004, 2005, v3r5, v3r6, v3r7 and the new V4. For the last know issue of the V4, is true and I believe is still true but can’t confirm it today. Last time I checked, it was on spring 2012 and I don’t think is fixed.  The matter the fact, we might never know, the VBA is removed from their web site so I guest the VBA is no longer available.

Anyway, there are not a lot of people around the world who program macros in See Electrical Expert™ or cares.
The concept of private and public works fine in MS-Excel, MS-Word, MS-Outlook, Autodesk products such as AutoCAD™, NavisWorks™ …

References :

Private in MSDN : Private (Visual Basic)
Public in MSDN: Public (Visual Basic)
My web site : Check Technologies
Download Sample Project :


  1. I need help with General Declarations "Public" in VB6. When I press F5 "Compile error:
    Constants, fix-length strings, arrays, user-defined types and Declare statements not allowed as Public members of object modules" appears and firstLastitem() As Integer is embolded white on black. How do I declare a variable length array using Public so I can use it throughout the Form?

    1. I hope this help you....

      QUESTION: How do I declare a variable length array?
      Dim firstLastitem() As Integer
      ReDim firstLastitem(100)
      firstLastitem(0) = 20
      firstLastitem(1) = 21
      firstLastitem(2) = 23
      firstLastitem(99) = 25

      QUESTION: Public so I can use it throughout the Form?
      @John , it hard to answer your question, i don't have your code....

      Public class MyClass
      public firstLastitem() as Integer

      End class

      'inside your form (userform)

      dim objMyClass as MyClass()
      set objMyClass = new MyClass() 'argument if required)
      ReDim objMyClass.firstLastitem(100)
      objMyClass.firstLastitem(0) = 21

      I am not sure for the syntax or if this will work, I have not use VB6 for a long time.

  2. I used Public writingsNumber As Integer without generating an error. A variable declared as an Integer doesn't cause an error yet an array as an Integer doesn't go through. When I wasn't trying to use these two variables Publicly I simply Dim them. I thought Public was to be used instead of Dim for variables to be used globally in a VB Project. Please shed some light on how to fix this.
    Thanks so much.

    1. dim "make" a local variable. dim inside a Module is a kind of local variable but available everywhere. Public+Private is a way to give or not to give access to a variable outside from himself.

      Exemple, a class with a public variable could be access from other class outside him. Private variable is only available inside the class.