Interface Statement

   
Interface Statement

Syntax

 [   accessmodifier   ] [Shadows] Interface   name   ...   statements   End Interface 
accessmodifier (optional; Keyword)

One of the following keywords, which determines the visibility of the interface:

Public (optional; Keyword)

Indicates that the interface is publicly accessible anywhere both inside and outside of the project.

Private (optional; Keyword)

Indicates that the interface is accessible to any nested types, as well as to the type (if any) in which it is defined.

Protected (optional; Keyword)

Indicates that the interface is accessible only to derived classes; a protected interface can only be declared inside of a class.

Friend (optional; Keyword)

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

Protected Friend (optional; Keyword)

Indicates that the interface is declard inside of a class and that it is accessible throughout the project that contains the interface definition, as well as to derived classes.

Shadows (optional; Keyword)

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

name (required; String literal)

The name of the interface

statements (required)

Code that defines the interface members that derived classes must implement

Description

Defines a virtual base class along with its public members. The interface can then be implemented by derived classes using the Implements statement.

Rules at a Glance

  • The standard naming conventions for name apply. However, by convention, interface names generally begin with the letter I .

  • If accessmodifier is omitted, the interface is Public by default.

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

    Inherits statement

    Indicates that name inherits 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 ] Property   procname   ([   arglist   ]) As   type   

    where procname is the name of the property, Default indicates that procname is a property array (whose argument list is defined by arglist ) that is the interface's default property, and type indicates the data type of the property. The ReadOnly and WriteOnly keywords can also be used.

    Function definitions

    Functions are defined as follows :

     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 procedure, and type indicates the function's return value.

    Procedure definitions

    Procedures are defined as follows:

     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 follows:

     Event membername[(   arglist   )] 

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

    In each case, the syntax of the statement is different from the "standard" VB.NET syntax. Access modifiers, for instance, are not permitted as a part of interface member definitions, nor are End... statements, such as End Function , End Sub , or End Property .

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

  • Classes that implement the interface must implement each of its methods, which must have the same argument list and, in the case of functions and properties, return a value of the same data type as specified by the interface definition.

Rules at a Glance

  • An interface can only inherit from another interface that has equal or wider accessability. Thus, for instance, a Public interface cannot inherit from a Private interface, but the reverse is allowed.

Programming Tips and Gotchas

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

VB.NET/VB 6 Differences

The Interface ... End Interface construct is new to VB.NET. In VB 6, an interface is defined by creating a class module whose members have no implementation.

See Also

Implements Keyword, Implements Statement

   


VB.Net Language in a Nutshell
VB.NET Language in a Nutshell
ISBN: B00006L54Q
EAN: N/A
Year: 2002
Pages: 503

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