Can anyone explain Private vs. Public properties in Class modules

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • TheSmileyCoder
    Recognized Expert Moderator Top Contributor
    • Dec 2009
    • 2322

    Can anyone explain Private vs. Public properties in Class modules

    I've started using class modules, and admittedly is quite inexperienced in this particular area, and there is one thing I can't seem to wrap my head around.

    Part of the code is the property StartHeight. Get and Let shown below:
    Code:
    Public Property Let StartHeight(intValue As Integer)
        pIntStartHeight = intValue
    End Property
    Public Property Get StartHeight() As Integer
        StartHeight = pIntStartHeight
    End Property
    At another point in my code I want to use:
    Code:
    Me.StartHeight=500
    Which works fine, but I would prefer to make the property a private property, but If I do that, I can no longer use the above syntax.

    In short my question:
    Can I make a property private, so that I can only manipulate it from within the class, but still retain the ability to use Me.
  • MikeTheBike
    Recognized Expert Contributor
    • Jun 2007
    • 640

    #2
    Hi

    As the Me key word refers to the Class then the only thing exposed are Public declared Variables, Subs, Functions and Properties.

    You are already refereing to the Private varable in your property definition ie
    Code:
    pIntStartHeight = intValue
    So to manipulate the Private varable (declared using Dim) you would just use
    Code:
    pIntStartHeight = 500
    in you classes code!

    As far as I can see the only reason for using Propert Let/Get definitions; that is to define Read or Write ONLY properties. All other properies (as opposed to Methods) can be delared ar Public!! But perhaps I am mistaken, I would be interested in other views/opinoins!

    Hope that makes sense!

    MTB

    Comment

    • TheSmileyCoder
      Recognized Expert Moderator Top Contributor
      • Dec 2009
      • 2322

      #3
      You are correct.

      I think I misunderstood the proper use of properties (no pun intended...).

      As I see it now, after your post, I realise that the idea behind making properties is to expose them to the outside world, and to possibly add extra code to the event of setting/getting them.

      In my simple example, I might as well simply dim my pIntStartHeight as public.

      Comment

      • hype261
        New Member
        • Apr 2010
        • 207

        #4
        The reason you have Public and Private is for data hiding. Variable that are Public can be seen and modified by other parts of the code outside the class. Variables that are Private cannot. Generally you do not want all your variable Public because it can become a debugging nightmare.

        I have one application that I inherited where every class is public and a lot of them are global to boot. I am afraid to touch anything in the application because it is very hard to figure out where everything is being used and the ramifications of changing something.

        Comment

        Working...