Results 1 to 5 of 5
  1. #1
    Platinum Lounger
    Join Date
    Nov 2001
    Location
    Melbourne, Victoria, Australia
    Posts
    5,016
    Thanks
    0
    Thanked 0 Times in 0 Posts

    const qualifier for procedure parameter (2000)

    I'm wondering if VB/VBA has the equivalent to the "const" qualifier (as used in C++) to enforce immutability of parameters passed to a procedure. The arglist of a procedure declaration in VB looks like:

    [Optional] [ByVal | ByRef] [ParamArray] varname[( )] [As type] [= defaultvalue]

    What I'd like to do is to emulate the C++ capability of declaring one or more of varname[( )] to be treated as unchangable. In C++ I'd simply include the keyword "const" at the appropriate place(s) in the argument list. In VB, the const keyword doesn't work in the declaration, nor in the body of the procedure definition when referring to a passed parameter. Any other possibilities?

    Alan

  2. #2
    Super Moderator jscher2000's Avatar
    Join Date
    Feb 2001
    Location
    Silicon Valley, USA
    Posts
    23,112
    Thanks
    5
    Thanked 93 Times in 89 Posts

    Re: const qualifier for procedure parameter (2000)

    Trying to protect yourself from... yourself? <img src=/S/wink.gif border=0 alt=wink width=15 height=15>

    The only way I can think of to pass a "read-only" value would be to pass a pointer of some kind, either to a Property of a Class, or maybe a record in a RecordSet. Interesting problem. Maybe in .Net.

  3. #3
    Plutonium Lounger
    Join Date
    Mar 2002
    Posts
    84,353
    Thanks
    0
    Thanked 29 Times in 29 Posts

    Re: const qualifier for procedure parameter (2000)

    As far as I know, VB/VBA doesn't have Const arguments, only ByRef and ByVal arguments. Both can be changed within a procedure, the only difference is whether changes will be visible outside the procedure. A Const argument as in C++ and Pascal/Delphi can't be changed within the procedure itself; as I said, VB/VBA doesn't provide this.

  4. #4
    Uranium Lounger
    Join Date
    Jan 2001
    Location
    South Carolina, USA
    Posts
    7,295
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: const qualifier for procedure parameter (2000)

    I think that declaring the parameter ByVal is as close as you will get in VBA. The procedure can still change the parameter, but it will only be changed on the call stack, and will not be reflected back to the calling procedure.
    Legare Coleman

  5. #5
    Platinum Lounger
    Join Date
    Nov 2001
    Location
    Melbourne, Victoria, Australia
    Posts
    5,016
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: const qualifier for procedure parameter (2000)

    <hr>Trying to protect yourself from... yourself? <hr>
    Well, in a nutshell, absolutely! <img src=/S/grin.gif border=0 alt=grin width=15 height=15>
    VBA and the object models are relatively new to me, and I'm finding myself dropping into careless mode as I get progressively more flustered with some of the stuff I'm trying to write. I began to clean my act up using scoped constants (among other things) and that's when the const qualifier in procedure declaration struck me.

    But thanks for the reply, and to Hans and Legare too. Nice to find out what can't be done, as well as what can be. <img src=/S/grin.gif border=0 alt=grin width=15 height=15>

    Alan

Posting Permissions

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