Interface...End Interface Statement


Interface...End Interface Statement

Syntax

     [accessModifier] [Shadows] Interface name [(Of typeParamList)]        [statements]     End Interface 


accessModifier (optional)

Specifies the scope and accessibility of the interface. One of the following access levels:

Access level

Description

Public

The interface is publicly accessible anywhere, both inside and outside of the project.

Private

The interface is accessible within the type in which it is defined.

Protected

The interface is accessible only to the type in which it is defined and to derived instances of that type.

Friend

The interface is accessible only within the project that contains the interface definition.

Protected Friend

Combines the access features of Protected and Friend.


If omitted, the Friend access level is used.


Shadows (optional)

Indicates that the interface shadows an identically named element in a base class.


name (required)

The name of the interface.


typeParamList (optional)

Adds type parameter placeholders that will later enforce strong typing when the interface is used through its class implementation. The Of clause implements generics, which are fully described in Chapter 10. If generics will not be used, this clause can be excluded.


statements (optional)

Code that defines the interface members that derived classes must implement.

Description

The Interface...End Interface statement defines a virtual base class along with its members. The members of an interface include sub procedures, functions, properties, and events. All members are automatically public within the interface.

Interfaces are implemented by derived classes and structures using the Implements statement.

Usage at a Glance

  • By convention, interface names generally begin with the capital letter I.

  • The interface definition (statements) may contain the following elements:


    Inherits statement

    Indicates that name inherits some of its properties and methods from another interface. Its syntax is:


    Inherits interfaceName[, interfaceName...]

    where interfacename is the name(s) of the interface(s) from which name inherits.


    Property definitions

    Property definitions take the form:

         [Default] [ReadOnly | WriteOnly] Property procname([arglist]) As type 

    where procname is the name of the property, with its defined arglist and return type. Default indicates that procname is the interface's default member. The ReadOnly and WriteOnly keywords limit the accessors of the property.


    Function definitions

    Functions are defined as:

         Function membername([arglist]) As type 

    where membername is the name of the function, arglist defines the number and type of arguments that can be passed to the function, and type indicates the function's return value.


    Procedure definitions

    Procedures are defined as:

         Sub membername[(arglist)] 

    where membername is the name of the procedure, and arglist specifies the number and type of arguments that can be passed to the procedure.


    Event definitions

    Events are defined as:

         Event membername[(arglist)] 

    where membername is the name of the event, and arglist defines the number and type of arguments that are passed to an event handler whenever the event is fired.

  • In each case, the syntax of the statement is different from the "standard" implementation syntax. Access modifiers, for instance, are not permitted as a part of interface member definitions (since they would all be Public), nor are End statements, such as End Function, End Sub, or End Property.

  • The name interface cannot inherit from an interface with an access type that is more restrictive than its own. For example, if name is a Public interface, it cannot inherit from a Friend interface.

  • Classes and structures that implement the interface must implement all of its members, and each member implementation must include the interface's signature for that member.

  • An interface can have only one default property. This includes properties defined in base interfaces, as well as in the interface itself.

Version Differences

  • The Interface...End Interface construct is new to VB under .NET. In VB 6, an interface was defined by creating a class module with members that had no implementation.

  • Visual Basic 2005 adds support for generics to interfaces, as discussed in Chapter 10.

See Also

Implements Keyword, Implements Statement




Visual Basic 2005(c) In a Nutshell
Visual Basic 2005 in a Nutshell (In a Nutshell (OReilly))
ISBN: 059610152X
EAN: 2147483647
Year: 2004
Pages: 712

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net