class or no class

Posted on 2005-04-08
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
    LVL 85

    Accepted Solution

    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. 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

    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

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

     What do you mean not true OOP language?

    LVL 85

    Expert Comment

    by:Mike Tomlinson

    Author Comment

    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.


    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    How your wiki can always stay up-to-date

    Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
    - Increase transparency
    - Onboard new hires faster
    - Access from mobile/offline

    Hello everybody This Article will show you how to validate number with TEdit control, What's the TEdit control? TEdit is a standard Windows edit control on a form, it allows to user to write, read and copy/paste single line of text. Usua…
    In my programming career I have only very rarely run into situations where operator overloading would be of any use in my work.  Normally those situations involved math with either overly large numbers (hundreds of thousands of digits or accuracy re…
    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…
    Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…

    759 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

    Need Help in Real-Time?

    Connect with top rated Experts

    13 Experts available now in Live!

    Get 1:1 Help Now