Results 1 to 7 of 7

Thread: & or + ?

  1. #1

    Thread Starter
    Addicted Member LAURENS's Avatar
    Join Date
    Jan 2000
    Location
    Utrecht, the Netherlands
    Posts
    138

    Smile

    What do you prefer for a string concatenation and why ?

    I prefer & because
    - it shows explicitly what the code is supposed to do.
    - it forces a concatenation even if the variables aren't
    declared as string.

    A collegue of mine insist on using +. He claims it's faster, (which was true in vb4 but not anymore in vb5 and up) and better in general because & is a variant operator which you should avoid. This wouldn't be so annoying if he hadn't developed the habit of scanning all code on & and changing them to + which becomes very annoying after a while.

    So what do you think ?






    Regards,
    Laurens

    Using VB5 Enterprise edition SP3
    VB6 Enterprise edition SP5

  2. #2
    Fanatic Member
    Join Date
    Mar 2000
    Location
    That posh bit of England known as Buckinghamshire
    Posts
    658

    Thumbs up & all the way.

    & is unambiguous. It clearly defines that you will be concatenating two strings.

    + can be misinterpreted, and many beginners in VB ask why the get the answer "1234" when doing the operation "12" + "34".
    Iain, thats with an i by the way!

  3. #3
    Hyperactive Member
    Join Date
    Dec 1999
    Posts
    321

    & is the character

    Altough the ampersand is a variant operator it's still better to use in code because you see exactly what the code is supposed to do.

    There's a disadvantage, however, but as you get more experienced, it disappears.

    If I write this (unformatted by vb):

    seekval = "the value being seek
    msgbox SeekVal&" times " & OtherVar

    then vb says it's syntax error, and I ask myself why da hell? It's just the simple reason that I'm telling it that the variable 'SeekVal' is a Long when it actually is a String, so vb get's all smarty and starts arguing.

    The plus sign is actually only ment for addition calculations from VB5. The reason why it can work as a concatenation symbol is for compatibility with old version programmers.

    Signed, Rodik ([email protected])
    Programmer,usesVB6ED
    ===========================
    Copyright©RodikCo,2002.

    Dont mind this signature ;] Its old

  4. #4

    Thread Starter
    Addicted Member LAURENS's Avatar
    Join Date
    Jan 2000
    Location
    Utrecht, the Netherlands
    Posts
    138

    Thumbs up

    Thanks guys, I couldn't agree more.
    Regards,
    Laurens

    Using VB5 Enterprise edition SP3
    VB6 Enterprise edition SP5

  5. #5
    Addicted Member
    Join Date
    Jun 2000
    Posts
    132
    '+' is a mathematical term to add 2 numbers together.
    '&' is a VB term to add 2 strings together.

    Reality is an illusion caused by by lack of drugs

    Is this real or am i just having a dream?

  6. #6
    Hyperactive Member
    Join Date
    Mar 2000
    Posts
    461
    Actually '&' is a VB term to implicitly convert the contents of both sides into strings and concatenate them.



  7. #7
    Hyperactive Member
    Join Date
    Jul 2002
    Location
    Canada
    Posts
    455
    Hello LAURENS,

    As a VB database user I prever to use "&".

    If you made a new record in a recordset all fields are Null.

    If you want to add (with "+") a Null text field to a string variable, the operation will gives an "invalid use of null" error.

    When you are using the "&" sign, the error will not occur.

    Maybe this is a reason to use "&" !
    Nice regards,

    Michelle.

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