Classes in Visual Basic
A class is a part of the program that defines the properties, methods and events of one or more object that will be created during execution. An object is an entity created at run time, which requires memory and some resources and is then destroyed when it's no longer needed or when the application ends. In short, a class are design time only entities, while object are run-time entities.
A class is a template that defines the properties of an object and the methods used to control that object's behavior. The description of these members is done only once, in the definition of the class. The object that belongs to a class, called instance of the class, share the code of the class to which they belong but contain only their particular setting for the properties of the class.
Everything an object knows is expressed in its properties and everything it can do is expressed in its methods. Object interacts with each other by sending message requesting that method be carried out, or that properties be set or returned. A message is simply the name of an object followed by the name of one of its members.
Encapsulation is the process of combining logically related procedure and data in one class/objects. This way, each object is separated from the rest of the program. Because the object is only using data contained within it or passed to it. And it executes only internal procedures. It does not contain any global or public variables, and does not require any external procedures to execute its members. The data and behaviors of an encapsulated object can only be accessed and manipulated through its properties and public methods. Thus encapsulation provides several advantages for you as a programmer. You can protect data from corruption by other objects or parts of the program. You can hide low-level, complex implementation details from the rest of the program, which results in the ability to implement a simple public interface to a more complex set of private member.
Polymorphism is the ability of different classes to expose similar (or identical) interfaces to the outside. The most unmistakable kind of polymorphism in Visual Basic is forms and controls. For example, TextBox and PictureBox controls are completely different objects, but they have some properties and methods in common, such as Left property and Move method.
Inheritance is the ability to derive a new class (the derived or inherited class) from another class (the base class). The derived class automatically inherits the properties and methods of the base class. For example, you could define a generic Person class with properties such as FirstName and Lastname and then use it as a base for more specific classes that inherit all those generic properties.
At runtime, when Visual Basic encounters a reference to an auto-instancing variable, it first determines whether it's pointing to an existing object and creates a brand new instance of the class if necessary. But auto-instancing variables have an advantage and disadvantage:
• It reduce the amount of code you need to write to be up and running with your classes. This can be useful if you are prototyping an application.
• In some condition, you might declare a variable but never actually use it: which happens all the time with standard variables and with object variables too. In truth is, if you create an object with a Set command at the beginning of a procedure, you might be creating an object for no real purpose. On the other hand, if you delay the creation of an object until you actually need it, Auto-instancing variables are automatically created by Visual Basic only if and when they are referenced. This is probably the situation in which auto-instancing variables are most useful.
• Your object variable cannot be tested against the Nothing value. In fact, as soon as you use one in the Is Nothing test, Visual Basic insistently creates a new instance and the test always returns False
• It eliminate errors, which is sometimes this is specifically what you don't need especially during the development stage, because during this state, you want to see all the errors because this might be the symptoms of other serious deficiency in your code logic.
• Auto-instancing variables make the debugging step a little more difficult to understand because you can never be sure when and why an object was created.
• You can't declare an auto-instancing variable of a generic type, such as Object, or Form because Visual Basic must know in advance which kind of object should be created when it references that variable for the first time.
• Finally, each time Visual Basic references an auto-instancing variable, it incurs a small performance hit each time Visual Basic reference an auto-instancing, because Visual Basic has to check whether it's Nothing.
Now is the time to make our class to more robust class. A robust class is one that actively protects its internal data from tampering. So how can a class protect itself from invalid assignments, such as an empty string for its FirstName or LastName properties. To accomplish this purpose, you must change the internal implementation of the class module, because in its present form you have no means of trapping the assignment operation. Simply change all the Public member into Private members and encapsulate them in pairs of Property procedures.
In addition to class member (property) data. Your custom class can also include Sub and Function procedures, which are commonly known as methods of the class. A method of a class represent some standard operations on the class itself (properties). As you know, the difference between Sub and Function, is that Sub does not return any value, whereas Function method returns a value. But Visual Basic lets you invoke a function and discard its return value.
In our example class, you could easily add a routine that calculate the Student Age.
Re: Classes in Visual basic
Please i need the code for examination time table generator using vb6..please i need it for my project.and it's urgent.
|All times are GMT +5.5. The time now is 12:37.|