A.5. Chapter 5

A.5.1.

A.5.1.1.
A.5.1.1.1. Exercise 1 solution

Both the Byte and the SByte data types are designed to hold small, numeric values. Both of them take up exactly the same amount of computer memory, so you're probably best off using the Byte data type. Because it doesn't allow you to store negative numbers, it's clear from the start that it can only contain a number between 0 and 255. However, it's much better not to store someone's age, but the date of birth instead. That way, you can always extract the age from the date of birth by comparing it with today's date, as demonstrated by the following example:

VB.NET

Dim albertsBirthday As DateTime = New DateTime(1879, 3, 14)
Dim age As Integer = DateTime.Now.Year - albertsBirthday.Year       ' displays 129 in 2008

C#

DateTime albertsBirthday = new DateTime(1879, 3, 14);
int age = DateTime.Now.Year - albertsBirthday.Year;                // displays 129 in 2008
A.5.1.1.2. Exercise 2 solution

This code looks a little tricky because it uses an expression and an assignment in one condensed line of code. Because of the order in which things are executed, the expression is evaluated first, followed by the assignment. In the original code, this means that ShoppingCart.Items.Count > 0 is evaluated first. This is an expression that checks if the Count property of the Items collection in the shopping cart is greater than zero. As you learned in this chapter, this results in a Boolean expression with either the value True or False. This Boolean ...

Get Beginning ASP.NET 3.5: In C# and VB now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.