class or no class

Posted on 2005-04-08
Medium Priority
Last Modified: 2013-11-23
Hello All,
                I'm at a point when my tasks  got so complex and interdependant that I am thinking of learning classes finally. My understanding is that classes act more or less like sub routines and functions where one calls them from some point in the program to perform some task, only do the calling differently. Now, the question is how classes are beneficial over using the traditional sub procedures and functions from software performance and architecture standpoint? Are there sagnificant coding efficiency and code reusabilty gains? How using classes made you a better designer?

Thank you,
Question by:Autofreak
  • 2
  • 2
LVL 86

Accepted Solution

Mike Tomlinson earned 360 total points
ID: 13739499
A class is not simply a place to hold subs/functions...if this were the case then you might as well use a module.

The benefit of using classes is that they act like cookie cutters on a rolled out a piece of dough.  Assuming you have a "dog" cookie cutter, you can stamp out as many "dogs" as you like.  Each is a standalone dog with all the qualities and properties you would expect from a dog cookie.  BUT...you can embellish each dog with different decorations and they won't affect each other.  So you can make on dog with sprinkles, one with frosting, one with nothing and another with sugar, etc...

Classes are most useful when you need to create an unkown number of instances of the same object and store them in some kind of collection object.  Say you were modeling a bank teller line.  You could create a Class called BankLine that would hold instances of a Class called BankCustomer.  So when a new customer enters the bank you would create an instance of BankCustomer and add it to your BankLine class.

Make sense?

Why would you want to uses classes over other methods?  To represent all the qualities of some object without a class, you would either need to use multiple arrays of variables or use arrays of a custom user defined Struct:

    Method 1:
        Private x() As Single
        Private y() As Single

    Method 2:
        Private Type Point
            X As Single
            Y As Single
        End Type

        Private P() As Point

The problem with both of these approaches is that it clutters your main code with all the support variables/subs/functions necessary to interact with the object.

By moving the representation of the data into a class, all of the data variables and support subs/functions become encapsulated into one easy to maintain unit.  This makes it easier to maintain both your GUI code and your object representation code.  Moving the representation of an object into a class is known as "Encapsulation" in the object oriented world.

In addition to making maintenance easier, by using classes you can create complex data representations by combining your classes.  So you have a Class called Day that represents a day of the week and a Class called Week that represents a week of the month.  You can make your class Week contain multiple instances of your Class Day.  This is known as "Composition" since your data representation is composed of other objects.

Hope that all made sense.  It is difficult to convey the benefit of OOP (object oriented programming) to the uninitiated.  The fact that VB6 is not a true OOP language only complicates matters.

I do babble on though...let's hear from some other experts.

LVL 32

Assisted Solution

Erick37 earned 140 total points
ID: 13739647
That's a big topic to cover.
I think the biggest advantage of using an object oriented approach is the use of encapsulation.  Classes allow you to encapsulate your code and data so that each module works independently and protects its data.

One big problem with a structured language approach is the use of too many Public or Global variables.  This can introduce bugs because any procedure could modify the data directly.  Encapsulation provides a way to protect data by only exposing data that is required, and forcing the programmer to access it through the class object.


If gIsBusy Then ... 'not so good
If MyDataClass.IsBusy Then ... 'more descriptive

>> How using classes made you a better designer?

By forcing you to think more about the design of the program and to group related functions with the related data.

Author Comment

ID: 13754924
> The fact that VB6 is not a true OOP language only complicates matters.

 What do you mean not true OOP language?

LVL 86

Expert Comment

by:Mike Tomlinson
ID: 13755094

Author Comment

ID: 13775139
Thanks a lot, excellent explanation Idle_Mind, I am going to look theu the links you provided. Can you Guys suggest a VB book on classes that can start me going, not too nurdish though, kind of step by step hands on arrangement.


Featured Post

Vote for the Most Valuable Expert

It’s time to recognize experts that go above and beyond with helpful solutions and engagement on site. Choose from the top experts in the Hall of Fame or on the right rail of your favorite topic page. Look for the blue “Nominate” button on their profile to vote.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction While answering a recent question (http://www.experts-exchange.com/Q_27402310.html) in the VB classic zone, I wrote some VB code in the (Office) VBA environment, rather than fire up my older PC.  I didn't post completely correct code o…
When trying to find the cause of a problem in VBA or VB6 it's often valuable to know what procedures were executed prior to the error. You can use the Call Stack for that but it is often inadequate because it may show procedures you aren't intereste…
Get people started with the process of using Access VBA to control Excel using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Excel. Using automation, an Access application can laun…
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…
Suggested Courses
Course of the Month16 days, 9 hours left to enroll

864 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question