98Microsoft Visual Studio 2010: A Beginner’s Guide

Making Classes Implement the Interface

To create a class, right-click the project in Solution Explorer, select Add New Item, select Code under the language branch in Installed Templates, and select the Class item. Name the class Checking and click Add. Using the same procedure as Checking, add another class, but name it Saving. Listings 4-3 and 4-4 show the two new classes.

Listing 4-3 Checking class that implements IAccount interface

C#:

using System;

using System.Collections.Generic; using System.Linq;

using System.Text;

namespace FirstProgram

{

class Checking : IAccount

{

public void Credit(decimal amount)

{

//implement checking logic CurrentBalance += amount; Console.Writeline("Added " + amount.ToString() +

"to Checking Account");

}

public void Debit(decimal amount)

{

//implement checking logic CurrentBalance -= amount; Console.Writeline("Debited " + amount.ToString() +

"from Checking Account");

}

public decimal CurrentBalance { get; set; }

}

}

VB:

Public Class Checking

Implements IAccount

Public Sub Credit(ByVal amount As Decimal) Implements IAccount. Credit

Page 121
Image 121
Microsoft 9GD00001 manual Making Classes Implement the Interface