Docstoc

vb.net(help)

Document Sample
vb.net(help) Powered By Docstoc
					What Is an Interface?
      Interfaces, also known as contracts, define a common set of members that all classes that
      implement the interface must provide. For example, the IComparable interface defines
      the CompareTo method, which enables two instances of a class to be compared for
      equality. All classes that implement the IComparable interface, whether custom-created
      or built in the .NET Framework, can be compared for equality.
      IDisposable is an interface that provides a single method, Dispose, to enable assemblies
      that create an instance of your class to free up any resources the instance has con-
      sumed. To create a class that implements the IDisposable interface using Visual
      Studio, follow these steps:
       1. Create the class declaration. For example:
           ' VB
           Class BigClass
           End Class


           // C#
           class BigClass
           {
           }

       2. Add the interface declaration, as shown in bold here:
           ' VB
           Class BigClass
               Implements IDisposable
           End Class


           // C#
           class BigClass : IDisposable
           {
           }

       3. If you are using Visual Basic, Visual Studio should generate method declarations
          automatically for each of the required methods after you press Enter at the end
          of the Implements command. If it does not, delete the Implements command and
          try again; Visual Studio may still be starting up. If you are using C#, right-click
          the Interface declaration, click Implement Interface, and then click Implement
          Interface again, as shown in Figure 1-1.
       4. Write code for each of the interface’s methods. In this example, you would write
          code in the Dispose method to deallocate any resources you had allocated.
     Figure 1-1   Visual Studio simplifies implementing an interface

Table 1-6 lists the most commonly used interfaces in the .NET Framework.
Table 1-6   Commonly Used Interfaces
 Interface        Description
 IComparable      Implemented by types whose values can be ordered; for example,
                  the numeric and string classes. IComparable is required for sorting.
 IDisposable      Defines methods for disposing of an object manually. This interface
                  is important for large objects that consume considerable resources,
                  or objects such as databases that lock access to resources.
 IConvertible     Enables a class to be converted to a base type such as Boolean, Byte,
                  Double, or String.
 ICloneable       Supports copying an object.
 IEquatable       Allows you to compare instances of a class for equality. For
                  example, if you implement this interface, you could say if (a == b).
 IFormattable     Enables you to convert the value of an object into a specially
                  formatted string. This provides greater flexibility than the base
                  ToString method.

You can create your own interfaces, too. This is useful if you need to create multiple
custom classes that behave similarly and can be used interchangeably. For example,
the following code defines an interface containing three members:
' VB
Interface IMessage
    Function Send() As Boolean
    Property Message() As String
    Property Address() As String
End Interface
// C#
interface IMessage
{
    bool Send();
    string Message { get; set; }
    string Address { get; set; }
}

If you implement that interface in a new class, Visual Studio generates the following
template for the interface members:
' VB
Class EmailMessage
    Implements IMessage

   Public Property Address() As String Implements IMessage.Address
       Get
       End Get
       Set(ByVal value As String)
       End Set
   End Property

   Public Property Message() As String Implements IMessage.Message
       Get
       End Get
       Set(ByVal value As String)
       End Set
   End Property

    Public Function Send() As Boolean Implements IMessage.Send
    End Function
End Class


// C#
class EmailMessage : IMessage
{
    public bool Send()
    {
    throw new Exception("The method or operation is not implemented.");
    }

   public string Message
   {
       get
       {
       throw new Exception("The method or operation is not implemented.");
       }
       set
       {
       throw new Exception("The method or operation is not implemented.");
       }
   }
      public string Address
      {
          get
          {
          throw new Exception("The method or operation is not implemented.");
          }
          set
          {
          throw new Exception("The method or operation is not implemented.");
          }
      }
}

If you create a custom class and later decide that it would be useful to have multiple
classes with the same members, Visual Studio has a shortcut to extract an interface from
a custom class. Simply follow these steps:
    1. Right-click the class in Visual Studio.
    2. Click Refactor and then click Extract Interface.
    3. Specify the interface name, select the public members that should form the inter- face,
       and then click OK.
Classes can implement multiple interfaces. For example, a class could implement both
the IComparable and IDisposable interfaces.

				
DOCUMENT INFO
Shared By:
Categories:
Stats:
views:13
posted:5/1/2012
language:English
pages:4
Description: n this File You Can easily Understand About .net Interface