Results 1 to 16 of 16

Thread: Interesting things about LONGS that I have just found out...

  1. #1

    Thread Starter
    Super Moderator Wokawidget's Avatar
    Join Date
    Nov 2001
    Location
    Headingly Occupation: Classified
    Posts
    9,632

    Interesting things about LONGS that I have just found out...

    OK, if we have:
    VB Code:
    1. Dim lngWoof As Long
    2.    lngWoof = lngWoof +1
    3.    Debug.Print lngWoof
    4.    lngWoof = lngWoof + 15
    5.    Debug.Print lngWoof
    Now, the 1st thing to be "printe" in the immediate window will be the number 1, and the 2nd thing to be printed will be 16...
    Now, if we follow on from the above code and add...
    VB Code:
    1. 'Above code +
    2.    lngWoof = lngWoof & 1
    3.    Debug.Print lngWoof
    4.    lngWoof = lngWoof & 15
    5.    Debug.Print lngWoof
    Now, instead off adding 1, the & statement multiples it by 10 and then adds one. The & 15 cause it to get multiplied by 100 then adds 15

    So & n, would be like: [where n is ANY number]
    VB Code:
    1. lngWoof = (lngWoof * 10^(Len(CStr(n)))) + n
    I just found that interesting...HOW SAD!

    Woka

  2. #2
    Retired VBF Adm1nistrator plenderj's Avatar
    Join Date
    Jan 2001
    Location
    Dublin, Ireland
    Posts
    10,359
    Probably some binary arithmetic thing ?
    Microsoft MVP : Visual Developer - Visual Basic [2004-2005]

  3. #3
    Frenzied Member Lightning's Avatar
    Join Date
    Oct 2002
    Location
    Eygelshoven
    Posts
    1,611
    This is, I think, because "&" is a string operator, so both parts (before and after the & -sign) are converted to string, the &-operation takes place (The second value is put after the first) and then it is converted back to long. But I can be wrong

  4. #4

    Thread Starter
    Super Moderator Wokawidget's Avatar
    Join Date
    Nov 2001
    Location
    Headingly Occupation: Classified
    Posts
    9,632
    Originally posted by Lightning
    This is, I think, because "&" is a string operator, so both parts (before and after the & -sign) are converted to string, the &-operation takes place (The second value is put after the first) and then it is converted back to long. But I can be wrong
    Think u hit the nail on the head there. Seems like a very reasonable explanation

    Woka

  5. #5
    PowerPoster beachbum's Avatar
    Join Date
    Jul 2001
    Location
    Wollongong, NSW, Australia
    Posts
    2,274
    lol u knob!!
    Stuart Laidlaw
    Brightspark Financial Software
    http://www.gstsmartbook.com

  6. #6

  7. #7
    Frenzied Member KayJay's Avatar
    Join Date
    Jul 2001
    Location
    Chennai
    Posts
    1,849

    Re: Interesting things about LONGS that I have just found out...

    Originally posted by Wokawidget
    OK, if we have:
    VB Code:
    1. Dim lngWoof As Long
    2.    lngWoof = lngWoof +1
    3.    Debug.Print lngWoof
    4.    lngWoof = lngWoof + 15
    5.    Debug.Print lngWoof
    Now, the 1st thing to be "printe" in the immediate window will be the number 1, and the 2nd thing to be printed will be 16...
    Now, if we follow on from the above code and add...
    VB Code:
    1. 'Above code +
    2.    lngWoof = lngWoof & 1
    3.    Debug.Print lngWoof
    4.    lngWoof = lngWoof & 15
    5.    Debug.Print lngWoof
    Now, instead off adding 1, the & statement multiples it by 10 and then adds one. The & 15 cause it to get multiplied by 100 then adds 15

    So & n, would be like: [where n is ANY number]
    VB Code:
    1. lngWoof = (lngWoof * 10^(Len(CStr(n)))) + n
    I just found that interesting...HOW SAD!

    Woka
    VB Code:
    1. Private Sub Command1_Click()
    2. Dim x As Long
    3. 'prints 0 -> default value
    4. Debug.Print x
    5.  
    6. x = x + 1
    7. 'prints 1 -> 0 + 1
    8. Debug.Print x
    9.  
    10. x = x + 123
    11. 'prints 124 -> 1 + 123
    12. Debug.Print x
    13.  
    14. x = x & 1
    15. 'prints 1241 -> 124 concantanated with "1"; No addition at all
    16. Debug.Print x
    17.  
    18. x = x & 15
    19. 'prints 124115 -> 1241 concantanated with "15"; No addition at all
    20. Debug.Print x
    21.  
    22. x = x + 5
    23. 'prints 124120 -> 124115 + 5; addition
    24. Debug.Print x
    25.  
    26. End Sub
    where did you get lngWoof = (lngWoof * 10^(Len(CStr(n)))) + n from?

    "Brothers, you asked for it."
    ...Francisco Domingo Carlos Andres Sebastian D'Anconia

  8. #8

    Thread Starter
    Super Moderator Wokawidget's Avatar
    Join Date
    Nov 2001
    Location
    Headingly Occupation: Classified
    Posts
    9,632
    Because:
    VB Code:
    1. lngWoof = (lngWoof * 10^(Len(CStr(n)))) + n
    2. MsgBox lngWoof
    produces EXACTALLY the same answer as:
    VB Code:
    1. lngWoof = lngWoof & n
    2. MsgBox lngWoof


    Woka

    PS Try it if you don't believe me

  9. #9
    Frenzied Member KayJay's Avatar
    Join Date
    Jul 2001
    Location
    Chennai
    Posts
    1,849
    I actually did after U my post. Guess Ur right. Neither had the time nor the inclination look for such a basic arithmetical derivation.

    "Brothers, you asked for it."
    ...Francisco Domingo Carlos Andres Sebastian D'Anconia

  10. #10
    Frenzied Member MerrionComputin's Avatar
    Join Date
    Apr 2001
    Location
    Dublin, Ireland
    Posts
    1,616
    Implicit type conversion = very bad...which is why I routinely cover anybody using variants in my company in a mixture of fish heads and red ants. It's the only way to make the lesson stick
    ----8<---------------------------------------
    NEW - The .NET printer queue monitor component
    ----8<---------------------------------------
    Now with Examples of use

  11. #11

    Thread Starter
    Super Moderator Wokawidget's Avatar
    Join Date
    Nov 2001
    Location
    Headingly Occupation: Classified
    Posts
    9,632
    I agree

    Only I would use mini lobster instead of ants

    Woka

    PS I came across it accidentally as I wants to use a + but for some strange reason I typed a & and tried to figure out why my answers where so large. the actual equation is VERY basic and took all of 0.5 seconds to work out...I did not sit here pondering trying to do it

  12. #12
    Retired VBF Adm1nistrator plenderj's Avatar
    Join Date
    Jan 2001
    Location
    Dublin, Ireland
    Posts
    10,359
    Originally posted by Wokawidget
    the actual equation is VERY basic and took all of 0.5 seconds to work out...I did not sit here pondering trying to do it
    *cough*bull*****cough*
    Microsoft MVP : Visual Developer - Visual Basic [2004-2005]

  13. #13
    old fart Frans C's Avatar
    Join Date
    Oct 1999
    Location
    the Netherlands
    Posts
    2,926
    Try this:
    Debug.Print TypeName(lngWoof & 1)
    Frans

  14. #14

    Thread Starter
    Super Moderator Wokawidget's Avatar
    Join Date
    Nov 2001
    Location
    Headingly Occupation: Classified
    Posts
    9,632
    Originally posted by plenderj
    *cough*bull*****cough*
    Seriously...u canm't say that the equation was difficult to deduct....!
    Mind you, I have done Maths all my life and concider myself an expert-ish
    maybe it comes more naturally to some than others...

    BUT I DID NOT SIT HERE WORKING THE ****ING THING OUT!!!

    ALL RIGHT!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!

    Hmph!...carrot munching hippies, the lot of ya!

    Woka

  15. #15
    Retired VBF Adm1nistrator plenderj's Avatar
    Join Date
    Jan 2001
    Location
    Dublin, Ireland
    Posts
    10,359
    Originally posted by Wokawidget
    Hmph!...carrot munching hippies, the lot of ya!
    Yeah? Well at least we admit it!
    Microsoft MVP : Visual Developer - Visual Basic [2004-2005]

  16. #16

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