18.22. Defining an Interface


The syntax for defining an interface is as follows:

 [attributes] [access-modifier] Interface identifier [InterfaceBases]   interface-body End Interface 

The keyword Interface is followed by an identifier (the interface name). It is common (but not required) to begin the name of your interface with a capital I. Thus, IStorable, ICloneable, IAndThou, etc.

The body of the interface is terminated with the keywords End Interface.

Suppose you wish to create an interface to define the contract for being stored. Your interface will define the methods and properties a class will need to be stored to a database or file. You decide to call this interface IStorable. The purpose of this interface is to define the capabilities that you want to have available in any class that can be stored.

In the IStorable interface you might specify two methods: Read and Write, and a property Status:

 Interface IStorable     Sub Read(  )     Sub Write(ByVal obj As Object)     Property Status(  ) As Integer End Interface 

Note that when declaring the methods of the interface you provide a prototype:

 Sub Read(  ) 

but no implementation and no End Function, End Sub, or End Property statement. Notice also that the IStorable method declarations do not include access modifiers (e.g., public, protected, internal, private). In fact, providing an access modifier generates a compile error. Interface methods are implicitly public because an interface is a contract meant to be used by other classes.



Programming Visual Basic 2005
Programming Visual Basic 2005
ISBN: 0596009496
EAN: 2147483647
Year: 2006
Pages: 162
Authors: Jesse Liberty

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