When you create a
class, you can exercise control on how its members can be accessed outside.
A member variable of a structure class is referred to as a friend if
it can be accessed by any class of the source file. Classes outside of its
source file cannot access such a member. To create such a
member variable, precede it with the Friend keyword. Here is an example:
Structure Circle
Friend Radius As Double
End Structure
A member of a structure or class is referred to as private
if it can be accessed only by other members of the same structure or class. To
mark a member as private, precede it the Private keyword. Here is an
example:
Public Structure Point
Dim x As Integer
Private y As Integer
End Structure
If you want a member of a structure or a class to be accessed
outside of its source file, you must make that member public. To do this, precede
the member with the Public keyword. Here is an example:
Public Structure Point
Public x As Integer
Dim y As Integer
End Structure
We mentioned that one of the differences between a structure
and a class is that the latter must be declared using the New operator. Another
difference has to do with access levels. By default, all members of a structure
are public. This means that, if you omit the access level on a member of a
structure, it is automatically made public and can be accessed outside the
source file. By default, all members of a class are private. This means that if
you create a member of class using the Dim keyword, the member is made public.
If you want the member of a class to be accessible outside the class, mark it as
public.
You can exercise
control on the access of a structure or class. You can make a structure or class friendly, private, or public.
A structure or a class is referred to as private if it
belongs to another structure or class. In other words, such a structure or class
must be created inside another structure or class. To create a private structure
or class, precede it with the Private
keyword. Here is an example:
Public Class Rectangle
Dim Length As Double
Dim Height As Double
Private Class Box
End Class
End Class
A structure or a class is referred to as friendly if it can be accessed
by other structures and classes of the same source file by not outside that
source file.
To create a friendly class, precede it with the Friend
keyword. Here is an example:
Friend Class Square
Public Side As Double
End Class
A structure or a class is referred to as public if it can be accessed outside
its source file. To create such a structure or class, precede the Structure
or Class keyword with Public when
creating it. Here is an example:
Public Structure Circle
Friend Radius As Double
End Structure
|
Introduction to Classes
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment