Full file at http://TestbankCollege.eu/Solution-Manual-Advanced-Programming-Using-Visual-Basic-2008-4th-Edition-Bradley

Chapter 2

Building Multitier Programs with Classes

ANSWERS TO REVIEW QUESTIONS

1.  Define abstraction, encapsulation, inheritance, and polymorphism.

Abstraction means to create a model of an object, for the purpose of determining the characteristics (properties) and the behaviors (methods) of the object. You need to use abstraction when planning an object-oriented program to determine the classes that you need and the necessary properties and methods.

Encapsulation refers to the combination of characteristics of an object along with its behaviors. You have one “package” that holds the definition of all properties, methods, and events.

Encapsulation is sometimes referred to as data hiding. Each object keeps its data (properties) and procedures (methods) hidden. Through use of the Public and Private keywords, an object can “expose” only those data elements and procedures that it wishes to allow the outside world to see.

Inheritance is the ability to create a new class from an existing class. You can add enhancements to an existing class without modifying the original. By creating a new class that inherits from an existing class, you can add or change class variables and methods.

Polymorphism actually means the ability to take on many shapes or forms. As applied to OOP, polymorphism refers to method names having identical names but different implementations, depending on the situation. When a derived class overrides a method of its base class, both methods have the same signature. But in each case, the actions performed are appropriate for the class. Polymorphism also allows a single class to have more than one method with the same name but a different argument list. The method is said to be overloaded.

2.  What is an abstract class and how is it used?

An abstract class is a class that is created specifically to use as a base for derived classes. You cannot instantiate objects from an abstract class, but only inherit new classes from it.

3.  Why should property variables in a class be declared as private?

Inside your class you define variables, which contain the values for the properties of the class. To accomplish encapsulation, you declare all properties in a class as Private. As a private variable, the value is available only to the procedures within the class, the same way that private module-level variables in a form are available only to procedures within the form's class.

4.  What are property procedures and what is their purpose?

The way that your class allows its properties to be accessed is through property procedures. A property procedure may contain a Get to retrieve a property value and/or a Set to assign a value to the property. The name that you use for the Property procedure becomes the name of the property to the outside world.

5.  Explain how to create a new class and instantiate an object from that class.

Create a new class by choosing Add Class from the Project menu. Next, analyze the characteristics and behaviors that your object needs. Create module-level private variables for each property procedure that will be created. Add the property procedures that will be used to pass the values to the class module and return values from the class module.

After a new class is defined, it can be used to instantiate (create) a new object. In the user interface tier (or other location that will use the class), in the event where the object is needed, instantiate the object using the New keyword and specify the class name. The New keyword creates a new instance of the object class. For example: Dim APayroll as New Payroll(HoursDecimal, RateDecimal)

6.  What is a constructor, how is it created, and when is it triggered?

A constructor is a method that executes automatically when an object is created. In VB, the constructor must be named New and must be Public or Protected.

7.  What is a parameterized constructor?

The term parameterized constructor refers to a constructor that requires arguments. This popular technique allows you to pass arguments/properties as you create a new object.

8.  How can you write methods for a new class?

Methods in a new class are written in sub procedures.

9.  What is a shared member? What is its purpose? How is it created?

Shared members are also referred to as shared properties or shared variables. They can be used by all instances of the class and they are generally used for accumulating totals and counts. Create Private shared members with the Shared keyword. Since these variables are Private to the class, Public Get methods are required to make the properties accessible. You retrieve shared properties by using the class name and the property name, such as Payroll.NumberProcessed.

10.  Explain the steps necessary to inherit a class from another class.

An inherited class can appear in the same file as the base class. When you make a new class inherit from an existing class, usually the first statement in the constructor is MyBase.New(). This executes the constructor of the base class. Create the new class, then add properties and methods to include the new capabilities.

11.  Differentiate between overriding and overloading.

A derived class with a method named the same as a method in the base class is said to override the method in the base class. Overriding allows an inherited class to take different actions from the identically named method in the base class.

Overloading is when two methods have the same name but a different argument list. When the overloaded method is called, the argument type determines which version of the method to use.

12.  What are the advantages of developing applications using multiple tiers?

Multitier applications separate program functions into the presentation tier (the user interface), the business tier (the logic of calculations and validation), and the data tier (accessing stored data).

One advantage of using multitier development is that the business rules can be changed without changing the interface or the interface can be changed without changing the business tier.

Also, each of the functions of a multitier application can be coded in a separate component and the components may be stored and run on different machines.

13.  Describe the steps necessary to perform validation in the business services tier but display the message to the user in the presentation tier.

