class or no class

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,
Serge
AutofreakAsked:
Who is Participating?
 
Mike TomlinsonMiddle School Assistant TeacherCommented:
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.

~IM
0
 
Erick37Commented:
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.

e.g.

If gIsBusy Then ... 'not so good
vs.
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.
0
 
AutofreakAuthor Commented:
> The fact that VB6 is not a true OOP language only complicates matters.


 What do you mean not true OOP language?

Serge
0
 
Mike TomlinsonMiddle School Assistant TeacherCommented:
0
 
AutofreakAuthor Commented:
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.

Serge
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.