HOME  |    TRAINING  |   FREE TUTORIALS   |   JOBS
Find out more about our new RSS feed.
FREE Tutorial
BEGINNING E-COMMERCE PART 6 - INTERFACES IN THE WROXCOMMERCE PROJECT

CATEGORY
SEARCH OUR OTHER TUTORIALS

DESCRIPTION

As we indicated previously, when we create an ActiveX object in Visual Basic, VB does all the hard work of putting the correct interfaces in place so that it can plug itself into Active Scripting, and by association, ASP, Windows Script Host, or anything else that acts as an ActiveX component container. Additionally, VB also creates an interface that just contains the methods and properties that are public on that object. This allows VB to call into that object highly efficiently.
Click here to be kept informed of our new Tutorials.


Try It Out - Using the OLE/COM Viewer

Let's try to illustrate the presence of interfaces by using the OLE/COM Viewer utility that comes with Visual C++ to inspect our recently built ActiveX objects. If you don't have Visual C++ or the Visual C++ tools installed on your machine, you can download the tool from http://www.microsoft.com/com/resources/oleview.asp. If you can't get hold of it, don't worry. This is the best tool to illustrate the interfaces on our object, but you should be able to follow the discussion through the screenshots.

1. Run the OLE/COM Viewer. Expand Object Classes then expand All Objects.

2. Scroll down until you find WroxCommerce.Visit. (You may have to scroll down quite a long way.) Expand the object. This is what our WroxCommerce.Visit object looks like inside this utility:

Look at the list of interfaces shown in the screenshot. The highlighted Visit interface shows the existence of the interface VB builds that just features the methods and properties on our object.

Note, although this is called Visit, it is an interface, not a class.

3. Let's look now at what's inside the Visit interface. To do this, double-click on the Visit object. This will display a little dialog with a button labeled View Type Info. Click this button and you'll see something like this:

4. Lastly, close down the ITypeInfo Viewer and then close down OLE/COM Viewer as well, otherwise we’ll prevent later examples from working.

When we examine the interface, we can see that this object contains only the methods we created on it. Note also that VB has defined these methods as having the parameters we defined against them. COM uses the term BSTR to represent the As String directive that you're familiar with. Notice that the Class_Terminate method has not made it into this interface. As you've probably guessed, that's because it's a private method and is therefore not accessible from outside the object.

This discussion is leading up to how we build another interface on our VB objects that lets us create public methods accessible to other objects in the model, but prevents those methods from being accessible to ASP.

Creating our own Interface

To achieve our aim of creating public methods for access by other objects, but not by ASP, we're going to create an interface definition of our own and then tell the Visit object that it supports that alternative interface. We can then pass that alternative interface through to other objects in the model when they're created.

Try It Out - Creating an Interface in the WroxCommerce Project

5. To kick off, return to the VB Project, add a new class module, and call it IUtility. We use the "I" to signify that it's an interface, in accordance with the COM standards.

6. When you create interface definitions, you simply define the methods and properties. You do not under any circumstances enter code into the methods and properties that are defined in the interface class module. So, it's OK to use the Public Property and Public Function constructs, but no methods or properties can contain any code. The next task is to enter the following:

' DB - property to return a populated and configured database object...
Public Property Get DB() As Database
End Property

' Visit - property to return the Visit object back again...
Public Property Get Visit() As Visit
End Property

To clarify the earlier point about where you can and can't put code, the above code is fine; however, the following two segments of code are both not acceptable:

' DB - property to return a populated and configured database object...
Public Property Get DB() As Database
MsgBox "There's code here. There shouldn't be!"
End Property

Dim Buf As String
Buf = "There's code here. There shouldn't be!"

' DB - property to return a populated and configured database object...
Public Property Get DB() As Database
End Property

Literally, the only things that can be in an interface class module are lines that are blank lines, lines that are comments, lines that start Public Property or Public Function, and lines that start End Property or End Function.

7. Our next job is to tell the Visit object that it implements (in other words - supports) that interface. So make the following addition to the Visit class module:

' this variable holds the connection string needed to
' connect to the database...
Private m_strDBString as String

' tell it what interfaces it supports...
Implements IUtility

When we tell an object that it implements an interface we enter into a contract with VB that we will add functions in order for the object to handle each of the methods and properties in the interface.

8. In the object drop-down list box in the top left of the code window, you should now be able to find the IUtility interface. Selecting that will display a list of the methods and properties on the interface, as shown here:

Continued...


NEXT PAGE



5 RELATED COURSES AVAILABLE
I-NET+ MODULE 13 - E-COMMERCE
On completion of this module, readers will be able to understand e-commerce terms and concepts.....
HTML 4.0 INTRODUCTION
To create, format and publish a small website using HTML 4.0. You will learn to create web pages incorporating fo....
MACROMEDIA DREAMWEAVER MX INTRODUCTION
To give an introduction to the Internet tools and features of Macromedia Dreamweaver MX. Readers will create an a....
MICROSOFT INTERNET EXPLORER 6.0 INTERNET INTRODUCTION
This course provides readers with an introduction to the concept of the Internet and the opportunity to gain a br....
A+ MODULE 5 - THE INTERNET
At the end of this course you will be able to: describe the functions of an operating system, describe the featur....
 
0 RELATED JOBS AVAILABLE
CONTACT US
Thursday 9th February 2012  © COPYRIGHT 2012 - website design by Website Design by Visualsoft