Const?

  • Thread starter Thread starter Joe User
  • Start date Start date
J

Joe User

If I declare "Const foo as Integer = 123", does that allocate storage?

Or does that create a compile-time constant that replaces "foo" wherever it
is used in executable statements?

If the latter, is "Const foo as Integer = 123*456" and "x = foo" more
efficient than "x = 123*456"? In particular, does the compiler treat "x =
foo" as "x = 56088"?
 
I believe that the compiler evaluates any constants and replaces them
with their values before any code execution takes place. This is why
you can assign to constants values that can be evaluated
arithmetically with actual constants (Const ABC = 1234 * 10) or other,
previously declared constants (Const ABC = DEF * 10, where DEF is a
previously declared constant).

As far as efficiency at run time, a simple constant makes no
difference at all and an arithmetic const is only marginally
(microseconds) better than having the arithmetic in the actual run
time code.

Cordially,
Chip Pearson
Microsoft MVP 1998 - 2010
Pearson Software Consulting, LLC
www.cpearson.com
[email on web site]
 
Chip Pearson said:
I believe that the compiler evaluates any constants
and replaces them with their values before any code
execution takes place. This is why you can assign to
constants values that can be evaluated arithmetically
with [...] previously declared constants (Const ABC = DEF * 10

Good point! Shoulda thought of that myself. Thanks.


----- original message -----

Chip Pearson said:
I believe that the compiler evaluates any constants and replaces them
with their values before any code execution takes place. This is why
you can assign to constants values that can be evaluated
arithmetically with actual constants (Const ABC = 1234 * 10) or other,
previously declared constants (Const ABC = DEF * 10, where DEF is a
previously declared constant).

As far as efficiency at run time, a simple constant makes no
difference at all and an arithmetic const is only marginally
(microseconds) better than having the arithmetic in the actual run
time code.

Cordially,
Chip Pearson
Microsoft MVP 1998 - 2010
Pearson Software Consulting, LLC
www.cpearson.com
[email on web site]


If I declare "Const foo as Integer = 123", does that allocate storage?

Or does that create a compile-time constant that replaces "foo" wherever
it
is used in executable statements?

If the latter, is "Const foo as Integer = 123*456" and "x = foo" more
efficient than "x = 123*456"? In particular, does the compiler treat "x =
foo" as "x = 56088"?
 
Back
Top