dcsimg
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 4 of 4

Thread: Public & Private

  1. #1
    Join Date
    Aug 2000
    Posts
    1,471

    Public & Private

    What do private and public variables/procedures mean separately in VB?

  2. #2
    Join Date
    Apr 2002
    Location
    Los Angeles, Ca
    Posts
    238
    It has to do with Scope.
    A public variable must be declared in either a seperate module or in the General declarations section of a form. They can be accessed by ANY code within the project. Public Variables cannot be used inside a Private module.

    Private variables can only be accessed by code within the module it is declared in, or within code that is on that form, if it is declared in the General Declarations section. Private variables can be declared and used within Public modules.

    Play around with those explanations in a test project. It will become very clear.
    Michael

  3. #3
    Join Date
    Jul 2000
    Location
    Milano, Italy
    Posts
    7,726

    Some more infos...

    There is a dfifference declaring a public variable in a bas module
    or in a Cass or form module.
    When you're in a bas module, you are declaring a variable.
    When you are in a class module or in a form module, you are
    declaring... a property of that object. But in this last case, better
    you use "Property get", "let" "set" routines...
    Thus, you can declare something public in a private module, but it
    becomes a property of that module.
    About scope:
    A public variable in a Bas module is visible everiwhere withing the
    project. If you have a public variable in a bas module of a dll, that
    public variable is visible throughuot the dll, but not outside it
    (=not from project referecing that dll)
    A public Property in a class module will have different scope
    whether the class has a private or public instance (=instancing
    property of class). When you add a class module to an exe
    standard, you do not have this property as it can be only private.
    In that case, that property can be accessed (as a property of that
    class) via an instance of that class.
    ie:
    in an exe standard project, you could have a form and a class
    module. Inside the class module, you could have:
    Code:
    Option Explicit
    Private mIamAProperty As String 'the correct way to hold property
    Public AnotherProperty As String 'the wrong way to declare a property
    
    'the correct way to declare property:
    Public Static Property Get IamAProperty() As String
        IamAProperty = mIamAProperty
    End Property
    
    Public Static Property Let IamAProperty(ByVal NewIam As String)
        mIamAProperty = NewIam
    End Property
    and in form you could have:
    Code:
    Option Explicit
    
    Private Sub Command1_Click()
        Dim cModule As Class1
        Set cModule = New Class1
        cModule.IamAProperty = "Hello"
        cModule.AnotherProperty = "Word"
        MsgBox cModule.IamAProperty & " " & cModule.AnotherProperty
    End Sub
    If the above class was not a private class but a class in a dll with instancing higher than private, you could access property in same way, but even from another exe project, and not only inside the dll. While if in same dll you have a class with instancing= private, that public property is visible only to those modules that can declare that class, which are all inside the dll project...
    ...at present time, using mainly Net 4.0, Vs 2010



    Special thanks to Lothar "the Great" Haensler, Chris Eastwood , dr_Michael, ClearCode, Iouri and
    all the other wonderful people who made and make Codeguru a great place.
    Come back soon, you Gurus.

  4. #4
    Join Date
    Aug 2000
    Posts
    1,471
    Thanks for your help, Hobbit and Cimperiali.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  


Windows Mobile Development Center


Click Here to Expand Forum to Full Width




On-Demand Webinars (sponsored)