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
    1. Advertisements

  2. Guest Guest

    Have you looked in VBA Help for definitions of the two? It would certainly
    help.
    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.
    --
    Dave Hargis, Microsoft Access MVP


    "" wrote:

    > what the difference between dim and const?
    >
    >
     
    Guest, Jul 9, 2007
    #2
    1. Advertisements

  3. On Mon, 09 Jul 2007 10:05:30 -0700, wrote:

    >what the difference between dim and const?


    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
    #3
  4. John W. Vinson <jvinson@STOP_SPAM.WysardOfInfo.com> wrote in
    news::

    > On Mon, 09 Jul 2007 10:05:30 -0700, wrote:
    >
    >>what the difference between dim and const?

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


    And at compile time, the constant value is hardcoded into all the
    places where it's been used.

    --
    David W. Fenton http://www.dfenton.com/
    usenet at dfenton dot com http://www.dfenton.com/DFA/
     
    David W. Fenton, Jul 9, 2007
    #4
  5. DAVID Guest

    David W. Fenton wrote:
    > John W. Vinson <jvinson@STOP_SPAM.WysardOfInfo.com> wrote in
    > news::
    >
    >> On Mon, 09 Jul 2007 10:05:30 -0700, wrote:
    >>
    >>> what the difference between dim and const?

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

    >
    > And at compile time, the constant value is hardcoded into all the
    > places where it's been used.
    >


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

    (david)
     
    DAVID, Jul 10, 2007
    #5
  6. DAVID <> wrote in
    news::

    > David W. Fenton wrote:
    >> John W. Vinson <jvinson@STOP_SPAM.WysardOfInfo.com> wrote in
    >> news::
    >>
    >>> On Mon, 09 Jul 2007 10:05:30 -0700, wrote:
    >>>
    >>>> what the difference between dim and const?
    >>> Dim reserves space for a variable which you can change; const
    >>> defines a constant which you can use but NOT change.

    >>
    >> And at compile time, the constant value is hardcoded into all the
    >> places where it's been used.

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


    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 http://www.dfenton.com/
    usenet at dfenton dot com http://www.dfenton.com/DFA/
     
    David W. Fenton, Jul 10, 2007
    #6
  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:

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


    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)


    David W. Fenton wrote:
    > DAVID <> wrote in
    > news::
    >
    >> David W. Fenton wrote:
    >>> John W. Vinson <jvinson@STOP_SPAM.WysardOfInfo.com> wrote in
    >>> news::
    >>>
    >>>> On Mon, 09 Jul 2007 10:05:30 -0700, wrote:
    >>>>
    >>>>> what the difference between dim and const?
    >>>> Dim reserves space for a variable which you can change; const
    >>>> defines a constant which you can use but NOT change.
    >>> And at compile time, the constant value is hardcoded into all the
    >>> places where it's been used.

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

    >
    > 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, Jul 11, 2007
    #7
  8. DAVID <> wrote in
    news::

    > 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.


    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
    called.

    [irrelevant discussion of compiling in a completely different
    languaged deleted]

    > Which brings me back to the original question:
    >
    > >> Are you sure? I thought I'd seen some decompiled
    > >> VBA indicating that is not the case?

    >
    > 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?


    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 http://www.dfenton.com/
    usenet at dfenton dot com http://www.dfenton.com/DFA/
     
    David W. Fenton, Jul 11, 2007
    #8
  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)



    David W. Fenton wrote:
    > DAVID <> wrote in
    > news::
    >
    >> 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.

    >
    > 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
    > called.
    >
    > [irrelevant discussion of compiling in a completely different
    > languaged deleted]
    >
    >> Which brings me back to the original question:
    >>
    >>>> Are you sure? I thought I'd seen some decompiled
    >>>> VBA indicating that is not the case?

    >> 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?

    >
    > 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, Jul 12, 2007
    #9
    1. Advertisements

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.
Similar Threads
  1. Albert D. Kallal

    Re: Difference between trim and trim$

    Albert D. Kallal, Jun 29, 2003, in forum: Microsoft Access VBA Modules
    Replies:
    0
    Views:
    4,505
    Albert D. Kallal
    Jun 29, 2003
  2. Chad

    Dim db as database and other built in functions

    Chad, Sep 20, 2003, in forum: Microsoft Access VBA Modules
    Replies:
    2
    Views:
    378
  3. Nick Ward

    Public Const Question

    Nick Ward, Nov 6, 2003, in forum: Microsoft Access VBA Modules
    Replies:
    6
    Views:
    1,587
    Emilia Maxim
    Nov 6, 2003
  4. Bob Alston

    Dim as ... statement causes VBA editor and Access to crash

    Bob Alston, Jan 8, 2004, in forum: Microsoft Access VBA Modules
    Replies:
    1
    Views:
    704
    gandalf
    Jan 8, 2004
  5. Guest

    DIM and Set db once per module

    Guest, Nov 9, 2005, in forum: Microsoft Access VBA Modules
    Replies:
    22
    Views:
    536
    Tim Ferguson
    Dec 28, 2005
  6. Jesper F

    public const is an integer - could it be a long?

    Jesper F, Jun 4, 2008, in forum: Microsoft Access VBA Modules
    Replies:
    2
    Views:
    509
    Jesper F
    Jun 4, 2008
  7. David

    Reg string public const

    David, Jan 14, 2009, in forum: Microsoft Access VBA Modules
    Replies:
    4
    Views:
    359
    Dymondjack
    Jan 14, 2009
  8. Robert

    Declaring Public Const

    Robert, Nov 25, 2009, in forum: Microsoft Access VBA Modules
    Replies:
    11
    Views:
    931
    David W. Fenton
    Nov 27, 2009
Loading...