Class Inheritance

Class inheritance might be the single most exciting innovation introduced for Visual Basic and VBA developers with Visual Basic .NET. Authors performing comparative language reviews were quick to point out that classic Visual Basic did not support class inheritance. Visual Basic .NET corrects that deficiency. Class inheritance is an important capability because it provides a means for adapting and extending code in previously created class libraries. With class inheritance, a new class can build on the code in an old class while concurrently adding new functionality and redefining the way old class members work. In short, you can think of class inheritance as a tool for creating classier classes.

Now that we have class inheritance, our challenge as Access database developers is to find practical ways of putting it to use in our applications. Before diving into this section, you need a good grasp of how to use classes. The overview in the Classes section in Chapter 3 presents basic coding techniques for class libraries that you will find useful to know as you read this section. The introduction to that section in Chapter 3 succinctly describes how class inheritance helps you to get more value out of your classes. So if you re feeling a little hazy on classes, now would be a good time to revisit that discussion.

This section has a couple of practical objectives. First, it briefly reviews key class concepts as a prelude to reviewing keywords that help you implement class inheritance with Visual Basic .NET. Second, the section demonstrates the use of these keywords with samples that work with Access databases.

Overview of Concepts and Keywords

Classes deliver benefits in three main areas. First, classes facilitate encapsulation. This benefit allows you to use a class and take advantage of its functionality without needing to know the inner workings of how that class exposes methods and properties as well as propagates events. Second, classes can enable inheritance. This section focuses on showing you how to take advantage of this benefit. You can inherit one class, known as a base class , into another class, known as a derived class . The derived class can make available all the members (properties, methods, and events) of the base class. This feature takes advantage of the encapsulation benefit, but a derived class does not have to create an instance of the base class to tap the members of its base class. In addition, derived classes can supplement base class members with new members. When applications instantiate objects based on a derived class, the instantiated object can expose members for both the base class and the derived class. The third main benefit of classes is polymorphism. This capability allows derived classes to expose members that work differently in the derived class than they do in the base class. Polymorphism enables you to change the definition of a base class in a derived class. In addition, an application instantiating an object for the derived class can access the original members of the base class or the modified members of the derived class.

The single most important concept of class inheritance to grasp is the Inherits keyword. This keyword permits a derived class to inherit the members of a base class. Inherits must appear in the first line of code in any derived class. Its syntax is very straightforward. The keyword takes one argument ”the name of the base class. In addition, your derived class must have a reference to the class library project serving as the base class. The Inherits statement in the derived class will not function unless this project reference exists for the base class.

The matter of references is important when working with derived classes. Any application instantiating objects based on a derived class must have a reference to the derived class as well as to the base class for the derived class. For example, if projectA instantiates objects based on a derived classB in projectB and the base class for classB is classC in projectC, then projectA must have references to both projectB and projectC.

You will likely use a couple of keywords in a derived class ” especially when you are redefining the operation of members. These keywords point at either the base class ( MyBase ) or the derived class ( MyClass ). By using the keywords as qualifiers for member names , you can clearly distinguish in a derived class among references to base class members and derived class members. In the following samples, you will discover that the base class and the derived class define the TotalOrder method differently. By using MyBase.TotalOrder , your code can refer unambiguously to the base class implementation of the method. Similarly, MyClass.TotalOrder designates the derived class implementation of the method.

Three additional keywords help you take advantage of the polymorphism benefit. When you plan to change a member in a derived class with the same signature (name, type, and arguments) as a member in a base class, you can use the Overridable and Overrides keywords. Mark the base class member with the Overridable keyword, and apply the Overrides keyword to the corresponding derived class member. To change the implementation of a member with the Overrides keyword, you must anticipate the need in your base class by applying the Overridable keyword to a member. If you decide to change a base class to add the Overridable keyword after it is compiled, you must recompile the base class and refresh the reference of the derived class for the base class. The Shadows keyword allows you to more flexibly redefine base class members in a derived class. When a derived class member shadows a base class member, the base class member does not require any special keyword. In addition, the signature for base class members and derived class members can differ . Therefore, you can use a property member in a derived class to redefine a member initially defined as a method in the base class.

