Understanding Classes

function OpenWin(url, w, h) { if(!w) w = 400; if(!h) h = 300; window.open(url, "_new", "width=" + w + ",height=" + h + ",menubar=no,toobar=no,scrollbars=yes", true); } function Print() { window.focus(); if(window.print) { window.print(); window.setTimeout('window.close();',5000); } }
Team-Fly    

Special Edition Using Microsoft® Visual Basic® .NET
By Brian Siler, Jeff Spotts
Table of Contents
Chapter 9.  Creating Code Components


Having used controls and forms in Visual Basic .NET applications, you have already used classes and objects. For example, when you draw a text box on a form, you are actually creating a specific instance of the TextBox class. For example, if you draw five text boxes on your form, you have created five instances of the TextBox class. Even though each instance is a distinct entity, they were all created from the same template. A form itself is an instance of the Form class.

Instances of a class are known as objects. Each different class is a template from which a specific type of object is created. In this example, the TextBox class defines that a text box has a Text property. However, the class definition itself does not contain information about the contents of the properties. Instead, an object you create from the class, for example, txtLastName, actually contains that information.

Object-Oriented Programming

You probably have heard the term object-oriented programming(OOP) or read about it in programming books and magazines. A key element of OOP is its use of reusable objects to build programs.

OOP begins in the design stage, when you determine the objects needed for an application. For example, suppose you have to write a system to manage paychecks for employees. A traditional design plan would be to determine each program function, such as "Adding an employee to the database" or "Printing an employee paycheck." An object-oriented design would instead try to separate programming tasks along the lines of the objects in the program (employees, database, paycheck, and so on). For a design to be considered object-oriented, several facts must be true about the objects. These fundamental concepts of OOP are summarized in the following list:

  • Encapsulation. Encapsulation, or information hiding, refers to the fact that objects hide the details of how they work. For example, when you set the Text property of a text box, you do not know (or care) how the text box internally repaints the characters. Information hiding allows the programmer of an object to change how an object works without affecting the users of the object.

  • Inheritance. A new object can be defined based on an existing object, and it can contain all the same properties and methods. For example, you can create a new object that contains all the standard properties and methods of an existing object, plus a few of your own. You can just add your own extra properties and "inherit" the existing ones.

  • Polymorphism. Although many objects can have methods bearing the same name, the method can perform differently for each of the objects. Through polymorphism, the program runs the method appropriate for the current object. For example, the + operator can be used with both strings and integers. Even though the same symbol is used for both data types, Visual Basic knows to perform different operations.

An important consequence of an OOP approach is reusable code. Part of what makes an object reusable is its interface, or the methods and properties the object uses to communicate with the outside world. If you build objects with well-defined interfaces, it is easy to change the object internally or even add new interfaces without affecting programs that use the object.

Classes in Visual Basic

You can create your own classes in Visual Basic .NET by adding a Class template to an existing project, or by building a class library consisting of one or more classes. Classes can contain several types of elements:

  • Properties. These elements are used to assign and retrieve values from the class.

  • Methods. These are public functions or subroutines that are defined in the class.

  • Events. Just as a control can raise events in the form that contains it, an object created from your class can also raise events in its containing object.

Class modules also contain two special events of their own: New and Finalize. The New event is triggered when a new instance of the class is created, and the Finalize event occurs when the object is destroyed.

Object definitions are created in a class module. A class module is like a standard code module in that it contains only variable declarations and procedure code. There is no user interface component of a class module. However, a class can take action using a form that is in the program, just like a normal code module. Class modules can be used in several ways, such as the following:

  • In a Visual Basic .NET project, a class module provides a way to create multiple instances of objects anywhere in your program, without using global variables.

  • You can create Class Library objects and compile them into a DLL that other programmers can use in their code. For example, you can put all your business financial rules in a class and compile it as a DLL. Other programmers can reference the DLL and use the financial rules in their applications.

  • You can build an add-in to Visual Basic, to enhance the functions of the Visual Basic IDE.


    Team-Fly    
    Top
     



    Special Edition Using Visual Basic. NET
    Special Edition Using Visual Basic.NET
    ISBN: 078972572X
    EAN: 2147483647
    Year: 2001
    Pages: 198

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