A Short Lesson on Vectors

In this chapter, you'll use the vector as the basis for an exercise in overloading operators. A vector is a line segment with direction and magnitude. You can specify both the direction and magnitude of the vector by specifying a coordinate pair x, y . Using this notation, the three vectors in the following diagram are 2, 4 , 3, 0 , and -4, -4 . Addition, subtraction, multiplication, and equality are defined on vectors, making the vector a good candidate for operator overloading. Vector...

Design Considerations for Properties

When designing and implementing properties, the following considerations apply Properties can be read-write, read-only, or write-only. A read-write property can be retrieved and set by client code. A read-only property can only be retrieved and a write-only property can only be set. Write-only properties are rare. Developers tend to use write-only properties to send data to hardware devices so that the value sent to the device can't be retrieved later. Use a read-only property when the property...

Create the accounts

1. Double-click Form1 to display the Form1_Load method in the code editor. 2. Delete the test code for the CheckingAccount class. 5. Private checking As New CheckingAccount Your Name 6. Private savings As New SavingsAccount Your Name 9. private CheckingAccount checking new CheckingAccount Your Name 10. private SavingsAccount savings new SavingsAccount Your Name 11. Add this code to the Form1_Load method to initialize the accounts Private Sub Form1_Load ByVal sender As System.Object, _ ByVal e...

Pascal Casing and camel Casing

In .NET applications, you'll find two styles of capitalization Pascal Casing and camel Casing. When you use Pascal Casing, you capitalize each word in an identifier the name of something , just as both Pascal and Casing are capitalized. Examples include FirstName and LastName. Using camel Casing, you capitalize every word except the first word of the identifier. Examples include firstName and lastName. The capitalized letter in the middle of the identifier might remind you of a camel's hump....

Create the SavingsAccount class

1. On the Project menu, click Add Class. The Add New Item dialog box appears. 2. Name the file SavingsAccount.vb or SavingsAccount.cs, depending on the language you're using. 3. Modify the class declaration to indicate that BankAccount is the base class as shown here 10. public class SavingsAccount TheBank.BankAccount If you're using Visual Basic, the following message appears in the Task List Cannot implicitly create a constructor for 'Class SavingsAccount' because its base class 'BankAccount'...

Quick Reference Ffk

In Visual Basic, declare the event name and its signature. Public Event CaughtOnFire ByVal sender As Object, ByVal e As CaughtOnFireEventArgs In Visual C , declare the delegate type of the event, and then declare the delegate of that type. public delegate void CaughtOnFireEventHandler object sender, CaughtOnFireEventArgs e public event CaughtOnFireEventHandler CaughtOnFire Declare a class field of the object using the WithEvents keyword. In the Class Name list click the class. In the Method...

Add the RoundButton class

If you're using Visual Basic, add the class declaration at the end of the source file. If you're using Visual C , add the class declaration at the end of the file, but before the closing brace of the RoundButton namespace. public class RoundButton Button By overriding the OnPaint method, you direct the runtime to draw a round button, instead of the usual rectangular button. 1. If you're using Visual Basic, click RoundButton Overrides in the Class List, and then click OnPaint in the Method Name...

BankAccount A Simple Example

The simple bank account provides the basis for this exercise in inheritance. You will implement a BankAccount class as a base class with the following members The simple bank account provides the basis for this exercise in inheritance. You will implement a BankAccount class as a base class with the following members A string property that identifies the owner of the account. property that identifies the account. A read-only decimal property. The value of this property depends on the deposits...

Install the practice files

Follow these steps to install the practice files on your computer's hard disk so that you can use them with the exercises in this book. 1. Remove the companion CD from the package inside the back cover of this book and insert the CD in your CD-ROM drive. 2. Double-click the My Computer icon on the Desktop. Tip On some computers, the startup program might run automatically when you close the CD-ROM drive. In this case, skip steps 2 through 5 and follow the instructions on the screen. 3....

Test the SavingsAccount class

1. Open Forml in the code editor. 2. Delete the code that you added to test the BankAccount class, so that the Form1_Load method is empty. 3. Add this code to test the SavingsAccount class 5. Private Sub Form1_Load ByVal sender As System.Object, _ 6. ByVal e As System.EventArgs Handles MyBase.Load 7. Dim savings As SavingsAccount New SavingsAccount Your Name 13. String.Format 0 1 C , savings.ID, savings.Balance 17. private void Form1_Load object sender, System.EventArgs e 18. SavingsAccount...

Create the CheckingAccount class

1. On the Project menu, click Add Class. The Add New Item dialog box appears. 2. Name the file CheckingAccount.vb or CheckingAccount.cs, depending on the language you're using. 3. Modify the class declaration to indicate that BankAccount is the base class as you see here 5. Public Class CheckingAccount 10. public class CheckingAccount TheBank.BankAccount

Use an instance of the Book class

1. Add the following code after the cookies code to display some of the text of the two books. In later chapters, you'll learn other ways to return the text of a particular page in the book. 5. report Page amp page.ToString amp ControlChars.CrLf _ 6. amp fairyTales.Title amp amp fairyTales.GetPage page _ 8. amp Cookies amp cookies.GetPage page 10. report Titles fairyTales.Title amp and amp cookies.Title 15. report Page page.ToString n 16. fairyTales.Title fairyTales.GetPage page n 17....

Override Equals and GetHashCode

Both the Equals method and the operator of the System.Object class the base class of Vector return true if two references point to the same instance. When you overloaded the and operators, you defined to mean that the two vectors had the same direction and magnitude. By overloading the Equals method, you give the Equals method the same meaning as the operator. The GetHashCode method is called if the Vector class is used as the key for a key-value pair in a hash table. A hash table is a data...