Chapter 13: Interfaces

Quiz

Solution to Question 13–1.

The interface defines the methods, properties, etc. that the implementing class must provide. The implementing class provides these members and, optionally, additional members.

Solution to Question 13–2.

Every class has exactly one base class, but may implement zero, one, or more interfaces. An abstract base class serves as the base to a derived class that must implement all its abstract methods or that derived class is also abstract.

Solution to Question 13–3.

class MyClass : MyBase, ISuppose, IDo {}

Note that the base class must come first after the colon.

Solution to Question 13–4.

The is and the as operator.

Solution to Question 13–5.

is returns false if the interface is not implemented; as returns null. Using the as operator can be more efficient.

Solution to Question 13–6.

Extending an interface is very much like deriving a class. The new interface inherits all the members of the parent interface, and can also include additional methods.

Solution to Question 13–7.

ExtendedInterface : OriginalInterface

For example, read:

ILoggedCompressible : ICompressible

as “ILoggb[edCompressible extends ICompressible.”

Solution to Question 13–8.

The class implementing a method of the interface can mark that method virtual, and the implementation of the method can be overridden in derived classes.

Solution to Question 13–9.

Explicit interface implementation identifies the member of the interface by naming the interface itself (e.g., IStorable.Write( )). This is done to differentiate implementation methods when there might otherwise be an ambiguity, such as when implementing more than one interface that have methods with the same signature.

Exercises

Solution to Exercise 13-1.

Define an interface IConvertible that indicates that the class can convert a string to C# or VB2005. The interface should have two methods: ConvertToCSharp and ConvertToVB2005. Each method should take a string, and return a string.

using System;

namespace Exercises
{
   interface IConvertible
   {
      string ConvertToCSharp( string stringToConvert );
      string ConvertToVB2005( string stringToConvert );
   }
}
Solution to Exercise 13-2.

Implement that interface and test it by creating a class ProgramHelper that implements IConvertible. You can use simple string messages to simulate the conversion.

using System;

namespace Exercises
{
   interface IConvertible
   {
      string ConvertToCSharp( string stringToConvert );
      string ConvertToVB2005( string stringToConvert );
   }


   public class ProgramHelper : IConvertible
   {
      public ProgramHelper( )
      {
         Console.WriteLine( "Creating ProgramHelper" );
      }

      public virtual string ConvertToCSharp( string stringToConvert )
      {
         return "Converting the string you passed in to CSharp syntax";
      }

      public virtual string ConvertToVB2005( string stringToConvert )
      {
         return "Converting the string you passed in to VB 2005 syntax";
      }

   }  // end class ProgramHelper

   class Tester
   {

      static void Main(  )
      {
         Tester t = new Tester(  );
         t.Run(  );
      }

      public void Run(  )
      {
         // Create a ProgramHelper object
         ProgramHelper theProgramHelper = new ProgramHelper(  );

         // convert a line of CSharp to vb
         string vbString = (
             "Public Sub Read(  ) Implements IStorable.Read " +
            "Console.WriteLine("Implementing the Read Method for IStorable")"
            + "End Sub 'Read")" );

         Console.WriteLine( vbString );

         // convert the converted line back to CSharp
         string cSharpString = theProgramHelper.ConvertToCSharp( vbString );

         Console.WriteLine( cSharpString );

      }  // end run
   }     // end class Tester
}        // end namespace
Solution to Exercise 13-3.

Extend the interface by creating a new interface, IConvertible. The new interface should implement one new method, CodeCheckSyntax, which takes two strings: the string to check, and the language to use. The method should return a bool. Revise the ProgramHelper class from Exercise 13-2 to use the new interface.

using System;

namespace Exercises
{
   interface IConvertible
   {
      string ConvertToCSharp( string stringToConvert );
      string ConvertToVB2005( string stringToConvert );
   }

   interface ICodeChecker : IConvertible
   {
      bool CheckCodeSyntax( string stringToCheck, string whichLang );
   }


   public class ProgramHelper : ICodeChecker
   {
      public ProgramHelper( )
      {
         Console.WriteLine( "Creating ProgramHelper" );
      }

      public virtual string ConvertToCSharp( string stringToConvert )
      {
         return "Converting the string you passed in to CSharp syntax";
      }

      public virtual string ConvertToVB2005( string stringToConvert )
      {
         return "Converting the string you passed in to VB 2005 syntax";
      }

      public bool CheckCodeSyntax ( string stringToCheck, string whichLang)
      {
         switch ( whichLang )
         {
            case "CSharp":
               Console.WriteLine(
                   "Checking the string "{0}" for C# Syntax", stringToCheck );
               return true;
            case "VB2005":
               Console.WriteLine(
                     "Checking the string "{0}" for VB 2005 Syntax",
                      stringToCheck );
               return true;
            default:
               return false;
         }

      }

   }  // end class ProgramHelper

   class Tester
   {

      static void Main(  )
      {
         Tester t = new Tester(  );
         t.Run(  );
      }

      public void Run(  )
      {
         // Create a ProgramHelper object
         ProgramHelper theProgramHelper = new ProgramHelper(  );

         // convert a line of CSharp to VB
         string cSharpString = theProgramHelper.ConvertToCSharp(
            "Public Sub Read(  ) Implements IStorable.Read " +
            "Console.WriteLine("Implementing the Read Method for IStorable")"
            + "End Sub 'Read")" );

         Console.WriteLine( cSharpString );
         Console.WriteLine (
            "Checking "{0}" for syntax... Result {1}", cSharpString,
             theProgramHelper.CheckCodeSyntax(cSharpString, "CSharp"));

         // convert the converted line back to VB
         string vbString = theProgramHelper.ConvertToCSharp( cSharpString );

         Console.WriteLine( vbString );
         Console.WriteLine (
          "Checking "{0}" for syntax... Result {1}", vbString,
           theProgramHelper.CheckCodeSyntax(vbString,"VB2005"));

      }  // end run
   }     // end class Tester
}        // end namespace
Solution to Exercise 13-4.

Demonstrate the use of is and as. Create a new class, ProgramConverter, that implements IConvertible. ProgramConverter should implement the ConvertToCSharp( ) and ConvertToVB( ) methods. Revise ProgramHelper so that it derives from ProgramConverter, and implements ICodeChecker.

using System;

namespace Exercises
{
   interface IConvertible
   {
      string ConvertToCSharp( string stringToConvert );
      string ConvertToVB2005( string stringToConvert );
   }

   interface ICodeChecker : IConvertible
   {
      bool CheckCodeSyntax( string stringToCheck, string whichLang );
   }

   public class ProgramConverter : IConvertible
   {
      public ProgramConverter(  )
      {
         Console.WriteLine( "Creating ProgramConverter" );
      }
      public virtual string ConvertToCSharp( string stringToConvert )
      {
         return "Converting the string you passed in to CSharp syntax";
      }
      public virtual string ConvertToVB2005( string stringToConvert )
      {
         return "Converting the string you passed in to VB 2005 syntax";
      }

   }

   public class ProgramHelper : ProgramConverter, ICodeChecker
   {
      public ProgramHelper(  )
      {
         Console.WriteLine( "Creating ProgramHelper" );
      }

      public bool CheckCodeSyntax( string stringToCheck, string whichLang )
      {
         switch ( whichLang )
         {
            case "CSharp":
               Console.WriteLine(
                 "Checking the string {0} for C# Syntax", stringToCheck );
               return true;
            case "VB2005":
               Console.WriteLine(
                  "Checking the string {0} for VB 2005 Syntax", stringToCheck );
               return true;
            default:
               return false;
         }     // end switch
      }        // end method Check Code Syntax
   }           // end class ProgramHelper

   class Tester
   {

      static void Main(  )
      {
         Tester t = new Tester(  );
         t.Run(  );
      }

      public void Run(  )
      {
         ProgramConverter[] converters = new ProgramConverter[4];
         converters[0] = new ProgramConverter(  );
         converters[1] = new ProgramHelper(  );
         converters[2] = new ProgramHelper(  );
         converters[3] = new ProgramConverter(  );

         foreach ( ProgramConverter pc in converters )
         {
            string vbString =
              pc.ConvertToCSharp( "Public Sub Read() Implements IStorable.Read "
              + "Console.WriteLine("Implementing the Read Method for
                                   IStorable")"
              + "End Sub 'Read")" );

            Console.WriteLine( vbString );

            ProgramHelper ph = pc as ProgramHelper;
            if ( ph != null )
            {
               ph.CheckCodeSyntax( vbString, "VB2005" );
            }
            else
            {
               Console.WriteLine( "No vb syntax check - not a Program helper" );
            }

            string cSharpString = pc.ConvertToCSharp( vbString );
            Console.WriteLine( cSharpString );
            if ( ph != null )
            {
               ph.CheckCodeSyntax( vbString, "CSharp" );
            }
            else
            {
               Console.WriteLine(
                  "No csharp syntax check - not a Program helper" );
            }
         }  // end foreach in converters
      }  // end run
   }     // end class Tester
}        // end namespace
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset