Partager via


Visual Basic Concepts

Friend Properties and Methods

In addition to declaring properties and methods Public and Private, you can declare them Friend. Friend members look just like Public members to other objects in your project. That is, they appear to be part of a class's interface. They are not.

In the ActiveX components you can create with the Professional and Enterprise editions of Visual Basic, Friend members play an important role. Because they're not part of an object's interface, they can't be accessed by programs that use the component's objects. They're visible to all the other objects within the component, however, so they allow safe internal communication within the component.

Important   Because Friend members aren't part of an object's public interface, they can't be accessed late bound — that is, through variables declared As Object. To use Friend members, you must declare variables with early binding — that is, As classname.

Standard Exe projects can't be ActiveX components, because their class modules can't be Public, and thus can't be used by other applications. All communication between objects in a Standard Exe project is therefore private, and there's no need for Friend members.

However, Friend members have one particularly useful feature. Because they're not part of an ActiveX interface, they can be used to pass user-defined types between objects without exposing them publicly. For example, suppose you have the following user-defined type in a standard module:

Public Type udtDemo

   intA As Integer
   lngB As Long
   strC As String
End Type

You can define the following private variable and Friend members in Class1:

Private mDemo As udtDemo

Friend Property Get Demo() As udtDemo
   Demo = mDemo
End Property

' Note that udtDemo must be passed by reference.
Friend Property Let Demo(NewDemo As udtDemo)
   mDemo = NewDemo
End Property

Friend Sub SetDemoParts(ByVal A As Integer, _
      ByVal B As Long, ByVal C As String)
   mDemo.intA = A
   mDemo.lngB = B
   mDemo.strC = C
End Sub

Public Sub ShowDemo()
   MsgBox mDemo.intA & vbCrLf _
   & mDemo.lngB & vbCrLf & mDemo.strC
End Sub

Note   When you pass user-defined types as Sub, Function, or property procedure arguments, you must pass them by reference. (ByRef is the default for procedure arguments.)

You can then write the following code to use Class1:

Private Sub Command1_Click()
   Dim c1A As New Class1
   Dim c1B As New Class1
   c1A.SetDemoParts 42, 1138, "Howdy"
   c1B.Demo = c1A.Demo
   c1B.ShowDemo
End Sub

The message box will display 42, 1138, and "Howdy."

Note   Because Friend procedures are not part of a class's interface, they are not included when you use the Implements statement to implement multiple interfaces, as described in "Polymorphism."

For More Information   The use of Friend members in components is discussed in "Private Communication Between Your Objects" in "General Principles of Component Design" in the Component Tools Guide.