Can anyone tell the difference between the following 3 declarations:
1. dim i, j as long
2. dim i as long, j as long
3. dim i as long
dim j as long
?
Printable View
Can anyone tell the difference between the following 3 declarations:
1. dim i, j as long
2. dim i as long, j as long
3. dim i as long
dim j as long
?
On 1, i will be declared as a variant. The other 2 are exactly the same apart from the layout difference of course.
-adehh
Thanks.
And yet another question: I want my app to be both efficient and economic. When should I use variables of type byte? integer? long?
I rarely use bytes in my apps so I'm not sure in that area, I suppose it depends entirely on what you're doing.
When it comes to integer/long though I always go for long as I once read somewhere that longs are 32bit, apposed to integers being 16bit so longs are faster as it is 'their' environment. Although I'm not 100 percent on this, wait for a second opinion.
-adehh
Depends on how much memory you want each of your variables to use. You need to use what best defines what will be inside of it.
Quote:
Originally posted by jsbach
Thanks.
And yet another question: I want my app to be both efficient and economic. When should I use variables of type byte? integer? long?
Intel type systems use 32 bits efficiently, but I doubt there is a cost for using a 16 bit integer over a 32. Therefore, you might actually save a little space with an integer, and expect no loss of efficiency. However, I believe that using a Byte will not actually save anything. I would expect that a byte type would actually be either an integer or a long, with the upper bytes masked out.
To take a little further I can assure you that VB's Long type performs just as fast as Integer so there is actually no reason not to use Long for at least avoiding some overflow errors.
But isn't it a waste of system resources?Quote:
Originally posted by RhinoBull
To take a little further I can assure you that VB's Long type performs just as fast as Integer so there is actually no reason not to use Long for at least avoiding some overflow errors.
You might say that it wastes two bytes to use a long when an int would suffice. However, I don't actually know whether that is true. I could imagine systems that manage memory such that each variable is stored on some boundary. This could mean that an int is stored in four bytes, despite using only two.
This is the case for booleans. Despite the fact that a boolean could be stored in one bit, eight booleans will not be packed into a byte, but will instead each take up at least two bytes.
If your design is so tight that you are concerned about using four bytes when you could get away with two, you might want to look into how the underlying architecture is actually managing memory. VB doesn't usually get into that.
Only if you use tens (or hundreds) of thousands of them.. there is so much memory available that having an extra few bytes used up isn't an issue.
i believe resources are wasted in code which is not optimized. just consider declaring variables only if you need them.
for instance, you might prefer reusing a same variable more than declaring two of the same kind:
will give same results asVB Code:
Dim a As Long Dim b As Long Dim c As Long b = a + 3 c = b + 4
VB Code:
Dim a As Long Dim c As Long c = a + 3 + 4
stupid example but you get the point. same goes for functions and loops: choose the best ones. use byte arrays for fast computations more than strings, on which VB is particularly slow.
RhinoBull: did you do some tests on that? i found this quite interesting.
And one more question:
What's the difference between:
const PI = 3.14159
and
const PI as long = 3.14159
?
Well for a start, Long is the shortened version of Long Integer, so it cannot hold decimal places. You should use Single or Double.
Using a specific data type is a good idea if possible, as VB may not make the right assumptions about what data type is should be (although I have never tested it myself, and have never heard of any problems).