Freetutes.com

VB6 beginners tutorial - Learn VB6

Advanced VB6 tutorial - Learn Advanced VB6

Systems Analysis - System analysis and Design tutorial for Software Engineering


You are here: Visual Basic > Advanced VB6 tutorial > Chapter 12

Using Public, Private, and Friend Keywords

If a property or method is added to a class module, it becomes part of the class. This property or method is usable by other modules in the same project as the class module. It is also available to other projects, as a property or method of objects derived from that class. This is the default setting for the scope of a property or method. There are three other ways of defining the scope of a property or method:

Using the Public Keyword

If the Public keyword is used when defining a property or method, the property behaves as though the Public keyword is not used. This is because the default scope of a property or method defined in a class module is Public.

Public properties and methods are available to other modules in the same project as the class module. They are also available to other projects as properties and methods of an object derived from that class module. Most properties and methods will be defined as Public.

Consider the following, for example:

   Public Property Get HireDate() as Date
   Public Sub Save()

Using the Private Keyword

Using the Private keyword when defining a property or method creates a property or method private to that class module. Only code in that class module can access the property or method.

Private methods are typically support routines used by the properties of a class. For example, you may need some helper routines that perform date calculations.

Consider the following, for example:

   Private Function NextWorkDay(FromDate as Date) As Date
   Private Sub PrintForm(FormName As String)

Using the Friend Keyword

Using the Friend keyword when defining a property or method creates a property or method private to the project containing the class module. Code in the same class module as the procedure can access the property or method. Code in other modules in the same project can also access the property or method. This is useful when creating helper classes that should not be used by other programs.

Friend properties and methods are often used when a number of classes assist the main class that other programs will use. A class used to write another object to a file, for example, might have two methods: Load and Save. These methods, declared as Friend, would allow the calling object to access them. They would not be available to other projects.

Consider the following, for example:

   Friend Sub Load(FileName As String)
   Friend Function Save(FileName As String) As Boolean

By explicitly setting the scope for each of the properties and methods in a class, you can better control how other modules and programs access them. Most properties and methods will be Public; however, having access to the Private and Friend keywords allows for greater control of access.


  

<< Previous | Contents | Next >>

Home | About Us | Privacy Policy | Contact Us

Copyright © Freetutes.com | All Rights Reserved