difference between dim and const?

Discussion in 'Microsoft Access VBA Modules' started by zionsaal@gmail.com, Jul 9, 2007.

  1. Guest

    what the difference between dim and const?
    , Jul 9, 2007
    1. Advertisements

  2. Guest Guest

    Have you looked in VBA Help for definitions of the two? It would certainly
    A Dim allocates memory for a variable. It identifies a specific data type.
    You may then use the variable within the scope of the declaration to store
    and retrieve values. A Const defines a constant. You also define the data
    type and you define the constant's value. You cannot change the value of a
    constant. The Scope rules apply to the contstant just as they do a variable.
    Guest, Jul 9, 2007
    1. Advertisements

  3. Dim reserves space for a variable which you can change; const defines a
    constant which you can use but NOT change.

    John W. Vinson [MVP]
    John W. Vinson, Jul 9, 2007
  4. And at compile time, the constant value is hardcoded into all the
    places where it's been used.
    David W. Fenton, Jul 9, 2007
  5. DAVID Guest

    Are you sure? I thought I'd seen some decompiled
    VBA indicating that is not the case?

    DAVID, Jul 10, 2007
  6. Er, what would be the indicator that this is not the case? What I
    said is that:

    Const c_strVariable = "Some Value"

    MsgBox c_strVariable

    would be replaced in the compiled code by:

    MsbBox "Some Value"

    I can't see how it could be done in any other way, to be honest --
    it wouldn't make any sense to maintain a set of pointers to a value
    that can't change at runtime.
    David W. Fenton, Jul 10, 2007
  7. DAVID Guest

    The way it could be done is the way it often has
    been done, the way it is done in C: constants
    implemented as initialised variables.

    Historically, constants were often implemented as
    initialised variables because either the hardware
    or software lacked the ability to use anything else,
    for example
    LOD 6, B
    LOD 5, A
    ADD A, B

    On an Intel x86, that would be coded as an
    add imediate
    ADD B, 5

    Notoriously on one early fortran compiler, constants
    were implemented as variables:
    a = func(5)
    allowing the redefinition of "5"

    We see this in the C language (and the MS C compiler
    forms the backend of the VBA compiler): const is a
    kind of variable declaration: hardcoding in of values
    is done only by the pre-processor, not by the compiler.

    What about a function calls? Your suggestion would
    imply separate copies of the function, one for each
    hardcopy parameter, rather than copying the parameters
    to variables.

    So there is no obvious reason to believe that
    a const value MUST be hardcoded into all the
    places where it is used.

    Which brings me back to the original question:

    Decoded VBA always shows implicit variables and the
    implicit close statements for objects, so I can't
    remember exactly what I've seen: decoded VBA always
    shows implicit variables, are some of those implicit
    variables the const values?

    DAVID, Jul 11, 2007
  8. But it *isn't* by VBA.

    Check it out yourself -- create an MDE and then browse the binary
    file and you'll see the constants hardwired into every time they are

    [irrelevant discussion of compiling in a completely different
    languaged deleted]
    What is "decoded VBA?" Are you talking decompiled?

    Well, I said *compiled* VBA, which means, well, the actually
    compiled p-code. In that p-code, you'll see all literal strings used
    anywhere in the code, whether declared as constants or not, and each
    use of a constant in the canonical code will be replaced with the
    literal value of the constant.

    You can check it out and see for yourself.

    But references to the compilation of completely different languages
    that have zilch to do with VBA's code compilation are not helpful.
    David W. Fenton, Jul 11, 2007
  9. DAVID Guest

    I can't see how it could be done in any other way, to be honest <

    But thanks for the information.


    DAVID, Jul 12, 2007
    1. Advertisements

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments (here). After that, you can post your question and our members will help you out.