Results 1 to 13 of 13

Thread: variables declaration

  1. #1

    Thread Starter
    Addicted Member
    Join Date
    Aug 2002
    Posts
    159

    variables declaration

    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
    ?

  2. #2
    Hyperactive Member
    Join Date
    Apr 2002
    Location
    UK
    Posts
    506
    On 1, i will be declared as a variant. The other 2 are exactly the same apart from the layout difference of course.

    -adehh

  3. #3

    Thread Starter
    Addicted Member
    Join Date
    Aug 2002
    Posts
    159
    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?

  4. #4
    Hyperactive Member
    Join Date
    Apr 2002
    Location
    UK
    Posts
    506
    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

  5. #5
    Frenzied Member
    Join Date
    May 2003
    Location
    So Cal
    Posts
    1,564
    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.

    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?

  6. #6
    Super Moderator Shaggy Hiker's Avatar
    Join Date
    Aug 2002
    Location
    Idaho
    Posts
    40,106
    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.

  7. #7
    PowerPoster RhinoBull's Avatar
    Join Date
    Mar 2004
    Location
    New Amsterdam
    Posts
    24,132
    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.

  8. #8

    Thread Starter
    Addicted Member
    Join Date
    Aug 2002
    Posts
    159
    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.
    But isn't it a waste of system resources?

  9. #9
    Super Moderator Shaggy Hiker's Avatar
    Join Date
    Aug 2002
    Location
    Idaho
    Posts
    40,106
    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.

  10. #10
    Super Moderator si_the_geek's Avatar
    Join Date
    Jul 2002
    Location
    Bristol, UK
    Posts
    41,974
    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.

  11. #11
    Fanatic Member wildcat_2000's Avatar
    Join Date
    Nov 2000
    Location
    Italy
    Posts
    727
    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:

    VB Code:
    1. Dim a As Long
    2. Dim b As Long
    3. Dim c As Long
    4.  
    5. b = a + 3
    6. c = b + 4
    will give same results as
    VB Code:
    1. Dim a As Long
    2. Dim c As Long
    3.  
    4. 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.
    Last edited by wildcat_2000; Apr 1st, 2004 at 10:40 AM.
    When your car breaks down,
    close all windows and retry

    => please rate all users posts! <=

  12. #12

    Thread Starter
    Addicted Member
    Join Date
    Aug 2002
    Posts
    159
    And one more question:

    What's the difference between:
    const PI = 3.14159
    and
    const PI as long = 3.14159
    ?

  13. #13
    Super Moderator si_the_geek's Avatar
    Join Date
    Jul 2002
    Location
    Bristol, UK
    Posts
    41,974
    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).

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width