John W. Colby
jwcolby at colbyconsulting.com
Wed Mar 2 10:21:50 CST 2005
>In my programming career, I've probably written 300,000 lines of code, but none of it in classes. And you're probably rolling your eyes about now! Not at all. I learned OO programming using TurboPascal in the late 80s. When I moved to Access AFAIK it didn't have classes back then so I "lost it", went back to procedural methods simply because that was all the tool provided. I only started using classes in Access about 5 years ago. I must say that I now use them a LOT. Sooo... Here ya go, about as simple as it gets, but it demonstrates admirably the benefits of a class. Insert a class and cut and paste this code in to your database. Save it as clsTimer. Option Compare Database Option Explicit ' ' Private Declare Function apiGetTime Lib "winmm.dll" _ Alias "timeGetTime" () As Long Private lngStartTime As Long 'THESE FUNCTIONS / SUBS ARE USED TO IMPLEMENT CLASS FUNCTIONALITY '*+Class function / sub declaration Function EndTimer() EndTimer = apiGetTime() - lngStartTime End Function Sub StartTimer() lngStartTime = apiGetTime() End Sub '*-Class function / sub declaration This class is a "timer" of something happening down to a millisecond resolution. To use it you simply dim the class and call the StartTimer method. Dim lclsTimer as clsTimer set lclsTimer = new clsTimer lclsTimer.StartTimer . . .some piece of code you want to time . . debug.print lclstimer.EndTimer To see the advantage of using it over "the competition", suppose you need to time an inner and outer loop of code... (or how long a couple of queries take to run?) Dim XInner as long Dim YOuter as long Dim lclsTimerInner as clsTimer Dim lclsTimerOuter as clsTimer set lclsTimerInner = new ClsTimer set lclsTimerOuter as new clsTimer lclsTimerOuter.StartTimer while YOuter = 1 to 2000 . .lots of code being timed . lclsTimerInner.StartTimer while Xinner = 1 to 15 . . .inner loop code being timed . wend debug.Print "Inner Loop: " & lclsTimerInner.EndTimer . .more code . wend debug.print "Outer Loop: " & lclsTimerOuter.EndTimer Because the "data" is stored internally to the class, you can use one or a thousand instances of this class, anywhere in your code you need to time something. You don't have to set up global variables to hold the data, you don't have to use a collection to hold the various timer values (some alternatives to a class) etc. The data for the timer is with the timer class instance. The variable holding the class instance is where the timing occurs. Can you do this another way? Of course but it isn't clean and elegant. The alternatives require storing the "time" somewhere "global" and then indexing in to get the times (collections or arrays) or setting up a new global variable for each timer and passing in the variable name or something similar. YUK Classes take a little more code to set up (the DIM and the SET (instantiation)), but where they make sense they will provide a much cleaner program. The timer is about the simplest useful class I have ever run into. Most classes have many variables holding data and many methods for processing. But start simple. Use the timer, learn how to Dim and Set a class pointer, how to call methods etc. Once you understand that then move on. John W. Colby www.ColbyConsulting.com Contribute your unused CPU cycles to a good cause: http://folding.stanford.edu/ -----Original Message----- From: dba-tech-bounces at databaseadvisors.com [mailto:dba-tech-bounces at databaseadvisors.com] On Behalf Of Dan Waters Sent: Wednesday, March 02, 2005 9:39 AM To: 'Discussion of Hardware and Software issues' Subject: RE: [dba-Tech] C++ to vb.net conversion John, In my programming career, I've probably written 300,000 lines of code, but none of it in classes. And you're probably rolling your eyes about now! Do you have a very simple example of a class, or have a tutorial somewhere of how to create one? I am also a single programmer, and want to at least know about this tool so I can pick it up when appropriate. Thanks! Dan Waters ProMation Systems