Wednesday, April 6, 2011

Objects and Classes



Traditional programming is made up of a collection of subroutines and functions that are typically processed in a sequential or looping manner. In contrast, object oriented programming is a different programming methodology where a program is viewed as being composed of a collection of individual objects. These objects process data and can interact with other objects directly without having to be explicitly programmed to do so. The advantages claimed by object-oriented program include code reusability, rapid deployment of large-scale complex tasks, and ease of use/debugging. Today, objectoriented programming is widely used and is supported with both programming languages (e.g. VB.NET, C++, Visual C++) and operating systems (e.g. Microsoft’s .NET architecture). Object-oriented programming has also become popular within scripting languages, such as VBScript. Beginning with VBScript 5.0, developers have been able to use user-defined Classes.

The key concepts with object-oriented programming include:

Class
The class is the highest level that defines a unit (set) of data and its behavior. Classes form the basis for modularity and structure in an object-oriented program. The class should sufficiently describe the set of data, and the code for a class should be contained within it and be selfsufficient (except for operating system support). While the terms classes and objects often get used interchangeably, classes describe the structure of objects. One way to think of a class is that it is a container for code. It can also be viewed as a template for an object. When a class is declared (instantiated) by the Set statement, it then becomes an object and memory is allocated for it.

• Object
An object is an in-memory instance of a class. In computer science terms, it is a run-time manifestation (instantiation) of a particular exemplar of a class. Each object has its own data, but the code within a class can be shared (for efficiency). Programs generally have multiple objects. Multiple copies (objects) of a given class can be created. Objects are temporary, i.e. they can be created and removed at will, depending on the programming needs. 

Encapsulation
Encapsulation wraps the data and functions into a single unit, ensuring that the object can be changed only through established interfaces. Encapsulation is sometimes referred to as information hiding. Some of these common interfaces are: 

o Fields
Fields are simply public variables stored within the object, as defined by the class. These variables store items of information about an object. 

o Properties
Properties, like fields, also store items of information on an object. But Properties use Property procedures to control how values are set or returned. VBScript has two primary Property procedures; Let and Get. The Get property procedure retrieves a Property value, while the Let Property procedure assigns a value to the property value. A third Property procedure Set is used with an Object inside of the Class block.

o Methods
Methods are a collection of subroutines (Sub) and function procedures (Function) declared within a class.

o Events
An event is a message sent by an object announcing that something important has happened. followed by a period, then the particular method, property or field of interest. E.g.

Object.Method
Object.Property
Object.Property.Item

• Dynamism
Dynamism relates to the method of allocating computer resources and definition resources required to run an object-oriented program. There are different types, but VBScript used latebound (late-binding) dynamic typing. This means that the VBScript engine will make the object type determination at runtime and allocate sufficient memory at that time. Note that VBScript and VB.NET are slightly different in their approach to dynamism, and therefore they can declare some variables and objects in different manners (although many forms of declaration are the same).

• Outlet Connections
At times, Objects will connect together and this connection needs to be defined. With IWS, an example of a connection would be between a VBScript object (e.g. ADODB) and a Database Provider (a Provider is a front-end to a database). This connection needs to be defined, and then the connection string (of parameters) between the objects gets defined. When the need for the connection is finished, the connection should be closed.

While a full treatment of object-oriented programming is beyond the scope of these materials, the fundamental concepts of Objects and Classes are important to understand. VBScript supports COMbased Objects (Component Object Module, a Microsoft standard) such as the ActiveX controls, ADO.NET, FileSystemObject, and Microsoft Office Automation objects. VBScript also supports userdefined classes, or Class Objects. VBScript COM objects and VBScript Class objects differ from each other in several important respects. These differences lead to each type of object having its unique strengths:

• VBScript classes are more flexible than VBScript COM objects. Class Objects have an abstract subtype that encapsulates the data you want and the functions you need to work with that data. VBScript COM objects have only basic subtypes (integer or string).

• VBScript classes are slightly more efficient than COM objects. The VBScript parser can execute the classes' code directly instead of asking the COM object to execute a method.

• COM objects are binary modules. VBScript classes are ASCII files.

• You can use any scripting language to write COM objects. You can only use VBScript to write VBScript classes.

• You can use COM objects from within any development environment that supports COM automation. VBScript classes can only be used within development and runtime environments that support VBScript (e.g IWS and Microsoft Internet Explorer). 




No comments:

Post a Comment