Object Oriented Programming in VB

First a more general overview of OOP and then into the VB version.

Why OOP?

What problem were we trying to solve? Mostly complexity and re-use. People noticed that code that was grouped together with the data it manipulated was easier to follow. Also, complex data problems could be simplified by making a graph of them.

Software re-use

Code is hard to re-use since everyone codes the interfaces slightly differently. At least differently than you want them to be. Classes allow a developer to present an interface to the user so they can access the data without messing with the internals.

Control of complexity

Systems with a large number of related but different data objects were hard to understand. Inheritance and access control makes that easier. Magic number 7 + or - 2. People can only hold about 7 things in their head at the same time. So if you can lump things together, you can hold more of the program in your head.

Code understanding

Similar problem. Break code into smaller modules to ease understanding.

Definitions and principles

Encapsulation

Data and code together. Object data can only be used by object code. Attached code called methods. Also known as member functions. Like user defined types with code fields.

Polymorphism

Multiple functions with the same name. Allows more natural function naming. Most data objects have a print operation. Now all can be called print().

Inheritance

There is often a natural hierarchy to data objects. All rooms have some things in common like walls and doors. But some rooms have windows. Some rooms have plumbing. Some walls go to the ceiling and some don't. Rather than describe everything for each kind of room, create a generic room object and have the special rooms add to that, subtract from that or overload that.

Client/server model

An object in an object-oriented system encapsulates all we know about a kind of data. If we have a person class that contains a persons name, address, etc. as well as methods that change these fields, link people together into groups, etc., we can view the person class as a server. All other programs that use a person object are clients of it, They make requests, using the methods, and the objects completes t hem and give the results back to the client. This is similar, without the networking, to the way a web browser talks to a web server.

A class is like a record but it includes fields that are functions. These are called member functions or methods. Some of the fields are public and some are private. Private functions and data are only available to other functions inside the class. Public functions and data are available to outside code. Need member functions to read and set the private data as outside functions won't be able to. Each instance of a class has a new instantiation of the private variables.

Overloading

Function names can be overloaded. That is, multiple versions of a function can exist with the same name.

Constructors

Called when a variable of that type is declared.

Destructors

Called when an instance of a class goes out of scope. This means it can't be seen anymore and so can be destroyed. The destructor de-allocates storage and any other housekeeping needed. It is called implicitly.

Member functions (methods)

Member functions don't have to specify the private members of the class.

OOP in VB

Visual Basic supports OOP but differently than other languagaes. For example, there is no 'class' reserved word to mark a class. This is done by creating a class module and putting things in it. In C++ or Java, the contents of a class are contained in a class statement.

Building a new class is pretty easy. First we will handle it by adding classes to aproject. A little later we will create a class library. Under the Project menu, there is an Add Class Module This creates an empty class. You should give it a meaningful name. Then you can start adding member data and methods. If the data items are marked Public, programs the use the class can access them directly. Genarally, this is not what you want. Part of the OO idea of encapsulation is to hide the internal workings of the class from the user. This way, if you need to change the internals, it doesn't require the user to change their code.

In VB, you can hide the member data, called properties in VB, but still access them as if they were simple public variables. Here is some example code, it represents the course object. Next to it is the more or less equivalent C++ code.
Option Explicit

Private m_classname As String
Private m_classnum As String
Private m_school As String
Private m_Dept As String


Public Property Get classname() As String
    classname = m_classname
End Property

Public Property Let classname(ByVal vNewValue As String)
    m_classname = vNewValue
End Property

Public Property Get classnum() As String
    classnum = m_classnum
End Property

Public Property Let classnum(ByVal vNewValue As String)
    m_classnum = vNewValue
End Property
Public Property Get school() As String
    school = m_school
End Property

Public Property Let school(ByVal vNewValue As String)
    m_school = vNewValue
End Property

Public Property Get dept() As String
    dept = m_Dept
End Property

Public Property Let dept(ByVal vNewValue As String)
    m_Dept = vNewValue
End Property

Public Function show() As String
    Dim str As String
    str = "Class Num = " & m_classnum
    str = str & " Dept = " & m_Dept
    str = str & " School = " & m_school
    str = str & " Name is " & m_classname
    show = str
End Function
class course {
	public:
		char * get_classname() { return(strdup(m_classname);}
		char * get_classnum() { return(strdup(m_classnum);}
		char * get_school() { return(strdup(m_school);}
		char * get_Dept() { return(strdup(m_Dept);}
		void set_classname(char * str) { strcpy(m_classname,str);}
		void set_classnum(char * str) { strcpy(m_classnum,str);}
		void set_school(char * str) { strcpy(m_school,str);}
		void set_Dept(char * str) { strcpy(m_Dept,str);}
		char * show();
	private:
		char m_classname[MAXSTR];
		char m_classnum[MAXSTR];
		char m_school[MAXSTR];
		char m_Dept[MAXSTR];
};
char *
course::show()
{
	int n;
	char * str;
	n = strlen(m_classname) + strlen(m_classnum) +
	    strlen(m_school) + strlen(m_Dept);
        str = new char[ n + 1 + 45];  // 45 to allow for labels and counting errors
	(void) sprintf("Class Num = %s Dept = %s School = %s Name = %s",m_classnum, m_Dept, m_school, m_classname);
	return str;
} // show
The easiest way to get the special property methods into the file is to use the Tools menu and choose the Add Procedure option. You get a window like this:
The name you fill in will be the name that users use to access the data. It shouldn't be the same as the private data elment. As above, the outside name is classname, but the private data name is m_classname. So I entered classname for the name. Choose the Property button and Public then click OK. Two methods will appear in the code like:

Public Property Get classnum() As String

End Property

Public Property Let classnum(ByVal vNewValue As String)

End Property
	
Then you fill in the code you want. It could convert data types. For example, the user sees a string variable, but you store the value internally as an integer and the Let method translates using a select statement. You can make the property read only by deleting the Let method or even write only by deleting the Get method. If the property is only used in the class, don't have either method.

You can add other methods, like the show() method above. Note that the methods on the class don't have to use the Let and Get methods, the access the private data directly.

Once you have created the class, you can use it like other data types. The difference is that there is a separation in VB between object variables and object instances. This is very like the difference between pointers and the things they point at in C++. You create an object variable like you do others.

	Dim cls as course
But this doesn't actually create a course object. That is done using the New keyword.
	set cls = new course
Now cls is a course object and we can start using its properties and methods.
	cls.classname = "Visual Basic"
	Dept.Text = cls.Dept
Note that we have to use the keyword Set when we assign new objects. We can combine the two steps like this
	Dim cls As New course
The first method is known as late binding. This means that cls can actuallt represent several course objects over time. We can also declare variables of type object that allows them to refer to many kinds of objects. Kind of like Variant.

ActiveX DLL

Registration

C:\kent\Teaching\gbook\courseobj>regsvr32 gradeslib.dll
You should see this box if it worked.

Reference

You should this box: