DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

Results 1 to 9 of 9

Thread: Variable declaration.

  1. #1
    stephan Guest

    Variable declaration.


    What is the different between Private, Public, and Dim?

  2. #2
    Kathleen Dollard-Joeris Guest

    Re: Variable declaration.

    Stephan,

    Use Dim inside procedures, and Private for module level variables and
    generally avoid Public.

    The ability to use Dim at the module level is a bad holdover for legacy
    reasons. It is not consistent in whether it behaves as public or private,
    and should be avoided. Property procedures are preferable to Public
    variables.

    Private variables can be seen only by the current file. Public variables can
    be seen anywhere the file can be seen (within the same project and across
    projects).

    In the case of objects (and forms) if there are multiple copies of the
    object at runtime, each has its own set of public and private variables.

    --
    Kathleen
    (MS-MVP)
    Reply in the newsgroup so everyone can benefit
    --



  3. #3
    Patrick Marshall Guest

    Re: Variable declaration.


    Hi Kathleen,

    I agree entirely with your description except the
    following may be an over-generalization:

    >... Property procedures are preferable to Public
    >variables.


    I tend not to use property procedures if indeed you
    are only setting a single private variable and require both
    a get and let for that variable, IMO this tends to be
    an extraneous level of indirection. However, f you have an
    unbalanced get/let (only let or only get) then I think
    it makes sense in that you are protecting the private
    variable from either being read or written from outside
    the module.

    A programmer needs to be aware that public
    variables and public property procedures, even though
    they might appear to be interchangeable, have very
    different side effects. For example you can't easily
    get circular references with public variables but
    public procedures can (e.g. abc=def >> def = abc >> abc = def ...)

    I recommend against wholesale replacement
    of public variables with their equivalent property
    procedures, each should be considered on a
    case-by-case basis. Even though "global" variables
    are intrinsically evil <g> property procedures don't
    really address this issue and in fact may make
    debugging even more difficult.

    I'm being overcautious here, since I otherwise agree
    with you, but I think the distinction should be made.

    Regards,

    <Patrick>

  4. #4
    Kathleen Dollard-Joeris Guest

    Re: Variable declaration.

    Patrick,

    Sorry I didn't get back sooner. Mom visiting and major hardware hassles.

    > >... Property procedures are preferable to Public
    > >variables.

    >
    > I tend not to use property procedures if indeed you
    > are only setting a single private variable and require both
    > a get and let for that variable, IMO this tends to be
    > an extraneous level of indirection. .


    What extra indirection? Exactly the same thing happens once COM gets ahold
    of it.

    > A programmer needs to be aware that public
    > variables and public property procedures, even though
    > they might appear to be interchangeable, have very
    > different side effects. For example you can't easily
    > get circular references with public variables but
    > public procedures can (e.g. abc=def >> def = abc >> abc = def ...)


    I don't understand what you are saying here. Why is it easier to get
    circular references?

    > I recommend against wholesale replacement
    > of public variables with their equivalent property
    > procedures, each should be considered on a
    > case-by-case basis. Even though "global" variables
    > are intrinsically evil <g> property procedures don't
    > really address this issue and in fact may make
    > debugging even more difficult.


    In what way do they make debugging more difficult

    FWIW, I used to argue for the use of public variables in classes, instead of
    property procedures. I was beaten into submissions by those in the know <g>.

    --
    Kathleen
    (MS-MVP)
    Reply in the newsgroup so everyone can benefit
    --



  5. #5
    Patrick Marshall Guest

    Re: Variable declaration.


    Hi Kathleen,

    It's very easy to get circular calls with public properties.

    Try this:

    'simple inches/cm conversion, the temptation here
    'is to set inches from cm and cm from inches

    '********* BAD CODE, DO NOT DO THIS *******

    'Note these could have been declared as public and
    'the let/get properties could have been removed
    Private CentimeterDbl As Double
    Private InchDbl As Double

    Public Property Let xCentimeters(NewCentimeterDbl As Double)
    CentimeterDbl = NewCentimeterDbl
    'since you KNOW that centimeters = inches * 2.54 then
    xInches = CentimeterDbl / 2.54
    End Property

    Public Property Let xInches(NewInchDbl As Double)
    InchDbl = NewInchDbl
    'since you KNOW that centimeters = inches * 2.54
    'then

    xCentimeters = InchDbl * 2.54

    End Property

    Me.xInches = 5
    'the two lets bounce between each other until you get
    'error 28, out of stack space

    OK, this is a glaringly obvious case, and you wouldn't
    do this, or you would find it very quickly while debugging,
    but I have (just recently) run into instances where only
    certain datapoints would trigger a loop, so they were
    very, very hard to find. Public variables don't have this
    type of issue.

    As to indirection, IMHO, it is easier to trace a public
    variable from either the left or right of the equal
    sign versus having to jump to EITHER a public let
    (left of =) or public get (right of =) then making sure
    that additional code is not fired from either property.
    The issue is that you have an additional layer between
    the desired result of getting/setting the actual variable
    and the point where it is called, and that there is an
    "in" branch and and "out" branch which have to be
    traced individually. Often this is a DESIRABLE quality,
    but sometimes it's not. My feeling is that you have
    to determine whether or not you are getting a benefit
    from using public properties or you would be better
    off (in that instance) sticking with a public variable.
    Using public properties MAY be preferable. I'm not
    so sure that they are ALWAYS preferable.

    Hope your Mom is over her hardware problems <g>.

    Best Regards,

    <Patrick>
    ****************************************

    "Kathleen Dollard-Joeris" <kjoeris@noemailplease.com> wrote:
    >Patrick,
    >
    >Sorry I didn't get back sooner. Mom visiting and major hardware hassles.
    >
    >> >... Property procedures are preferable to Public
    >> >variables.

    >>
    >> I tend not to use property procedures if indeed you
    >> are only setting a single private variable and require both
    >> a get and let for that variable, IMO this tends to be
    >> an extraneous level of indirection. .

    >
    >What extra indirection? Exactly the same thing happens once COM gets ahold
    >of it.
    >
    >> A programmer needs to be aware that public
    >> variables and public property procedures, even though
    >> they might appear to be interchangeable, have very
    >> different side effects. For example you can't easily
    >> get circular references with public variables but
    >> public procedures can (e.g. abc=def >> def = abc >> abc = def ...)

    >
    >I don't understand what you are saying here. Why is it easier to get
    >circular references?
    >
    >> I recommend against wholesale replacement
    >> of public variables with their equivalent property
    >> procedures, each should be considered on a
    >> case-by-case basis. Even though "global" variables
    >> are intrinsically evil <g> property procedures don't
    >> really address this issue and in fact may make
    >> debugging even more difficult.

    >
    > In what way do they make debugging more difficult
    >
    >FWIW, I used to argue for the use of public variables in classes, instead

    of
    >property procedures. I was beaten into submissions by those in the know

    <g>.
    >
    >--
    >Kathleen
    >(MS-MVP)
    >Reply in the newsgroup so everyone can benefit
    >--
    >
    >



  6. #6
    Kathleen Dollard-Joeris Guest

    Re: Variable declaration.

    Patrick,

    I am sorry I didn't get back earlier. What a couple of weeks! As hardware
    problems got settled (I am still nervous about turning off my server) and we
    took Mom to the airport, a fire exploded behind me, inching (actually
    "miling") closer on Tuesday and Thursday (all is well now), then I was so
    behind in work! Life has just been a mess, but I am ready to get back to
    normal.

    > It's very easy to get circular calls with public properties.


    We had been using the term "circular reference" which is different than this
    type of back and forth loop. Circular references can be created whether you
    use property procedures or not, just as easily either way.

    The type of code you show can not (obviously) be created without property
    procedures. But if it is a simple assignment, this would not occur, and if
    code needs to be executed, it needs to be executed. Again, I don't see a
    drawback in property procedures.

    > As to indirection, IMHO, it is easier to trace a public
    > variable from either the left or right of the equal
    > sign versus having to jump to EITHER a public let
    > (left of =) or public get (right of =) then making sure
    > that additional code is not fired from either property.


    OK, this makes sense. I find it trivial. Since some of my properties need
    additional code, I prefer to do it all the same way, all in property
    procedures, but yes, it adds to the trace in the code window.

    Note that VB creates property procedures for you if you do not create them
    yourself. The amount of indirection your code hops through at run time is
    identical.

    Did I tell you that I use to be on the other side of this issue? A couple
    people may be giggling to hear the passion of the converted heathen. I did
    come to using all property procedures (except in trivial classes that
    contain only read/write properties) after a couple of years of not doing it
    this way.

    Again sorry to be so delayed in tying up this thread.

    --
    Kathleen
    (MS-MVP)
    Reply in the newsgroup so everyone can benefit
    --



  7. #7
    Patrick Marshall Guest

    Re: Variable declaration.


    Hi Kathleen,

    Wow, I thought I had problems last week, but I
    think you have me beat. Hope things are well
    again. We just went through a heat wave here, and
    it got so hot that my front door warped and popped
    open. I got home at 7:30 pm and (fortunately!)
    everything was still there. I guess people thought
    that the door was open because of the heat!

    As to circular references/calls, OK, you got me for
    inexact choice of wording.

    >...The type of code you show can not (obviously)
    >be created without property procedures. But if it is
    >a simple assignment, this would not occur...


    that was pretty much my whole point. I know it is
    rather trivial, but I've been sensitized to this lately
    from working on a large piece of code I've inherited
    liberally scattered with property procedures that
    don't look like property procedures. I think I'm
    making simple value assignments when I'm
    actually triggering "phantom" code. There are
    field level validation routines firing unexpectedly
    all over the place :-( worse, many have side
    effects which make life...interesting.

    You are right that COM creates the same level
    of indirection in the background, but I'm only
    concerned with code tracing not efficiency.

    Best Regards,

    <Patrick>
    "Kathleen Dollard-Joeris" <kjoeris@noemailplease.com> wrote:
    >Patrick,
    >
    >I am sorry I didn't get back earlier. What a couple of weeks! As hardware
    >problems got settled (I am still nervous about turning off my server) and

    we
    >took Mom to the airport, a fire exploded behind me, inching (actually
    >"miling") closer on Tuesday and Thursday (all is well now), then I was so
    >behind in work! Life has just been a mess, but I am ready to get back to
    >normal.
    >
    >> It's very easy to get circular calls with public properties.

    >
    >We had been using the term "circular reference" which is different than

    this
    >type of back and forth loop. Circular references can be created whether

    you
    >use property procedures or not, just as easily either way.
    >
    >The type of code you show can not (obviously) be created without property
    >procedures. But if it is a simple assignment, this would not occur, and

    if
    >code needs to be executed, it needs to be executed. Again, I don't see

    a
    >drawback in property procedures.
    >
    >> As to indirection, IMHO, it is easier to trace a public
    >> variable from either the left or right of the equal
    >> sign versus having to jump to EITHER a public let
    >> (left of =) or public get (right of =) then making sure
    >> that additional code is not fired from either property.

    >
    >OK, this makes sense. I find it trivial. Since some of my properties need
    >additional code, I prefer to do it all the same way, all in property
    >procedures, but yes, it adds to the trace in the code window.
    >
    >Note that VB creates property procedures for you if you do not create them
    >yourself. The amount of indirection your code hops through at run time is
    >identical.
    >
    >Did I tell you that I use to be on the other side of this issue? A couple
    >people may be giggling to hear the passion of the converted heathen. I did
    >come to using all property procedures (except in trivial classes that
    >contain only read/write properties) after a couple of years of not doing

    it
    >this way.
    >
    >Again sorry to be so delayed in tying up this thread.
    >
    >--
    >Kathleen
    >(MS-MVP)
    >Reply in the newsgroup so everyone can benefit
    >--
    >
    >



  8. #8
    Kathleen Dollard-Joeris Guest

    Re: Variable declaration.

    Patrick,

    I know I should let this old thread rest, but I think that there is a point
    here worth emphasizing.

    > that was pretty much my whole point. I know it is
    > rather trivial, but I've been sensitized to this lately
    > from working on a large piece of code I've inherited
    > liberally scattered with property procedures that
    > don't look like property procedures. I think I'm
    > making simple value assignments when I'm
    > actually triggering "phantom" code. There are
    > field level validation routines firing unexpectedly
    > all over the place :-( worse, many have side
    > effects which make life...interesting.


    Since this is a beginner's group:

    Procedures, all procedures, should accomplish exactly what they say in their
    title they will accomplish. Property procedures are no different. They
    should set a property, and not spawn unexpected code. This is especially
    confusing if the additional code is spawned through events. Event code can
    not be traced through the debugger with certainty that the same code is
    executed in the same order.

    I don't mean to say that property procedures should never result in event
    code being executed. Within user controls I can imagine cases where this
    would be reasonable (setting a size of a subcomponent spawning that
    subcomponents resize event, for example), but it should be local to the user
    control or spawn predictable events (a DataChanged event, for example).

    As we build components using COM, it is more and more important that each
    building block be very well behaved. It sounds like yours are poorly
    behaved.

    If the code was "phantom" it wouldn't be causing any problems <VBG>.

    --
    Kathleen
    (MS-MVP)
    Reply in the newsgroup so everyone can benefit
    --



  9. #9
    Patrick Marshall Guest

    Re: Variable declaration.


    Hi Kathleen,

    >As we build components using COM, it is more and more important that each
    >building block be very well behaved. It sounds like yours are poorly
    >behaved.


    Not MY code <emphasis> <bg>. You can't choose your relatives and
    you can't choose what you inherit.

    >
    >If the code was "phantom" it wouldn't be causing any problems <VBG>.


    Wishful thinking, I wish it _was_ "phantom".

    I'm still an unconverted heathen as far as property procedures go.
    Excuse me, I have to go make a necklace from chicken bones and
    small rodent skulls now.

    <Patrick>

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center
 
 
FAQ
Latest Articles
Java
.NET
XML
Database
Enterprise
Questions? Contact us.
C++
Web Development
Wireless
Latest Tips
Open Source


   Development Centers

   -- Android Development Center
   -- Cloud Development Project Center
   -- HTML5 Development Center
   -- Windows Mobile Development Center