Accesssing private constant

Posted on 2005-04-06
Last Modified: 2010-05-02


I have a class called CControl which is the parent class of CButton and CLabel.

I stored a constant variable called MAX_CONTROL (maximum number of button and label i can create)

so in CControl:
Private Const MAX_CONTROL = 19


How do i access this constant variable from my child class (CButton for example) ?

and How do i access this constant variable from a normal class? (meaning not in the inheritance chain of CControl)


Question by:TungVan
    LVL 65

    Expert Comment

    by:Jim Horn
    Hi TungVan,

    Instead of Private use Friend Const MAX_CONTROL = 19

    You can also use Public to make sure it can be access from anywhere.

    Hope this helps.
    LVL 44

    Assisted Solution

    you can also add a Property to CControl, to allow you to read (and/or write) the value:

    If all you need is to be able to read the value, then you simple add a Public Property Get routine:


    Private Const MAX_CONTROL = 19

    Public Property Get MaxControl() as Integer
         MaxControl = MAX_CONTROL
    End Property

    Now, elsewhere in you program, you can get the value of objControl.MaxControl

    which will return the value 19.



    Author Comment


    When i use:

    Friend Const MAX_CONTROL = 19

    I have Compile error:

    Expected: Sub or Function or Property
    LVL 5

    Accepted Solution

    jim's explanation is for

    You can use also a function for reading private constant

    Const MAX_CONTROL As Long = 19

    Friend Function GetMAX_CONTROL() As Long
    End Function
    LVL 5

    Expert Comment

    You can also do this

    Public MAX_CONTROL As Long

    Private Sub Class_Initialize()
        MAX_CONTROL = 19
    End Sub
    LVL 44

    Expert Comment

    the explanation that I provided will work in BOTH VB6 and VB.NET, with NO changes, and is the Object-Oriented way to approach this problem.  Declaring a PUBLIC variable in a Class is an invitation to trouble.  Maybe not in this limited instance, but getting into that habit will lead you to serious problems somewhere down the line.  Doing things in a Proper OO manner, from the beginning, will pay huge didvends in the future, when you migrate to VB.NET (as you must, at some time do, if you have not done so already).  VB.NET is 100% Object Oriented, and many of the habits that you have gotten into with VB 6 will not work in the .NET world.


    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    Threat Intelligence Starter Resources

    Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

    There are many ways to remove duplicate entries in an SQL or Access database. Most make you temporarily insert an ID field, make a temp table and copy data back and forth, and/or are slow. Here is an easy way in VB6 using ADO to remove duplicate row…
    Since upgrading to Office 2013 or higher installing the Smart Indenter addin will fail. This article will explain how to install it so it will work regardless of the Office version installed.
    As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
    Get people started with the process of using Access VBA to control Outlook using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Microsoft Outlook. Using automation, an Access applic…

    779 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    13 Experts available now in Live!

    Get 1:1 Help Now