In the Property Set procedure of the class, test if the value(s) are outside of the acceptable range for valid input and throw an exception if data is invalid. Use the exception class to throw an exception and include arguments for the error message and the name of the field that caused the error.

Example:

Dim Ex As New ApplicationException( “Hours are outside of the acceptable range.”)

Ex.Source = “Hours”

Back in the form a Select Case statement will determine what field caused the error and an appropriate message will be displayed to the user.

14.  Explain the differences between a namespace-level variable and a module-level variable. How is each created and how is it used?

Namespace-level variable - A namespace is an area used for grouping and referring to classes and structures. Because each project is in its own namespace by default, generally namespace scope also means project scope. However, you can structure your own namespaces to contain multiple projects.

Module-level variable - Module-level scope is sometimes also called class-level scope. A module-level variable is Private by default. It is declared inside any class, structure, or module, but outside of any sub procedure or function. A module-level variable can be used in any of the procedures of the class.

The lifetime of a namespace-level variable is as long as the program is running. The lifetime of a module-level variable is as long as any reference to the class remains, which is generally as long as the program runs.

15.  Explain the differences between a procedure-level variable and a block-level variable. How is each created and how is it used?

Procedure-level variable - Any variable that you declare inside a procedure or function, but not within a block, has procedure-level scope, also called local scope. You can reference the variable anywhere inside the procedure but not in other procedures. The Public keyword is not legal inside a procedure; all procedure-level variables are Private.

Block level - If you declare a variable inside a code block, the variable has block-level scope. That is, the variable can be referenced only inside that block. Code blocks include: If/End If, While/End While, Do/Loop, For/Next, Select Case/End Select, and Try/Catch/Finally/End Try.

16.  What is the lifetime of a procedure-level variable? A block-level variable? A module-level variable?

The lifetime of a variable, including object variables, is as long as the variable remains in scope.

The lifetime of a module-level variable is as long as any reference to the class remains, which is generally as long as the program runs.

The lifetime of a procedure-level variable is one execution of the procedure. Each time the procedure is executed, a new variable is established and initialized. For this reason, you cannot use procedure-level variables to maintain running totals or counts unless you declare them with the Static keyword, which changes the lifetime of a procedure-level variable to the life of the class or module.

The lifetime of a block-level variable is only inside that block and it terminates when the block completes.

17.  Explain the difference between overriding and shadowing methods.

An inherited class can have a method with the same name as a method in its base class. Depending on how it is declared, the new method may shadow or override the base class method.
Overriding: To override a method in a base class, the method must be declared as overridable and in the derived class you must use the Overrides keyword and have the same accessibility (Public|Private). If the base-class method has more than one signature, the override applies only to the base-class method with the identical signature.

Shadowing: A method in a derived class can shadow a method in the base class. The new (shadowing) method replaces the base-class method in the derived class but not in any new classes derived from that class. The shadowing method “hides” all signatures (overloaded methods) with the same name in the base class.

18.  What is the effect of using the Protected accessibility modifier? The Friend modifier?

The Protected accessibility modifier will allow access from anywhere inside this class or in any class that inherits from this class.

The Friend modifier will allow access from anywhere inside this project/assembly.

The Protected Friend is a combination of Protected and Friend. It is accessible from anywhere inside this project/assembly and in any class that inherits from this class, even though the derived class is in a different project/assembly.

19.  What is an advantage of using the TryParse methods rather than Parse?

Parsing (Parse) takes considerable system resources to handle exceptions. TryParse returns zero if the parse fails, rather than throwing an exception.

20.  What is an advantage of using an ErrorProvider component rather than a message box?

Using an ErrorProvider component, you can make an error indicator appear next to the field in error, rather than pop up a message box. The ErrorProvider component can display a blinking icon next to the field in error and display a message in a pop-up, similar to a ToolTip.

21.  What is the purpose of an enum? How is one created?

An enum is a list of named constants. The enum structure must define integer values. The data type of the constants must be one of the integer types (integer, short, long, or byte).

The general format for creating an enum:

Enum EnumName

ConstantName1 [ConstantValue]

ConstantName2 [ConstantValue]

...

End Enum

The Enum statement belongs at the namespace level or class level, which means that it cannot appear inside a procedure. By default, an Enum is public, but you can declare it to be private, friend, or protected, if you wish.

22.  What is garbage collection? What does it do and when does it run?

The .NET Framework destroys unused objects and reclaims memory in a process called garbage collection. The garbage collector runs periodically and destroys any objects and variables that no longer have any active reference. You have no way of knowing when the garbage collection will occur. For current versions of VB, Microsoft recommends that you just allow object variables to go out of scope when you are finished with them.

Chapter 2 – Building Multitier Programs with Classes

2-1