Results 1 to 6 of 6

Thread: Classes: Public Var vs. Property

  1. #1

    Thread Starter
    Hyperactive Member
    Join Date
    Aug 1999
    Location
    Ont, Canada, Earth
    Posts
    458
    Hi,
    After reading about classes, this isn't clear to me. When should I use private variables 'm_something' (for property Get & Let) and when should I use Public (without Get & Let)?
    I know abuout Let allowing me data validation. So for things like Name of object (or any String variable) could declare variable Public and forget about Let & Get or is Let & Get better in any way?

    As always Thank you.
    Thanks

    Tomexx.

  2. #2
    Fanatic Member
    Join Date
    Oct 2000
    Location
    London
    Posts
    1,008
    If you are using VB after version 4, there is no two ways about it. Use Let and Get. If you use a public variable in a class, VB implements hidden Let and Get procedures automatically! This can cause problems using your class because the variable cannot be passed by reference because you pass the result of the hidden Get procedure and not the memory address of the variable itself.

    Use Let and Get. Every Time.

    Cheers,

    P.
    Not nearly so tired now...

    Haven't been around much so be gentle...

  3. #3
    Addicted Member Shrog's Avatar
    Join Date
    Aug 1999
    Location
    Darkest Africa
    Posts
    186
    You are absolutely right - you could just use a public variable.

    I prefer to use the property procedures though, because if I need to change anything later on, like make a property read-only, or take any action when a property changes, then I can do so without changing the way the property was implemented.

    Also, when using property procedures, you can type in a description for the properties, which shows up in the object browser. This is very handy if you are sharing your coding with others, since it makes it easier for them to use my classes.

    And lastly, I like to stick to a standard. When creating ActiveX controls, I have to use property procedures, otherwise I cannot call the PropertyChanged statement when a property changes. And because I do it in AxtiveX controls, I do it everywhere.

    But that is just my preference. You can use public variables as properties.

    Shrog

  4. #4
    Hyperactive Member
    Join Date
    Jun 2000
    Location
    NY
    Posts
    497

    some input to your quesiton

    Prop Get/Let gives *structure* to your code. It allows you to define and make clear just by looking at the declare statement what your internal variable is supposed to be. It also provides a way to keep members of your class strictly *read-only*, if you eliminate the Prop Let routine, or write-only if you eliminate the Prop Get.

    I'll bet money that a public variable, especially with the public sub or function needed for computation, takes up a lot more resources than Prop Get/Let.
    end war
    stop greed

  5. #5
    Addicted Member Shrog's Avatar
    Join Date
    Aug 1999
    Location
    Darkest Africa
    Posts
    186

    Wink Ah yes, that too...

    I don't know about it using more resources, but what Paulw says is absolutely true, and the problems that arise are not worth the lazyness of just using public properties.

    I'd actually forgotten about that problem. The reason I've forgotten about it is because I never use public variables as properties, and that's a good enough reason for me.

    Shrog

  6. #6

    Thread Starter
    Hyperactive Member
    Join Date
    Aug 1999
    Location
    Ont, Canada, Earth
    Posts
    458
    Now it's clear!
    Thanks.
    Thanks

    Tomexx.

Posting Permissions

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



Click Here to Expand Forum to Full Width