First a more general overview of OOP and then into the VB version.
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.
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 |
Public Property Get classnum() As String End Property Public Property Let classnum(ByVal vNewValue As String) End PropertyThen 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 courseBut this doesn't actually create a course object. That is done using the New keyword.
set cls = new courseNow cls is a course object and we can start using its properties and methods.
cls.classname = "Visual Basic" Dept.Text = cls.DeptNote 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 courseThe 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.
C:\kent\Teaching\gbook\courseobj>regsvr32 gradeslib.dllYou should see this box if it worked.