Results 1 to 8 of 8

Thread: & or +? *RESOLVED*

  1. #1

    Thread Starter
    Frenzied Member agmorgan's Avatar
    Join Date
    Dec 2000
    Location
    Lurking
    Posts
    1,383

    & or +? *RESOLVED*

    Which is the standard/better way to add strings together?
    VB Code:
    1. MsgBox "DataSave.Param" + Format(n + 1) + "=s"
    or
    VB Code:
    1. MsgBox "DataSave.Param" & Format(n + 1) & "=s"
    Is there any difference between using '&' or '+'?
    Is either considered bad practice?

  2. #2
    Frenzied Member Memnoch1207's Avatar
    Join Date
    Feb 2002
    Location
    DUH, Guess...Hint: It's really hot!
    Posts
    1,861
    It depends on what programming language your using.

    VB and ASP use & for string concatenation.

    But the C based languages use the + for string concatenation.
    Being educated does not make you intelligent.

    Need a weekend getaway??? Come Visit

  3. #3

    Thread Starter
    Frenzied Member agmorgan's Avatar
    Join Date
    Dec 2000
    Location
    Lurking
    Posts
    1,383
    Im using VB6 and I always used & for string concatenation
    I just inherited a project and it is full of +'s.
    It still works though.
    I guess the previous programmer had a C background then...

  4. #4
    PowerPoster
    Join Date
    Aug 2001
    Location
    new jersey
    Posts
    2,904
    depends on what it is you want to do. "+" means addition, so if you do

    1 + "2", then answer is 3

    but "&" is concatendation, so if you do

    1 & "2", then the answer is "12"

    See, they are not even remotely the same thing.

  5. #5

    Thread Starter
    Frenzied Member agmorgan's Avatar
    Join Date
    Dec 2000
    Location
    Lurking
    Posts
    1,383
    This gives the same result though
    VB Code:
    1. Private Sub Form_Load()
    2. MsgBox "hi" & " phinds"
    3. MsgBox "hi" + " phinds"
    4. Unload Me
    5. End Sub
    So they can be similar in some situations...

  6. #6
    Frenzied Member RudyL's Avatar
    Join Date
    Mar 2001
    Location
    Chicago
    Posts
    1,519
    Originally posted by agmorgan
    This gives the same result though
    VB Code:
    1. Private Sub Form_Load()
    2. MsgBox "hi" & " phinds"
    3. MsgBox "hi" + " phinds"
    4. Unload Me
    5. End Sub
    So they can be similar in some situations...
    I believe that is called getting lucky!

    That only worked because the compliere was unable to perform the math and add the 2 strings together.. The compiler has a bug in it.. Becarefull and don't put a complementing bug in your code..

    Rudy
    10 different ways to skin a cat and amazingly enough each and every one has the same result, the cat gets skinned! The same can be applied to code, so be nice and accept each others "preferences".

  7. #7
    Junior Member joe_h_31028's Avatar
    Join Date
    Jul 2003
    Posts
    29
    My VB book "Sam's teach yourself VB in 24 hours" shows that either will work, but recommends to use & when putting strings together and reserve + for math, since it looks cleaner,

  8. #8
    Hyperactive Member
    Join Date
    Aug 2002
    Posts
    416
    always use '&' when concatenating 2 strings and '+' when adding a value to a number......

    its just bad programming practice to use '+' when dealing with strings.

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