Overloads is another keyword that implements a style of shadowing within a class. .NET Framework classes commonly offer overloaded constructors. An overloaded constructor is actually a set of New methods within a class. Each New method has the same name, but the list of arguments for each constructor is different. For example, an object offering database functionality can use a default record source. Therefore, its default constructor will not need an argument designating a record source. On the other hand, you can obtain more functionality from the class by letting a user specify his own record source for the class when constructing a new instance. By overloading the default constructor with a different New method that takes an argument for the record source, you can accommodate this expanded functionality for the class. The great thing about overloaded constructors is that Visual Basic .NET figures out what constructor to use based on the arguments you provide! With constructors, you do not actually use the Overloads keyword, although the .NET Framework implements overloaded constructors through its overloading capability.

You can also use the Overloads keyword with functions implementing methods in a class. For example, you can have a class with different functions for computing the areas of geometric shapes , such as squares, triangles , and trapezoids. A piece of sample code in the Parameter Arrays section in Chapter 3 showed how to implement this task with a single function procedure that takes a parameter array. Those familiar with class programming methods will probably find using a single class with an overloaded Area method simpler to code. By applying the Overloads keyword to three different Area methods, you can specify area expressions for each shape. Because each shape requires a different number of arguments, the signature for the method corresponding to each shape will be distinct. Again, the beauty of overloading is that Visual Basic .NET will automatically figure out which function to use for implementing the Area method based on the number and type of arguments passed when invoking the method.

Setting Up for Class Inheritance Demonstrations

When you run applications using class inheritance, you will typically have at least three projects to manage. First, you will need a project for your base class. You should make this a class library project. The project can contain as many different base classes as you need. Second, you will need another project for your derived class. This project must include a reference to the project containing the base class. Third, you need a project for the application that creates one or more instances of derived classes in the second project. This project must contain references to the .dll files for the projects containing the base class and the derived class.

Note  

You can include a class within another project for demonstration and quick reference, but this has limited utility because multiple other projects will not be able to reference the class. When working with class inheritance, it is even more useful to isolate classes in their own projects.

Base Class Features

The base class, MyAnalyzer , resides in the ADODBOrderDetailAnalyzer project. This is a class library project. As with all Visual Basic projects, this project has its own folder named after it. Within the bin subdirectory of that folder is the .dll file for the class library. Therefore, the ADODBOrderDetailAnalyzer.dll file resides in the bin folder of the ADODBOrderDetailAnalyzer folder. This .dll file contains the compiled code for the MyAnalyzer class.

The MyAnalyzer class implements a method named TotalOrder and a read-only property named OrderLineItems . You should be familiar with the techniques for implementing both the method and the property by now because they borrow from previous samples in this chapter as well as Chapter 3. The TotalOrder method computes the total extended price for an OrderID value passed as a value when invoking the method, and the property returns the number of line items in the order passed to the method.

