Custom Classes

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Keith Wilby

    #1

    Custom Classes

    I've been trying to get my head around custom classes by following the
    example in the Visual Basic Language Developer's Handbook by Sybex. I think
    I have a handle on what they're about, albeit a fragile one at the moment.
    Having said that, I can't think of a single application for one so obviously
    I need educating further. Does anyone have any practical examples of custom
    classes and their application that might help me broaden my horizons?

    Many thanks.

    Keith.


  • jlepack

    #2
    Re: Custom Classes

    Anything can be a custom class. Think of anything that has multiple
    variables and you can make it into a custom class.

    Take an Employee for example. I am an employee, you presumably are an
    employee.

    At the root, all employees have a first name, last name, birthdate,
    SSN, etc. You could store each of those variables and have a
    JasonLepack as Employee, or a KeithWilby as Employee.

    Another example is a slot machine. Each slot machine has a few
    variables.
    It has a cost structure, payout structure, amount of money in it, data
    on the reels, current position of reels.

    On top of that a slot machine has actions (functions) that can be
    performed. You could depositMoney, pullArm, fullPlay (the button on
    the front), payOut.

    Hope that makes a little more sense.

    Cheers,
    Jason Lepack

    Keith Wilby wrote:
    I've been trying to get my head around custom classes by following the
    example in the Visual Basic Language Developer's Handbook by Sybex. I think
    I have a handle on what they're about, albeit a fragile one at the moment.
    Having said that, I can't think of a single application for one so obviously
    I need educating further. Does anyone have any practical examples of custom
    classes and their application that might help me broaden my horizons?
    >
    Many thanks.
    >
    Keith.

    Comment

    • Rick Brandt

      #3
      Re: Custom Classes

      Keith Wilby wrote:
      I've been trying to get my head around custom classes by following the
      example in the Visual Basic Language Developer's Handbook by Sybex. I
      think I have a handle on what they're about, albeit a fragile one
      at the moment. Having said that, I can't think of a single
      application for one so obviously I need educating further. Does
      anyone have any practical examples of custom classes and their
      application that might help me broaden my horizons?
      Many thanks.
      >
      Keith.
      I have classes that ...

      Provides the Windows File Browser dialog
      Creates and sends an Email using Outlook
      Creates and send an Email using CDO
      Displays a custom Message Dialog Form
      (others that are to esoteric to bother describing)

      There are times when having a bunch of related code in a class is better
      than just having functions and sub-routines in a module. Classes can expose
      certain things while not exposing others. You can instantiate multiple
      objects of a Class at the same time. Since they are instantiated as objects
      you can pass them around between other functions and sub-routines.

      I'm not an expert at using them in Access and Classes in VBA have some
      limitations, but they definitely have their place. Some developers uses
      classes more than others especially if they came from a programming
      background where object-oriented was stressed.

      It is good to learn about Classes and be able to dig into those you
      encounter and see what's going on, but don't feel guilty if you can't think
      of hundreds of ways to use them. In most cases plain old functions and
      sub-routines solve the problem in the best fashion.

      --
      Rick Brandt, Microsoft Access MVP
      Email (as appropriate) to...
      RBrandt at Hunter dot com



      Comment

      • Lyle Fairfield

        #4
        Re: Custom Classes


        Keith Wilby wrote:
        I've been trying to get my head around custom classes by following the
        example in the Visual Basic Language Developer's Handbook by Sybex. I think
        I have a handle on what they're about, albeit a fragile one at the moment.
        Having said that, I can't think of a single application for one so obviously
        I need educating further. Does anyone have any practical examples of custom
        classes and their application that might help me broaden my horizons?
        There aren't a lot of uses for classes. The major one is to make
        programers/developers feel that they have arrived.
        I use my own classes almost never but over the years I have defended
        them as useful for:
        1. standardizing procedures in an application developed by many
        persons; for example an append class;
        2. allowing for multiple instances of code to exist concurrently; for
        example code that persisted values while forms were closed and opened;
        3. code which used objects or properties whose creation and or
        initialization were time consuming; for example a numerals to words
        procedure where arrays and strings were used.

        You have used classes extensively if you have used forms that have
        modules, and I expect that you have. The form module is, for all
        extents and purposes, a class module. It has initalization and
        termination procedures (form_open and form_close), properties in the
        form of controls or property definitions, and can have multiple
        instances. IMO the form is a very superior Class. When invisible it
        acts just like a class. It can be made visible on demand (and invisible
        again on demand) to get input from the user, to show as progress bar,
        to show a message, etc and as a single default entity it does not have
        to be initialized; we can jst call Form_MYClass.Do Something()
        [DoSomething is a Public Procedure] and slam, bam it's done.

        Comment

        • Keith Wilby

          #5
          Re: Custom Classes

          "Rick Brandt" <rickbrandt2@ho tmail.comwrote in message
          news:swBbh.1407 $Py2.754@newssv r27.news.prodig y.net...
          >
          It is good to learn about Classes and be able to dig into those you
          encounter and see what's going on, but don't feel guilty if you can't
          think of hundreds of ways to use them. In most cases plain old functions
          and sub-routines solve the problem in the best fashion.
          >
          You've hit the nail right on the head there Rick because I do extensively
          use subs/functions to return values and perform various tasks, I suppose I'm
          going to have to keep reading up until the penny fully drops to see if any
          would be more efficient as a class. Thanks for your response.

          Keith.


          Comment

          • Keith Wilby

            #6
            Re: Custom Classes

            "jlepack" <jlepack@gmail. comwrote in message
            news:1164895226 .867182.261250@ 80g2000cwy.goog legroups.com...
            Anything can be a custom class. Think of anything that has multiple
            variables and you can make it into a custom class.
            >
            Take an Employee for example. I am an employee, you presumably are an
            employee.
            >
            At the root, all employees have a first name, last name, birthdate,
            SSN, etc. You could store each of those variables and have a
            JasonLepack as Employee, or a KeithWilby as Employee.
            >
            <snip>

            Thanks Jason, I'll have a go at creating one of those, should help that
            penny drop.

            Regards,
            Keith.


            Comment

            • Keith Wilby

              #7
              Re: Custom Classes

              "Lyle Fairfield" <lylefairfield@ aim.comwrote in message
              news:1164896452 .192808.311430@ j72g2000cwa.goo glegroups.com.. .
              <snip>

              Thanks Lyle.


              Comment

              • Rick Brandt

                #8
                Re: Custom Classes

                Keith Wilby wrote:
                "jlepack" <jlepack@gmail. comwrote in message
                news:1164895226 .867182.261250@ 80g2000cwy.goog legroups.com...
                >Anything can be a custom class. Think of anything that has multiple
                >variables and you can make it into a custom class.
                >>
                >Take an Employee for example. I am an employee, you presumably are
                >an employee.
                >>
                >At the root, all employees have a first name, last name, birthdate,
                >SSN, etc. You could store each of those variables and have a
                >JasonLepack as Employee, or a KeithWilby as Employee.
                >>
                <snip>
                >
                Thanks Jason, I'll have a go at creating one of those, should help
                that penny drop.
                >
                Regards,
                Keith.
                An easy example where *using* a class can be easier. Take my Email class.
                As a class it works something like this.

                Dim msg as New CDOMsg

                With msg
                .AddRecipient(" SomeAddress")
                .Subject = "foo"
                .TextBody = "bar"
                .Send
                End With

                Now, that class has lots of other properties and methods that I might use
                from time to time, but most often I only use the ones you see above. If I
                wrote that as a standard function I would have to write it out with a bunch
                of comma separated arguments and include all the commas for the arguments I
                don't use and all optional arguments would have to be specified at the end
                of the function signature. I could use the named argument syntax with a
                standard function and "almost" do it the same as with the class, but its
                just nicer to be able to use the class syntax.

                --
                Rick Brandt, Microsoft Access MVP
                Email (as appropriate) to...
                RBrandt at Hunter dot com



                Comment

                • Keith Wilby

                  #9
                  Re: Custom Classes

                  "jlepack" <jlepack@gmail. comwrote in message
                  news:1164895226 .867182.261250@ 80g2000cwy.goog legroups.com...
                  >
                  Take an Employee for example. I am an employee, you presumably are an
                  employee.
                  >
                  At the root, all employees have a first name, last name, birthdate,
                  SSN, etc. You could store each of those variables and have a
                  JasonLepack as Employee, or a KeithWilby as Employee.
                  >
                  Any chance of posting an example Jason?

                  Keith.


                  Comment

                  • Keith Wilby

                    #10
                    Re: Custom Classes

                    "Rick Brandt" <rickbrandt2@ho tmail.comwrote in message
                    news:JSCbh.1435 $Py2.203@newssv r27.news.prodig y.net...
                    >
                    An easy example where *using* a class can be easier. Take my Email class.
                    As a class it works something like this.
                    >
                    Dim msg as New CDOMsg
                    >
                    With msg
                    .AddRecipient(" SomeAddress")
                    .Subject = "foo"
                    .TextBody = "bar"
                    .Send
                    End With
                    >
                    <snip>

                    Many thanks Rick. I think I'm OK using the class, it's what's in the class
                    itself that my small brain is struggling with. Here's what I have in my
                    Employee class so far:

                    Private mstrLastName As String
                    Private mstrFirstName As String
                    Private mdteDOB As Date
                    Private mintEmployeeNo As Integer

                    Property Get LastName()
                    LastName = mstrLastName
                    End Property

                    Property Get FirstName()
                    FirstName = mstrFirstName
                    End Property

                    Now it's the properties that are stumping me here because when I create a
                    new instance of Employee I want to assign values:

                    Dim objEmployee As clsEmployee
                    Set objEmployee = New clsEmployee
                    objEmployee.Las tName = "Wilby"
                    objEmployee.Fir stName = "Keith"

                    but of course this doesn't work. I'm not sure what's happening in those
                    property declarations. The logical part of my brain has quit for Christmas
                    already I think.

                    Keith.


                    Comment

                    • Gord

                      #11
                      Re: Custom Classes


                      Keith Wilby wrote:
                      "Rick Brandt" <rickbrandt2@ho tmail.comwrote in message
                      news:JSCbh.1435 $Py2.203@newssv r27.news.prodig y.net...

                      An easy example where *using* a class can be easier. Take my Email class.
                      As a class it works something like this.

                      Dim msg as New CDOMsg

                      With msg
                      .AddRecipient(" SomeAddress")
                      .Subject = "foo"
                      .TextBody = "bar"
                      .Send
                      End With
                      <snip>
                      >
                      Many thanks Rick. I think I'm OK using the class, it's what's in the class
                      itself that my small brain is struggling with. Here's what I have in my
                      Employee class so far:
                      >
                      Private mstrLastName As String
                      Private mstrFirstName As String
                      Private mdteDOB As Date
                      Private mintEmployeeNo As Integer
                      >
                      Property Get LastName()
                      LastName = mstrLastName
                      End Property
                      >
                      Property Get FirstName()
                      FirstName = mstrFirstName
                      End Property
                      >
                      Now it's the properties that are stumping me here because when I create a
                      new instance of Employee I want to assign values:
                      >
                      Dim objEmployee As clsEmployee
                      Set objEmployee = New clsEmployee
                      objEmployee.Las tName = "Wilby"
                      objEmployee.Fir stName = "Keith"
                      >
                      but of course this doesn't work. I'm not sure what's happening in those
                      property declarations.
                      The Property Get statements allow you to retrieve the value of the
                      named property. A Property Let statement will enable you to give the
                      property a value. I have never been a "classy" programmer , but I
                      believe the magic you seek is

                      Property Let LastName(ByVal vNewValue As Variant)
                      mstrLastName = vNewValue
                      End Property

                      (I'm sure that someone will correct me if I am wrong.)

                      Comment

                      • Keith Wilby

                        #12
                        Re: Custom Classes

                        "Gord" <gdt@kingston.n etwrote in message
                        news:1164904204 .500707.221050@ j44g2000cwa.goo glegroups.com.. .
                        >
                        >
                        The Property Get statements allow you to retrieve the value of the
                        named property. A Property Let statement will enable you to give the
                        property a value. I have never been a "classy" programmer , but I
                        believe the magic you seek is
                        >
                        Property Let LastName(ByVal vNewValue As Variant)
                        mstrLastName = vNewValue
                        End Property
                        >
                        (I'm sure that someone will correct me if I am wrong.)
                        >
                        Clearly I have much to learn still. Many thanks for that pointer Gord, I
                        will now go back to my book!

                        Regards,
                        Keith.


                        Comment

                        • jlepack

                          #13
                          Re: Custom Classes

                          Keith,

                          Your problem here is that oyu have followed sound programming
                          principles. You have created your variables within your class as
                          Private. Therefore, only methods and functions within your class can
                          access them, same as a private variable anywhere else for that matter.


                          As Gord correctly noted, these Get and Let methods can be accessed
                          publicly and therefore will allow non-local functions and procedures
                          that have an instance of your class to modify/view those values.

                          PS. You took the "unintersti ng path" and chose the employee and not
                          the slot machine ;)

                          Cheers,
                          Jason Lepack

                          Keith Wilby wrote:
                          "Rick Brandt" <rickbrandt2@ho tmail.comwrote in message
                          news:JSCbh.1435 $Py2.203@newssv r27.news.prodig y.net...

                          An easy example where *using* a class can be easier. Take my Email class.
                          As a class it works something like this.

                          Dim msg as New CDOMsg

                          With msg
                          .AddRecipient(" SomeAddress")
                          .Subject = "foo"
                          .TextBody = "bar"
                          .Send
                          End With
                          <snip>
                          >
                          Many thanks Rick. I think I'm OK using the class, it's what's in the class
                          itself that my small brain is struggling with. Here's what I have in my
                          Employee class so far:
                          >
                          Private mstrLastName As String
                          Private mstrFirstName As String
                          Private mdteDOB As Date
                          Private mintEmployeeNo As Integer
                          >
                          Property Get LastName()
                          LastName = mstrLastName
                          End Property
                          >
                          Property Get FirstName()
                          FirstName = mstrFirstName
                          End Property
                          >
                          Now it's the properties that are stumping me here because when I create a
                          new instance of Employee I want to assign values:
                          >
                          Dim objEmployee As clsEmployee
                          Set objEmployee = New clsEmployee
                          objEmployee.Las tName = "Wilby"
                          objEmployee.Fir stName = "Keith"
                          >
                          but of course this doesn't work. I'm not sure what's happening in those
                          property declarations. The logical part of my brain has quit for Christmas
                          already I think.
                          >
                          Keith.

                          Comment

                          • Rick Brandt

                            #14
                            Re: Custom Classes

                            Keith Wilby wrote:
                            "Gord" <gdt@kingston.n etwrote in message
                            news:1164904204 .500707.221050@ j44g2000cwa.goo glegroups.com.. .
                            >>
                            >>
                            >The Property Get statements allow you to retrieve the value of the
                            >named property. A Property Let statement will enable you to give the
                            >property a value. I have never been a "classy" programmer , but I
                            >believe the magic you seek is
                            >>
                            >Property Let LastName(ByVal vNewValue As Variant)
                            >mstrLastName = vNewValue
                            >End Property
                            >>
                            >(I'm sure that someone will correct me if I am wrong.)
                            >>
                            >
                            Clearly I have much to learn still. Many thanks for that pointer
                            Gord, I will now go back to my book!
                            >
                            Regards,
                            Keith.
                            The problem is you made those properties Private. Private properties are
                            either intended to be used strictly within the class or must have a Let (or
                            Set) function that can be used by code outside the class.

                            --
                            Rick Brandt, Microsoft Access MVP
                            Email (as appropriate) to...
                            RBrandt at Hunter dot com



                            Comment

                            • CDMAPoster@FortuneJames.com

                              #15
                              Re: Custom Classes

                              Lyle Fairfield wrote:
                              Keith Wilby wrote:
                              I've been trying to get my head around custom classes by following the
                              example in the Visual Basic Language Developer's Handbook by Sybex. I think
                              I have a handle on what they're about, albeit a fragile one at the moment.
                              Having said that, I can't think of a single application for one so obviously
                              I need educating further. Does anyone have any practical examples of custom
                              classes and their application that might help me broaden my horizons?
                              >
                              There aren't a lot of uses for classes. The major one is to make
                              programers/developers feel that they have arrived.
                              I use my own classes almost never but over the years I have defended
                              them as useful for:
                              1. standardizing procedures in an application developed by many
                              persons; for example an append class;
                              2. allowing for multiple instances of code to exist concurrently; for
                              example code that persisted values while forms were closed and opened;
                              3. code which used objects or properties whose creation and or
                              initialization were time consuming; for example a numerals to words
                              procedure where arrays and strings were used.
                              >
                              You have used classes extensively if you have used forms that have
                              modules, and I expect that you have. The form module is, for all
                              extents [:-)] and purposes, a class module. It has initalization and
                              termination procedures (form_open and form_close), properties in the
                              form of controls or property definitions, and can have multiple
                              instances. IMO the form is a very superior Class. When invisible it
                              acts just like a class. It can be made visible on demand (and invisible
                              again on demand) to get input from the user, to show as progress bar,
                              to show a message, etc and as a single default entity it does not have
                              to be initialized; we can jst call Form_MYClass.Do Something()
                              [DoSomething is a Public Procedure] and slam, bam it's done.
                              Slam, bam, thank-you RAM? Is that from a call to
                              Form_ClassicRoc k.GetAlteredLyr icSnippet()?

                              James A. Fortune
                              CDMAPoster@Fort uneJames.com

                              Comment

                              Working...