difference between dim and const?

G

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

John W. Vinson

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]
 
D

David W. Fenton

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

DAVID

David said:
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)
 
D

David W. Fenton

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

DAVID

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)
 
D

David W. Fenton

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:


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

DAVID

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

But thanks for the information.

(david)


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:

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.
 

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. After that, you can post your question and our members will help you out.

Ask a Question

Top