?
Solved

Accesssing private constant

Posted on 2005-04-06
6
Medium Priority
?
255 Views
Last Modified: 2010-05-02


Hi


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)


Thanks

0
Comment
Question by:TungVan
6 Comments
 
LVL 66

Expert Comment

by:Jim Horn
ID: 13716934
Hi TungVan,

Instead of Private use Friend Const MAX_CONTROL = 19
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/vblr7/html/vakeyFriend.asp

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

Hope this helps.
-Jim
0
 
LVL 44

Assisted Solution

by:Arthur_Wood
Arthur_Wood earned 135 total points
ID: 13717182
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:

cControl
-------------------

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.

AW

0
 

Author Comment

by:TungVan
ID: 13717219

When i use:

CControl
------------------------------------------------------------------------------------------------
Friend Const MAX_CONTROL = 19
------------------------------------------------------------------------------------------------

I have Compile error:

Expected: Sub or Function or Property
0
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
LVL 5

Accepted Solution

by:
Rubyn earned 240 total points
ID: 13721152
jim's explanation is for vb.net

You can use also a function for reading private constant

Const MAX_CONTROL As Long = 19

Friend Function GetMAX_CONTROL() As Long
    GetMAX_CONTROL = MAX_CONTROL
End Function
0
 
LVL 5

Expert Comment

by:Rubyn
ID: 13721163
You can also do this

Public MAX_CONTROL As Long

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

Expert Comment

by:Arthur_Wood
ID: 13723528
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.

AW
0

Featured Post

Become an Android App Developer

Ready to kick start your career in 2018? Learn how to build an Android app in January’s Course of the Month and open the door to new opportunities.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction While answering a recent question (http://www.experts-exchange.com/Q_27402310.html) in the VB classic zone, I wrote some VB code in the (Office) VBA environment, rather than fire up my older PC.  I didn't post completely correct code o…
Background What I'm presenting in this article is the result of 2 conditions in my work area: We have a SQL Server production environment but no development or test environment; andWe have an MS Access front end using tables in SQL Server but we a…
Get people started with the process of using Access VBA to control Excel using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Excel. Using automation, an Access application can laun…
Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…
Suggested Courses
Course of the Month9 days, 10 hours left to enroll

609 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