Consulting

Results 1 to 6 of 6

Thread: Class property vs public variable

  1. #1

    Class property vs public variable

    Hi

    My question is conceptual, and is about the difference between a property and a public variable of a class.

    Let's say I'm creating a new object class on a Class Module.
    I can use
    [vba]Dim MP As String
    Property Let MyProperty(Value As String)
    MP = Value
    End Property
    [/vba] or
    [vba]
    Public MyProperty As String
    [/vba] In both cases, MyProperty is available from outside of the class.
    What are then the advantages/disadvantages of them? In what cases is either one of them advisable over the other?

    Thanks

    Jimmy
    -------------------------------------------------
    The more details you give, the easier it is to understand your question. Don't save the effort, tell us twice rather than not at all. The amount of info you give strongly influences the quality of answer, and also how fast you get it.

  2. #2
    Distinguished Lord of VBAX VBAX Grand Master Bob Phillips's Avatar
    Joined
    Apr 2005
    Posts
    25,453
    Location
    In the first, the property is write only, in the latter it is read/write property. If you just used Get, it would be read only.

    I alwasy use the former style myself, and it has the advantage that you can process the passed value. So for instance, you could get a employee grade, and from that you can calculate the slary in the Get procedure.
    ____________________________________________
    Nihil simul inventum est et perfectum

    Abusus non tollit usum

    Last night I dreamed of a small consolation enjoyed only by the blind: Nobody knows the trouble I've not seen!
    James Thurber

  3. #3
    Thanks Bob. (Again )

    Now, sticking to your example, where do you put the calculation of salary?
    • Into the Property Let EmpGrade part?
    • Or into the Property Get Salary part?
    I guess they're all the same, but maybe one of them is better for some reason.

    Another thing is, when you want to validate the data that is passed on to the instance of the class.

    E.g. EmpGrade should be 1, 2, 3, 4 or 5, any other values should be rejected. Where do you put the validation code?
    • Incorporate it into the object class, e.g. into Property Let EmpGrade part?
    • Or leave it outside the class, so that it's the main program's job to make sure that only valid data is passed on to the object?
    Is there a "Best Practice" for these things?

    Jimmy
    -------------------------------------------------
    The more details you give, the easier it is to understand your question. Don't save the effort, tell us twice rather than not at all. The amount of info you give strongly influences the quality of answer, and also how fast you get it.

  4. #4
    Distinguished Lord of VBAX VBAX Grand Master Bob Phillips's Avatar
    Joined
    Apr 2005
    Posts
    25,453
    Location
    Deep philosophical questions Jimmy <G>

    My answer is a tad vague, but really you have to look at the business process, and the rules surrounding the object that the class is emulating. So in our example, let us say that the company employs people so we are going to create an employee class (with an employee collection class of course, but that is the next level). Now every employee has a name of course, so this is a simple read/write proeprty set for given and surnames. The company will have a system of iding the individual that will be unique (names are not), so this will be generated from within the class when a new employee is added (this would normally require vsiisting some external data source to get the next id, or some genarting algorithm).

    As to the salary, you have to ask what is the criteria for determing the salary. If it were so simplistic as say a combination of Grade and Years Served, I would add coe to each of the Let property preocedures to calculate the Salary, as long as the other one is set. I would not do it in the Get procedure (that was a typo in my previous post), because it is likely the class needs that info, calculate pay rise for instance, so you would save it in a private class variable, calculate once, not recalculate every time.

    As to whether to validate within the class or outwith the class, I tend to keep the classes pure. In other words, if you use the class, you make sure that you know the API, and that you conform to it. If you don't, be it on your head (a bit like Windows API calls <G>). This way I can keep the class simple, concentrating on what it is about, the custom object it represents.
    Last edited by Bob Phillips; 03-20-2008 at 04:03 AM.
    ____________________________________________
    Nihil simul inventum est et perfectum

    Abusus non tollit usum

    Last night I dreamed of a small consolation enjoyed only by the blind: Nobody knows the trouble I've not seen!
    James Thurber

  5. #5
    MS Excel MVP VBAX Tutor
    Joined
    Mar 2005
    Posts
    246
    Location
    To generalize xld's responses, in a public variable, you are passing a value. In a property procedure, you are passing a value and you have the opportunity to run additional code within the procedure. You can process the value, validate the value, calculate other values, change the layout of a user form, or whatever else you may want to do.
    - Jon
    -------
    Jon Peltier, Microsoft Excel MVP
    Peltier Technical Services
    Tutorials and Custom Solutions
    http://PeltierTech.com
    _______

  6. #6
    Deep philosophical questions Jimmy
    Bob,
    Yes, well, as I'm getting comfortable with classes, I'm also developing such questions. I still have to digest your answers, but it is sure as sunlight that you gave me some interesting points to consider. My opinion might change, but right now I think your last paragraph will stay with me forever


    Jon,
    I thank you as well for clearing up a bit of my confusion.



    Jimmy

    PS
    My immediate questions are answered, but I think I'll leave this thread open for future ones. I plan to have some
    -------------------------------------------------
    The more details you give, the easier it is to understand your question. Don't save the effort, tell us twice rather than not at all. The amount of info you give strongly influences the quality of answer, and also how fast you get it.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •