Hi guys, query about ampersand..

Author Message
rats

  • Total Posts : 6
  • Scores: 0
  • Reward points : 0
  • Joined: 12/25/2011
  • Status: offline
Hi guys, query about ampersand.. Sunday, December 25, 2011 8:33 AM (permalink)
0
 
Hi, am new to vbscripting & have a doubt, pls tell me where am wrong..
 
Ampersand(&) is used for string concatenation in vbscript, but when we use counter variable in programs eg in loops we use '&' in front of variable. In C language ampersand denotes address of variable, but in vb script it shows string concatenation so why we use ampersand(&) in front of variable in loops in vbscript. for example,
<script type="text/vbscript">
For i = 0 To 2
document.write("The number is " & i )
Next
</script>
outputs:-
The number is 0
The number is 1
The number is 2

why we are using '&i' here instead of just 'i'....if its for concatenation, then the program code should work if we remove '&' but it doesn't work then. please clarify where am wrong...thanks a lot..
 
#1
    ehvbs

    • Total Posts : 3320
    • Scores: 112
    • Reward points : 0
    • Joined: 6/22/2005
    • Location: Germany
    • Status: offline
    Re:Hi guys, query about ampersand.. Sunday, December 25, 2011 9:01 PM (permalink)
    0
     Dim s : s = "abc"
    Dim n : n = 4711
    WScript.Echo 0, n + n
    WScript.Echo 1, s & s
    WScript.Echo 2, s & n
    WScript.Echo 3, s + s
    On Error Resume Next
    WScript.Echo 4, n + s
    WScript.Echo 5, Err.Description
    On Error GoTo 0
    WScript.Quit 0 

     0 9422
    1 abcabc
    2 abc4711
    3 abcabc
    5 Typen unverträglich 

    • (0) + is the operator for numerical addition
    • (1) & is the operator for string concatenation with (2) automatic stringification
    • (3) + can be used to concatenate strings
    • (4/5) + won't concatenate strings with items of non-string type
    Your
    document.write("The number is " & i )
    contains spurious () and the & is needed because otherwise you have two operands (string and number) but no operator.
     
    #2
      rats

      • Total Posts : 6
      • Scores: 0
      • Reward points : 0
      • Joined: 12/25/2011
      • Status: offline
      Re:Hi guys, query about ampersand.. Monday, December 26, 2011 10:30 AM (permalink)
      0
      Okay..Thanks a lot for making me understandthis :)... so '&' in vbscript is used to concatenate character string with any number or digit...am i right now..
       
      thanks again..learning vbscript so would be visiting this great forum every now n then..thnxxx:)
       
      #3
        ehvbs

        • Total Posts : 3320
        • Scores: 112
        • Reward points : 0
        • Joined: 6/22/2005
        • Location: Germany
        • Status: offline
        Re:Hi guys, query about ampersand.. Monday, December 26, 2011 8:13 PM (permalink)
        0
        The & operator isn't restricted to pairs of a string and a number; it will concatenate all items that can be stringified (CStr()) into a string.
         
        #4
          rats

          • Total Posts : 6
          • Scores: 0
          • Reward points : 0
          • Joined: 12/25/2011
          • Status: offline
          Re:Hi guys, query about ampersand.. Thursday, December 29, 2011 9:23 AM (permalink)
          0
          Hmm..got it now..was thinking whether it is also used to denote that argument in 'document. write' is a variable..so checked it via
          1. document.write("i")
          2. document.write(i)
          3. document.write(&i)
          4. document.write("&i")
          5. document.write("the number is " i)
           
          here only 2nd option is good. So,yes understood that is used only for concatenation.
           
          Thanks, at learning stage for vb script, not as easy as i thought when i came across vbscript objects & all
          would be coming here often :)
           
          thanks for d help!
           
          #5

            Online Bookmarks Sharing: Share/Bookmark

            Jump to:

            Current active users

            There are 0 members and 1 guests.

            Icon Legend and Permission

            • New Messages
            • No New Messages
            • Hot Topic w/ New Messages
            • Hot Topic w/o New Messages
            • Locked w/ New Messages
            • Locked w/o New Messages
            • Read Message
            • Post New Thread
            • Reply to message
            • Post New Poll
            • Submit Vote
            • Post reward post
            • Delete my own posts
            • Delete my own threads
            • Rate post

            2000-2012 ASPPlayground.NET Forum Version 3.9