OOP TERMS


In Visual Basic, objects are created from classes. A class provides a template that specifies data, properties, and methods that are available for working with the data.

image from book
DEFINITION

A class is a template that can be used to instantiate objects.

image from book

Visual Basic provides access to a large collection of classes. For example, the code behind a Form comes from the Form class, which is located in the System.Windows.Forms namespace. Examples of other classes include System.Windows.Forms.Button, where the Button class is defined, and System.Windows.Forms.CheckBox, where the CheckBox class is defined.

image from book
DEFINITION

A namespace is an organized collection of classes. The .NET Framework provides access to numerous namespaces, which it makes available in its class library.

image from book

Whether you realized it or not up to this point, you have been working with classes in all of the chapter game projects that you have worked on in this book. Every time that you have designed a new user interface, you have worked with classes provided by the .NET Framework. If you go back and look at your various chapter project applications, you'll notice that all of the code for the application is wrapped up inside an opening and closing Class statement. For example, the first and last statements in the Dice Poker game are shown below.

 Public Class frmMain End Class 

As simple as these statements look, they deserve a little extra explanation. The opening statement publicly defines a new Class called frmMain, and the last statement defines the end of the class. However, there is more going on here than first meets the eye. Visual Basic 2005 Express makes things easy on you by hiding much of the work that it does behind the scenes on your behalf. To see what is really happening here, click on the Shall All Files button in Solution Explorer, expand the Form1.vb entry and click on the Form1.designer.VB entry. You'll see a whole bunch of behind-the-scenes code that Visual Basic has automatically generated on your behalf. You can learn a lot from studying the code that you see here, but it isn't necessary that you understand everything that you see. Take a look at the first two statements that are displayed, as shown below.

 Partial Public Class frmMain   Inherits System.Windows.Forms.Form 

As you can now see, the form that Visual Basic has automatically provided you with is actually inherited (or cloned) from the Form class stored in the System.Windows.Forms namespace.

As should be obvious at this point, OOP simplifies the coding process by hiding many of the complexities of an application from the programmer. In addition, it promotes the reuse of code by making it possible to define a class once and then use it over and over again to create as many objects as required.

Visual Basic also allows you to create your own custom classes, which is much of the focus of this chapter. But before I get too deep into our discussion of OOP, let's stop for a minute and go over a few key terms and make sure that you understand the basic building blocks of OOP. These terms are abstraction, encapsulation, inheritance, and polymorphism.

Abstraction

Abstraction is a process by which you define in program code a logical representation of a class. This includes the specification of the base functionality belonging to the class and the definition of properties and methods that are required for the proper operation of the class. Returning to the Form class as an example, base properties include the Text, Size, BackColor, and ForeColor properties.

Encapsulation

Encapsulation is the process whereby you package the base functionality of a class and provide access to the features of the class through a collection of properties and methods. When an object is created from a particular class, the inner workings of the object are hidden from the programmer. Again, consider the Form class. You don't know what the code behind it looks like or how it is written. You cannot directly access its internal code or data. You only know that to work with the form, you modify its properties, execute its methods, and provide code for its events. How the Form class was designed to make all these things work is hidden from you. Thus, encapsulation helps simplify program development and hide complexity.

Encapsulation also includes data protection, meaning that code can be added to process and verify the validity of any data passed to an object to ensure that it meets certain criteria.

Inheritance

Inheritance is the process whereby one class is derived from another. When you define a new class in your program code, such as when you define a new class for a Visual Basic Form, the class that you create inherits its features from a base class, which in the case of this example is System.Windows.Forms.Form.

Polymorphism

Polymorphism is the ability to create something in different forms. Within Visual Basic, polymorphism is accomplished through a programming technique know as overloading. For example, using overloading, a programmer might develop multiple versions of the same procedure (with the same procedure name), each of which is designed to handle a different combination of arguments. For example, one version of a procedure might be set up to process a single Integer argument, whereas another version of the procedure might be set up to process two arguments, one of which might be an Integer and the other of which might be a string.

image from book
DEFINITION

Overloading is the process of defining the same method multiple times, each with a different argument list.

image from book

Trick 

One really good example of overloading that you have already seen and worked with is the MessageBox class's Show method. This method supports multiple overloads, allowing it to accept various combinations of arguments and data types. To view the various combinations of arguments and data that you can pass to the MessageBox.Show method, open the Visual Basic code editor and type MessageBox.Show. In response, IntelliSense will display a pop-up window showing the various options that are available to you. Click on the up and down arrows in the upper left-hand corner of the pop-up window to explore the various overload options that are available.




Microsoft Visual Basic 2005 Express Edition Programming for the Absolute Beginner
Microsoft Visual Basic 2005 Express Edition Programming for the Absolute Beginner
ISBN: 1592008143
EAN: 2147483647
Year: 2006
Pages: 126

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net