def numbers

J

John

What is the best way to def integer counters? I don't understand vb's
definitions (Integer, long etc.). It used to be you used the def that
used the least memory. Is that still true?

for n = 1 to 100

what would you def n as for most efficiency and least memory use. Or
isn't that a factor anymore?

I am getting back into programming a little after 18 years.

John
 
R

RB Smissaert

I would use Long. In this case you could use Byte, but then if maybe later
you go beyond the reach of Byte then
you will get an overflow error. Long is more efficient
than Integer, so I would do Dim n As Long.

RBS
 
R

Rick Rothstein \(MVP - VB\)

What is the best way to def integer counters? I don't understand vb's
definitions (Integer, long etc.). It used to be you used the def that used
the least memory. Is that still true?

for n = 1 to 100

what would you def n as for most efficiency and least memory use. Or isn't
that a factor anymore?

I am getting back into programming a little after 18 years.

You are thinking of DefTYPE where TYPE is one of the data types (for
example, DefInt, DefStr, etc.). The use of DefType's to identify ranges of
letters that will automatically be declared as a certain data type went out
of favor quite some time ago. Simple lists of Dim statements identifying
each variable are the norm today. Just remember that in VB and VBA, while
you can combine multiple variable in a single Dim statement, you must
identify each variable's type individually. So, while in some languages you
can do the equivalent of this...

Dim A, B, C As Long

in VB and VBA you ***MUST*** do it this way instead...

Dim A As Long, B As Long, C As Long

If you forget and do it like the first Dim statement, only C would be
declared as a Long... A and B would be Variants (which take up more memory
and are slower to work with). Most people seem to like to stick with one
variable per Dim statement....

Dim A As Long
Dim B As Long
Dim C As Long

sort of makes an easy to look up "dictionary" of your variables.

Rick
 
G

Guest

Unless you are using a 64 bit operating system, the 32 bit registers line up
with Longs. Using Byte or Integer must be handled specially, so it is more
efficient to use Longs.

When you had only 16K of memory, it was more of a premimum than when you
have more than 256 MB
 
C

Chip Pearson

John,


The difference between an Integer and a Long is the amount of memory
allocated for each and the largest number that such a variable can contain.
An Integer is 2 bytes in length and, assuming signed values, can contain a
number between -32,768 to 32,767. A Long is 4 bytes and can contain a number
between -2,147,483,648 to 2,147,483,647. If you attempt to exceed these
bounds, you'll get an overflow error. Neither an Integer nor a Long may
contain fractional numbers (e.g., 1.234). The Single and Double data types
are used for fractional numbers and these types differ in the range of the
values that may be stored and the number of digits of precision (e.g., how
many decimal places) the number may contain.

Look in VBA Help (not Excel Help) for "Data Type Summary" for more
information about the various data types and their limitations.

Generally speaking, you should use Longs for integral values even if the
given number will fit within an Integer or even a Byte. This is due to the
fact that the CPU works in 32-bit (4 byte) "chunks" and must do additional
conversions for Integer values.

--
Cordially,
Chip Pearson
Microsoft MVP - Excel
Pearson Software Consulting
www.cpearson.com
(email on the web site)
 

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