none
What in the world are classes? RRS feed

  • Question

  • I'm a new programmer, and I'm on a roll writing VBA applications for Access, Excel, Word, PowerPoint, and making them all play ball seamlessly with each other. But I can't wrap my mind around one concept: what the heck are classes? I can't grasp what they do, what merit they have, or just what their purpose is altogether.

    Whoever can help me understand classes gets a cookie.

    Tuesday, October 25, 2011 7:40 PM

Answers

  • No, that is an Excel Object, that's why I should actually not use the word 'object'.

     

    An object is a combination of code and data that can be treated as a unit. An object can be a piece of an application, like a control or a form. An entire application can also be an object.

    Each object in Visual Basic is defined by a class. A class describes the variables, properties, procedures, and events of an object. Objects are instances of classes; you can create as many objects you need once you have defined a class.

    To understand the relationship between an object and its class, think of cookie cutters and cookies. The cookie cutter is the class. It defines the characteristics of each cookie, for example size and shape. The class is used to create objects. The objects are the cookies.

    You must create an object before you can access its members.

     

    Another explenation (from the book Access 2010 Programmer Reference):

    In object-oriented programming (OOP) an object is a unique instance of a data structure, called a class, that has both properties (which defin its characteristics) and procedures called methods (which define its behavior).

    Templates are a good way to think about the distinction between a class module and a class instance. The class module is equivelant to a Word template and an instance of the class would be equivelant to a Word document that is based on that template.

     

    Does that make sense?

     


    Daniel van den Berg | Washington, USA | "Anticipate the difficult by managing the easy"
    Wednesday, October 26, 2011 1:54 AM
    Moderator

All replies

  • In very simple explenation, its a custom created Object, and you can assign properties to this custom Object.

    With that you can use this Custom Object in your Application, and can be very handy!

     

    See for more info, below links:

    http://www.cpearson.com/excel/classes.aspx

    http://office.microsoft.com/en-us/access-help/HV080750149.aspx

     

    Hope this helps, 


    Daniel van den Berg | Washington, USA | "Anticipate the difficult by managing the easy"
    Tuesday, October 25, 2011 7:56 PM
    Moderator
  • Okay, so if this is the case... I may have created classes already.

     

    Like if I am in Access, and I want to  create a new Excel application by declaring it as a new object, that would be creating a class?

     

     

    Wednesday, October 26, 2011 12:47 AM
  • No, that is an Excel Object, that's why I should actually not use the word 'object'.

     

    An object is a combination of code and data that can be treated as a unit. An object can be a piece of an application, like a control or a form. An entire application can also be an object.

    Each object in Visual Basic is defined by a class. A class describes the variables, properties, procedures, and events of an object. Objects are instances of classes; you can create as many objects you need once you have defined a class.

    To understand the relationship between an object and its class, think of cookie cutters and cookies. The cookie cutter is the class. It defines the characteristics of each cookie, for example size and shape. The class is used to create objects. The objects are the cookies.

    You must create an object before you can access its members.

     

    Another explenation (from the book Access 2010 Programmer Reference):

    In object-oriented programming (OOP) an object is a unique instance of a data structure, called a class, that has both properties (which defin its characteristics) and procedures called methods (which define its behavior).

    Templates are a good way to think about the distinction between a class module and a class instance. The class module is equivelant to a Word template and an instance of the class would be equivelant to a Word document that is based on that template.

     

    Does that make sense?

     


    Daniel van den Berg | Washington, USA | "Anticipate the difficult by managing the easy"
    Wednesday, October 26, 2011 1:54 AM
    Moderator
  • Hi, give Daniel a cookie!!

    Was going to add a bit more but there really isn't any point.


    "The programmer, like the poet, works only slightly removed from pure thought-stuff. He builds his castles in the air, from air, creating by exertion of the imagination." - Fred Brooks
    Wednesday, October 26, 2011 8:10 AM
  • Daniel,

    I'll use your explanation to answer people that ask me the same.

    The cookie cutter and cookies is simple therefore excellent!

    João

     

    Wednesday, October 26, 2011 10:11 AM
  • Hi, give Daniel a cookie!!

    Was going to add a bit more but there really isn't any point.


    "The programmer, like the poet, works only slightly removed from pure thought-stuff. He builds his castles in the air, from air, creating by exertion of the imagination." - Fred Brooks


    Thanks Derek!

    Btw I love cookies hehe ;)

     

    Cheers,

    Daniel


    Daniel van den Berg | Washington, USA | "Anticipate the difficult by managing the easy"
    Wednesday, October 26, 2011 4:35 PM
    Moderator
  • Daniel,

    I'll use your explanation to answer people that ask me the same.

    The cookie cutter and cookies is simple therefore excellent!

    João

     


    Thanks João! :)
    Daniel van den Berg | Washington, USA | "Anticipate the difficult by managing the easy"
    Wednesday, October 26, 2011 4:35 PM
    Moderator
  • Maybe an example would be good.  I found classes helpful for a Word addin that is used to populate numerous tables.  In the first pass I just duplicated the code to talk to the database replacing the parts specific to the table/fields/buttons.  This created a huge mess of code that was difficult to maintain.  In the next version I used a class.  I created a generic routine to talk to the database and could be easily configured  for the specific table when it was instanced.   This reduced my code by 10x and made it much easier to maintain.  I don't use classes a lot but it certain cases it can be a lifesaver.
    Thursday, October 27, 2011 12:51 PM
  • Question must be if classes saved you writing 10x as much code and make thing easior to maintain then why not use them all the time?

    :) No need to answer, just poking fun.

    There is a little more I can add. Object orientation introduces something to software that is not really considered all that necessary with VBA. It introduces the idea that software needs to be designed. The above demonstrates that rather well. The mess of code to something that was more maintainable was down to introducing a design to the internals of the software.

    What is object orientated design? well for me it's about data management, which is what all software is anyway. It's asking questions like what data do I have, what data is related to each other, how can the data can be 'laid out', what things need to be done to that data and what other data is needed to do it. That's what objects are.

    Now I'll make an example comparison but thing is this comparison is wrong but it's ok to use it, think of a database and it's data... that's like an object, now tables are related together and combined represent more than they do seperately, that's an objects dependencies, and then think of queries... now these are things that are done against and using the data... well these are the methods of an object.

     


    "The programmer, like the poet, works only slightly removed from pure thought-stuff. He builds his castles in the air, from air, creating by exertion of the imagination." - Fred Brooks
    Friday, October 28, 2011 9:05 AM
  • Daniel gets a cookie. Thanks Daniel! That helps a lot.

    I started playing with classes in the past two days after work every night. I am starting to see the merit in them. I am starting to see the potential they can hold and I am going to definitely use them the next time I create an Access application.

    Friday, October 28, 2011 1:04 PM
  • Hi,

    I am just about ready to publish a fairly sophisticated class on my website that can be used to deduce "real" sentences in Word (by real I mean filter out a lot of the false sentences that the built-in collection returns due to the use of perionds in abbreviations).

    If you want I send you a copy to look at.  Since you are new to class perhaps you could provide some input on my comments in the code.

    Use the "Contact Me" link on my site if you want me to send it to you.  http://gregmaxey.mvps.org/word_tips.htm


    Greg Maxey Please visit my website at: http://gregmaxey.mvps.org/word_tips.htm
    Friday, October 28, 2011 2:29 PM
  • Sure! I'll send you a message on the site.

    Friday, October 28, 2011 8:55 PM