Interface Statement - Visual Basic (original) (raw)

Declares the name of an interface and introduces the definitions of the members that the interface comprises.

Syntax

[ <attributelist> ] [ accessmodifier ] [ Shadows ] _  
Interface name [ ( Of typelist ) ]  
    [ Inherits interfacenames ]  
    [ [ modifiers ] Property membername ]  
    [ [ modifiers ] Function membername ]  
    [ [ modifiers ] Sub membername ]  
    [ [ modifiers ] Event membername ]  
    [ [ modifiers ] Interface membername ]  
    [ [ modifiers ] Class membername ]  
    [ [ modifiers ] Structure membername ]  
End Interface  

Parts

Term Definition
attributelist Optional. See Attribute List.
accessmodifier Optional. Can be one of the following: - Public- Protected- Friend- Private- Protected Friend- Private Protected See Access levels in Visual Basic.
Shadows Optional. See Shadows.
name Required. Name of this interface. See Declared Element Names.
Of Optional. Specifies that this is a generic interface.
typelist Required if you use the Of keyword. List of type parameters for this interface. Optionally, each type parameter can be declared variant by using In and Out generic modifiers. See Type List.
Inherits Optional. Indicates that this interface inherits the attributes and members of another interface or interfaces. See Inherits Statement.
interfacenames Required if you use the Inherits statement. The names of the interfaces from which this interface derives.
modifiers Optional. Appropriate modifiers for the interface member being defined.
Property Optional. Defines a property that is a member of the interface.
Function Optional. Defines a Function procedure that is a member of the interface.
Sub Optional. Defines a Sub procedure that is a member of the interface.
Event Optional. Defines an event that is a member of the interface.
Interface Optional. Defines an interface that is a nested within this interface. The nested interface definition must terminate with an End Interface statement.
Class Optional. Defines a class that is a member of the interface. The member class definition must terminate with an End Class statement.
Structure Optional. Defines a structure that is a member of the interface. The member structure definition must terminate with an End Structure statement.
membername Required for each property, procedure, event, interface, class, or structure defined as a member of the interface. The name of the member.
End Interface Terminates the Interface definition.

An interface defines a set of members, such as properties and procedures, that classes and structures can implement. The interface defines only the signatures of the members and not their internal workings.

A class or structure implements the interface by supplying code for every member defined by the interface. Finally, when the application creates an instance from that class or structure, an object exists and runs in memory. For more information, see Objects and Classes and Interfaces.

You can use Interface only at namespace or module level. This means the declaration context for an interface must be a source file, namespace, class, structure, module, or interface, and cannot be a procedure or block. For more information, see Declaration Contexts and Default Access Levels.

Interfaces default to Friend access. You can adjust their access levels with the access modifiers. For more information, see Access levels in Visual Basic.

Rules

Behavior

Public Interface IDemo  
    Sub DoSomething()  
End Interface  
Public Class implementIDemo  
    Implements IDemo  
    Private Sub DoSomething() Implements IDemo.DoSomething  
    End Sub  
End Class  
Dim varAsInterface As IDemo = New implementIDemo()  
Dim varAsClass As implementIDemo = New implementIDemo()  

If you access class members through varAsInterface, they all have public access. However, if you access members through varAsClass, the Sub procedure doSomething has private access.

Example

The following example uses the Interface statement to define an interface named thisInterface, which must be implemented with a Property statement and a Function statement.

Public Interface thisInterface
    Property ThisProp(ByVal thisStr As String) As Char
    Function ThisFunc(ByVal thisInt As Integer) As Integer
End Interface

Note that the Property and Function statements do not introduce blocks ending with End Property and End Function within the interface. The interface defines only the signatures of its members. The full Property and Function blocks appear in a class that implements thisInterface.

See also