Results 1 to 6 of 6

Thread: Strings and + and &

  1. #1

    Thread Starter
    Lively Member
    Join Date
    May 2000
    Location
    Norway
    Posts
    112

    Question

    What is commonly used when working with strings?
    Do one use + or & to put two string together?
    & can be annoying when you type fast and get
    string1& string2 ---> error (string1& is not a Long parameter.)

  2. #2
    Fanatic Member Stevie's Avatar
    Join Date
    Mar 2000
    Location
    London, UK
    Posts
    565
    I always use &.

    I'm not sure of this but I think if you use the + and both strings contain numbers then you will get the result of them being added.

    Nope just checked that and that doesn't happen.

    [Edited by Stevie on 07-14-2000 at 05:56 AM]

  3. #3
    Lively Member
    Join Date
    Mar 2000
    Location
    Germany
    Posts
    84
    Hi

    + is only used with strings, while & can be used to add an Integer/Long/... variable to a string

    Code:
    a = "string1"
    b = "string 2"
    
    c = a + " " + b   'returns "string1 string2"
    
    d = 123.456
    
    e = a & " = " & d  'returns "string1 = 123.456"
    hope this helps

  4. #4
    Fanatic Member Stevie's Avatar
    Join Date
    Mar 2000
    Location
    London, UK
    Posts
    565
    Code:
    Dim strValue As String
    
      strValue = "1"
      
      MsgBox strValue & 2
      MsgBox strValue + 2
    The first msgbox will display 12.
    The second msgbox will display 3.

  5. #5
    PowerPoster Fox's Avatar
    Join Date
    Jan 2000
    Location
    *afk*
    Posts
    2,088
    You should always use the & coz if you add a number to a string VB tries to convert the first strig to a number and add the second instead of just putting them together. That only happens if you do something like this:
    (Where the values can also be variables of course)
    Code:
        MsgBox "5" & 3   'Returns 53
        MsgBox "5" + 3   'Returns 8
        MsgBox "5" + "3"   'Returns 53
    Oh and type a bit slower

  6. #6
    Addicted Member
    Join Date
    Apr 2000
    Location
    Sheffield, England.
    Posts
    136
    Originally posted by Nina
    Hi

    + is only used with strings, while & can be used to add an Integer/Long/... variable to a string
    That's a bit confusing. It sound like you're saying the code below wouldn't work.

    Code:
    Dim A As Long
    Dim B As Long
    Dim C As Long
    
    A = 500
    B = 1000
    
    C = A + B
    Just thought I'd clear that up.

    To answer the original question, I always use '&' for adding two strings together. Someone once said that using '+' to add strings together is not very reliable for some reason... I forget now.
    Visual Basic 6 Enterprise Edition + SP4

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