The listing that follows highlights in boldface the code lines that are special to this class in one way or another. For example, notice the Overridable keyword appearing in the declaration of the TotalOrder function for the method by the same name. Using this keyword in the function declaration enables a derived class to override the implementation for the TotalOrder method in the base class. The property returning the number of line items is initially computed as the value for a private field, m_count . The sample uses a Byte data type for the OrderLineItems property because the number of line items is expected to be less than 256 but always positive. The OrderLineItems property procedure exposes the m_count field value as a read-only property.

  Public Class MyAnalyzer   Private m_count As Byte   Overridable Function TotalOrder(ByVal OrderID As Integer) As Decimal  Declare and instantiate ADODB objects. Dim cnn1 As New ADODB.Connection() Dim rst1 As New ADODB.Recordset() Open an ADO Connection object. cnn1.Open("Provider=Microsoft.Jet.OLEDB.4.0;" & _ "Data Source=C:\Program Files\" & _ "Microsoft Office\Office10\Samples\Northwind.mdb;", _ "Admin", "") Open recordset for rows from Order Details, matching OrderID. Dim str1 As String = _ "SELECT * FROM [Order Details] " & _ "WHERE OrderID =" & OrderID.ToString Open a recordset on a table. With rst1 .ActiveConnection = cnn1 . Open (str1, , _ ADODB.CursorTypeEnum.adOpenForwardOnly, _ ADODB.LockTypeEnum.adLockReadOnly) End With Loop through the ADO recordset and compute the total for the designated OrderID. Dim Quantity As Integer Dim UnitPrice As Decimal Dim Discount As Decimal Dim OrderTotal As Decimal Dim byt1 As Byte Do Until rst1.EOF Quantity = CInt(rst1("Quantity").Value) UnitPrice = CDec(rst1("UnitPrice").Value) Discount = CDec(rst1("Discount").Value) OrderTotal = _ Quantity * UnitPrice * (1 - Discount) + OrderTotal byt1 += 1 rst1.MoveNext() Loop  Count of line items in order   m_count = byt1   Return OrderTotal value from TotalOrder method.   Return OrderTotal  End Function  ReadOnly Property OrderLineItems() As Byte   Get   Return m_count   End Get   End Property  End Class 

Derived Class Features

The derived class, MyADODBNorthwind , resides in the ADODBNorthwind project. The project actually contains two classes. The second class, Rows , is not a derived class. The Rows class demonstrates how to implement overloaded constructors.

Notice that the first line in the MyADODBNorthwind class is the Inherits keyword. This keyword points back to the MyAnalyzer class in the ADODBOrderDetailAnalyzer project. For the Inherits statement to function properly, the ADODBNorthwind project requires a reference to the ADODBOrderDetailAnalyzer.dll file. You can achieve this by opening the Add Reference dialog box, selecting the Projects tab, navigating to the .dll file, and closing the Add Reference dialog box to confirm your selection.

The first function, MyTotalOrder , actually exposes the implementation for the TotalOrder method in the base class. The use of the MyClass keyword passes a reference back to the base class.

The second function, TotalOrder , overrides the implementation of the TotalOrder method by the function with that name in the base class. The MyADODBNorthwind class uses the TotalOrder function to return the number of line items as a Decimal data type. The function uses the Decimal data type for the return value to keep the signature for the function the same as in the base class. Failing to maintain the data type as Decimal for the return value from the function causes a compilation error.

The third function is a new method, MyPercentIncrease , which is added by the derived class. This method is not available in the base class. The method takes two arguments. One specifies the OrderID for an order. The second argument, Percent , indicates how much to increase the total extended price for an order on a percentage basis. Specifying a Percent argument value of 1.1 increases the total extended price for the order matching the OrderID value by 10 percent. Although this method requires the value returned by the TotalOrder method from the base class, it requires just one line. The expression Me.MyTotalOrder refers to a function in the current class that, in turn , refers back to the TotalOrder method in the base class. In this way, the MyPercentIncrease method uses the TotalOrder base class method to create a new class.

The InheritedLineItems property procedure directly passes back the OrderLineItems property from the base class. This approach is desirable when you shadow a base class property with another property or method but still want to retain access to the original property in the base class. The MyADODBNorthwind class concludes with an example of how to shadow a base class property with a derived class method. The old definition of the OrderLineItems property in the base class returns the number of line items for the order designated in the base class implementation of the TotalOrder method. The new definition of the OrderLineItems method in the derived class is the total number of line items for a range of orders, from a beginning OrderID through an ending OrderID .

The Rows class that appears after the MyADODBNorthwind class demonstrates the syntax for overloading a constructor. This class creates a recordset with one of two constructors. If the user does not specify a record source argument for the Rows class, the recordset automatically points at the Shippers table from the Northwind database. If the user designates a record source by using the second constructor, the function uses the table or row-returning query from the Northwind database designated by the string argument. The recordset is private to the Rows class, but two properties expose the Source property for the recordset as well as the number of rows in the recordset. The Source property for a recordset based on a Jet database is a SQL string designating the source specified by the ActiveConnection property for the recordset.

 Public Class MyADODBNorthwind Inherits ADODBOrderDetailAnalyzer.MyAnalyzer Function MyTotalOrder(ByVal OrderID As Integer) As Decimal Return (MyBase.TotalOrder(OrderID)) End Function Overrides Function TotalOrder(ByVal OrderID As Integer) _ As Decimal Declare and instantiate ADODB objects. Dim cnn1 As New ADODB.Connection() Dim rst1 As New ADODB.Recordset() Open an ADO Connection object. cnn1.Open("Provider=Microsoft.Jet.OLEDB.4.0;" & _ "Data Source=C:\Program Files\" & _ "Microsoft Office\Office10\Samples\" & _ "Northwind.mdb;", _ "Admin", "") Open recordset for rows from Order Details, matching OrderID. Dim str1 As String = _ "SELECT * FROM [Order Details] " & _ "WHERE OrderID =" & OrderID.ToString Open a recordset on a table. With rst1 .ActiveConnection = cnn1 .Open(str1, , _ ADODB.CursorTypeEnum.adOpenForwardOnly, _ ADODB.LockTypeEnum.adLockReadOnly) End With Loop through the ADO recordset and compute the total for the designated OrderID. Dim Quantity As Integer Dim UnitPrice As Decimal Dim Discount As Decimal Dim OrderTotal As Decimal Dim int1 As Integer Do Until rst1.EOF Convert ADODB recordset line items to Visual Basic .NET data types. Quantity = CInt(rst1("Quantity").Value) UnitPrice = CDec(rst1("UnitPrice").Value) Discount = CDec(rst1("Discount").Value) Compute total for order. OrderTotal = _ Quantity * UnitPrice * (1 - Discount) + OrderTotal int1 += 1 rst1.MoveNext() Loop Return OrderTotal value from TotalOrder method as a count of the number of line items. Return CDec(int1) End Function Function MyPercentIncrease(ByVal OrderID As Integer, _ ByVal Percent As Double) As Decimal Return Me.MyTotalOrder(OrderID) * Percent End Function ReadOnly Property InheritedLineItems() Get Return MyBase.OrderLineItems() End Get End Property Shadows Function OrderLineItems( _ ByVal FirstOrderID As Integer, _ ByVal SecondOrderID As Integer) As Integer Declare and instantiate ADODB objects. Dim cnn1 As New ADODB.Connection() Dim rst1 As New ADODB.Recordset() Open an ADO Connection object. cnn1.Open("Provider=Microsoft.Jet.OLEDB.4.0;" & _ "Data Source=C:\Program Files\" & _ "Microsoft Office\Office10\Samples\Northwind.mdb;", _ "Admin", "") Open recordset for rows from Order Details, matching OrderID. Dim str1 As String = _ "SELECT * FROM [Order Details] " & _ "WHERE OrderID >=" & FirstOrderID.ToString & _ " AND OrderID <=" & SecondOrderID.ToString Open a recordset on a table. With rst1 .ActiveConnection = cnn1 .Open(str1, , _ ADODB.CursorTypeEnum.adOpenKeyset, _ ADODB.LockTypeEnum.adLockOptimistic) End With Return rst1.RecordCount End Function End Class Public Class Rows Private rst1 As New ADODB.Recordset() Sub New() Declare and instantiate ADODB objects. Dim cnn1 As New ADODB.Connection() Open an ADO Connection object. cnn1.Open("Provider=Microsoft.Jet.OLEDB.4.0;" & _ "Data Source=C:\Program Files\" & _ "Microsoft Office\Office10\Samples\Northwind.mdb;", _ "Admin", "") Open recordset for rows from Shippers table. Dim str1 As String = _ "SELECT * FROM Shippers" Open a recordset on a table. With rst1 .ActiveConnection = cnn1 .Open(str1, , _ ADODB.CursorTypeEnum.adOpenKeyset, _ ADODB.LockTypeEnum.adLockOptimistic) End With End Sub Sub New(ByVal RecordSourceName As String) Declare and instantiate ADODB objects. Dim cnn1 As New ADODB.Connection() Open an ADO Connection object. cnn1.Open("Provider=Microsoft.Jet.OLEDB.4.0;" & _ "Data Source=C:\Program Files\" & _ "Microsoft Office\Office10\Samples\Northwind.mdb;", _ "Admin", "") Open recordset for rows from Shippers table. Dim str1 As String = _ "SELECT * FROM " & RecordSourceName Open a recordset on a table. With rst1 .ActiveConnection = cnn1 .Open(str1, , _ ADODB.CursorTypeEnum.adOpenKeyset, _ ADODB.LockTypeEnum.adLockOptimistic) End With End Sub ReadOnly Property RowCount() As Integer Get Return rst1.RecordCount End Get End Property ReadOnly Property RowSource() As String Get Return rst1.Source End Get End Property End Class 

Application Project Features

To make the process for invoking the classes as obvious as possible, the sample uses a project based on the Empty Project template. This type of project has no customization or built-in references to assist or interfere with the invoking of class methods and properties. The project in the sample has the name ADODBNorthwindLibUser. This project demonstrates the syntax for using the classes defined in the ADODBNorthwind project.

The project has a single module, Module1 , with a main procedure and a series of Sub procedures. Each Sub procedure invocation illustrates a different aspect of working with class inheritance or overloading. These Sub procedures, in turn, invoke properties and methods in the derived class, MyADODBNorthwind , or the Rows class that demonstrates overloading syntax for a New method serving as a constructor.

The names of all the Sub procedure calls are commented out in the main procedure. I tested the Sub procedure calls by removing the comments for each Sub procedure one at a time. You can interpret the outcome of each Sub procedure because they each present one or more message boxes to display values returned by methods and properties from the classes in the ADODBNorthwind.dll.

For the code in the ADODBNorthwindLibUser project to compile correctly, you must add references to the MyADODBNorthwind.dll file and the ADODBOrderDetailAnalyzer.dll file. To do so, verify that the projects for the .dll files are on a workstation and are compiled. You generate the .dll file for a project when you invoke the Build, Build Solution command or Build, Rebuild Solution command. You add the .dll for a referenced component with the Project, Add Reference command. Your application depends on the application project folder and its related component project folders. Therefore, you can ship a solution that invokes classes just by shipping the application s project folder and its related component project folders. You do not need any special installation program that makes registry entries. In this sample, just copy the three folders for the ADODBOrderDetailAnalyzer, ADODBNorthwind, and ADODBNorthwindLibUser projects to a directory on any computer with the .NET Framework installed, the Northwind.mdb file in its default location for Access 2002, and the 2.5 version of the ADODB library installed on the computer. This library installs automatically with Access 2002.

 Module Module1 Sub main() Dim OrderID As Integer = 10248 Dim PercentGrowth As Double = 1.1 BaseMethod(OrderID) BaseMethodandProperty(OrderID) DerivedMethod(OrderID, PercentGrowth) OverriddenBaseMethod(OrderID) Dim SecondOrderID As Integer = 10250 MethodShadowsProperty(OrderID, SecondOrderID) OverloadedConstructor("Categories") End Sub Sub BaseMethod(ByVal OrderID As Integer) Instantiate object based on MyADODBNorthwind class. Dim MyInstance As _ New ADODBNorthwind.MyADODBNorthwind() Invoke base class implementation of TotalOrder method through derived class, MyTotalOrder method. Dim dec1 As Decimal = _ MyInstance.MyTotalOrder(OrderID) Display return from base class method. MsgBox(dec1.ToString, , _ "From BaseMethod") End Sub Sub BaseMethodandProperty(ByVal OrderID As Integer) Instantiate object based on MyADODBNorthwind class. Dim MyInstance As _ New ADODBNorthwind.MyADODBNorthwind() Invoke derived class method and property that are mapped to base class method and property. Dim dec1 As Decimal = _ MyInstance.MyTotalOrder(OrderID) Dim dec2 As Decimal = _ MyInstance.InheritedLineItems() Display return from base class method and property. MsgBox(dec1.ToString & ", " & dec2.ToString, , _ "From BaseMethodandProperty") End Sub Sub DerivedMethod(ByVal OrderID As Integer, _ ByVal PercentGrowth As Double) Instantiate object based on MyADODBNorthwind class. Dim MyInstance As _ New ADODBNorthwind.MyADODBNorthwind() Invoke new derived class method. Dim dec1 As Decimal = _ MyInstance.MyPercentIncrease(OrderID, _ PercentGrowth) Display return from derived class. MsgBox(dec1.ToString, , _ "From DerivedMethod") End Sub Sub OverriddenBaseMethod(ByVal OrderID As Integer) Instantiate object based on MyADODBNorthwind class. Dim MyInstance As _ New ADODBNorthwind.MyADODBNorthwind() Invoke derived class method overridding base class method. Dim dec1 As Decimal = _ MyInstance.TotalOrder(OrderID) Display return from derived class overriding method. MsgBox(dec1.ToString, , _ "From OverriddenBaseMethod") End Sub Sub MethodShadowsProperty( _ ByVal FirstOrderID As Integer, _ ByVal SecondOrderID As Integer) Instantiate object based on MyADODBNorthwind class. Dim MyInstance As _ New ADODBNorthwind.MyADODBNorthwind() Invoke derived class method shadowing base class property. Dim int1 As Integer = _ MyInstance.OrderLineItems(FirstOrderID, _ SecondOrderID) Display return from derived class shadowing method. MsgBox(int1.ToString, , _ "MethodShadowsProperty") End Sub Sub OverloadedConstructor(ByVal TableName As String) Instantiate object based on Rows class with default constructor not specifying a record source. Dim MyInstance As _ New ADODBNorthwind.Rows() Display return from RowSource and RowCount properties based on default constructor. MsgBox(MyInstance.RowSource & ", " & _ MyInstance.RowCount.ToString, , _ "OverloadedConstructor") Instantiate object based on Rows class with constructor allowing specification of a record source. Dim MyInstance2 As _ New ADODBNorthwind.Rows(TableName) Display return from RowSource and RowCount properties based on constructor allowing specification of a record source. MsgBox(MyInstance2.RowSource & ", " & _ MyInstance2.RowCount.ToString, , _ "OverloadedConstructor") End Sub End Module 

Running the Application Project

Because each Sub procedure called by the main procedure in the ADODBNorthwindLibUser project displays at least one message box, the samples are self-explanatory. The main routine contains six Sub procedure calls. This section gives you a feel for how to explore this sample application by reviewing the operation of the second, fifth, and sixth procedure calls.

The second Sub procedure has the name BaseMethodandProperty . This procedure ultimately invokes the TotalOrder method and returns a value from the OrderLineItem property from the MyAnalyzer class in the ADODBOrderDetailAnalyzer project. However, the BaseMethodandProperty procedure invokes the base class method and property by invoking a derived class method and property that, in turn, references the base class method and property. This approach enables you to expose base class methods and procedures even when you directly reference a derived class.

The BaseMethodandProperty procedure takes an OrderID argument defined in the main routine as 10248 in a Dim statement. The procedure then references the MyADODBNorthwind class with the MyInstance variable. Next, the procedure saves the return value from the MyTotalOrder method in the dec1 variable. The return value is the total extended price based on the TotalOrder method in the MyAnalyzer class of the ADODBOrderDetailAnalyzer project. You can confirm this by referring to the listing for the MyTotalOrder method in the ADODBNorthwind project, which references the base class method with the expression MyBase.TotalOrder(OrderID) . The BaseMethodandProperty procedure then references the InheritedLineItems property in the derived class. However, the property procedure for the InheritedLineItems property references the base class implementation of the OrderLineItems property with the expression MyBase.OrderLineItems .

Figure 4-7 shows the results of running the BaseMethodandProperty procedure. Its message box contains two numbers . The first one is 440, which is the total extended price for the order with an OrderID of 10248. The second number is 3, which is the number of line items in the order.


Figure 4-7: A message box showing results from a base class method and property obtained by invoking a derived class method and returning a value from a derived class property

To run the fifth procedure, MethodShadowsProperty , you must remove the comment marker from the procedure call in the main routine. This procedure requires two arguments ”one for a beginning OrderID value and a second for an ending OrderID value. The value of OrderID should be less than or equal to the value of SecondOrderID . I report results for the OrderID and SecondOrderID values that appear in the main procedure listing just shown. The Sub procedure invokes the OrderLineItems method in the derived class. This method shadows, or obscures, the visibility of the base class property by the same name. Shadowing is one of the most powerful inheritance techniques for supporting polymorphism because any derived class member can shadow any base class method ”even if they are of different types, as in this case, where the base class member is a property and the derived class member is a method.

Figure 4-8 shows the results of running the MethodShadowsProperty procedure. The message box shows a value of 8. By reviewing the code for the OrderLineItems method in the derived class, you can see this method returns the total number of line items for a range of orders, from the beginning order to the ending one. The OrderID and SecondOrderID values in the previous sample listing specify the return of the sum of the line items for the orders with OrderID values of 10248 through 10250. You can readily confirm that 8 is a valid return from the Order Details table in the Northwind database.


Figure 4-8: The MethodShadowsProperty procedure, which returns the value 8 in its message box when run with default beginning and ending OrderID and SecondOrderID values of 10248 and 10250

The OverloadedConstructor procedure is the only one in the sample application to instantiate an object based on the Rows class in the ADODBNorthwind project. The New method for the Rows class is overloaded. That is, it contains two or more different versions of the New function ”each with a different argument list. In this case, only two versions exist. The default version with no argument creates an object instance based on the Shippers table from the Northwind database; as mentioned, this table initially ships with three rows. However, you can specify any other table or row-returning query from the Northwind database when you instantiate an instance of the Rows class. If you name a table or query, you automatically invoke the second version of the New method.

The OverloadedConstructor procedure instantiates two objects, MyInstance and MyInstance2 , based on the Rows class. Each object is based on a different version of the New method. Both New method versions create a recordset based on either the Shippers table in the first instance or the Categories table in the second instance. The procedure also displays a message box after instantiating an object that returns the RowSource and RecordCount properties for the recordset underlying the instantiated object.

Figure 4-9 shows the result from running the OverloadedConstructor procedure. When creating instances based on a class with overloaded methods, you can obtain different outcomes just by using different arguments. The top message box shows the result of running the default version of the New method for the Rows class. As you ve learned, this method constructs an object based on the Shippers table. The RecordSource property, which is a simple SELECT statement for all the rows and columns from the Shippers table, appears first. The second item shows the number of rows in the Shippers table. The second message box displays the outcome from using the second version of the New method with an argument of Categories.


Figure 4-9: Two message boxes resulting from two different invocations of the New method for the Rows class
 


Programming Microsoft Visual Basic. NET for Microsoft Access Databases
Programming Microsoft Visual Basic .NET for Microsoft Access Databases (Pro Developer)
ISBN: 0735618194
EAN: 2147483647
Year: 2006
Pages: 111
Authors: Rick Dobson

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