String Concatenation

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Joe

    #1

    String Concatenation

    Hello All:

    In VB6, we were encouraged never to use "+" to concatenate strings, due to
    issues arisiing from Variants. We wre encouraged to use "&" instead.

    Since .NET doesn't allow Variants, in your opinion, is it still good
    programming practice to use either:

    String.Concat(" a", "b")
    or
    "a" & "b"

    Or are we now allowed to use "a" + "b" as well.

    What has your experience been?

    TIA,
    --
    Joe
  • Carlos J. Quintero [VB MVP]

    #2
    Re: String Concatenation

    Hi Joe,

    If you set Option Strict On, you can use safely "+" since the compiler will
    spot the wrong operations.

    Also, if you are concatenating a lot, use the System.Text.Str ingBuilder
    class instead.
    --

    Best regards,

    Carlos J. Quintero

    MZ-Tools: Productivity add-ins for Visual Studio
    You can code, design and document much faster:
    MZ-Tools has a single goal: To make your everyday programming life easier. As an add-in to several Integrated Development Environment (IDEs) from Microsoft, MZ-Tools adds new menus and toolbars to them that provide many new productivity features.



    "Joe" <Joe@discussion s.microsoft.com > escribió en el mensaje
    news:13866040-E1E9-4873-995C-1B27F622BAE4@mi crosoft.com...[color=blue]
    > Hello All:
    >
    > In VB6, we were encouraged never to use "+" to concatenate strings, due to
    > issues arisiing from Variants. We wre encouraged to use "&" instead.
    >
    > Since .NET doesn't allow Variants, in your opinion, is it still good
    > programming practice to use either:
    >
    > String.Concat(" a", "b")
    > or
    > "a" & "b"
    >
    > Or are we now allowed to use "a" + "b" as well.
    >
    > What has your experience been?
    >
    > TIA,
    > --
    > Joe[/color]


    Comment

    • Cor Ligthert [MVP]

      #3
      Re: String Concatenation

      [color=blue]
      > If you set Option Strict On, you can use safely "+" since the compiler
      > will
      > spot the wrong operations.
      >[/color]
      However if that is Off you can not safe use it, therefore go on using that
      &, it is probably more documentative for VB programmers as well.

      Just my thought,

      Cor


      Comment

      • Herfried K. Wagner [MVP]

        #4
        Re: String Concatenation

        "Joe" <Joe@discussion s.microsoft.com > schrieb:[color=blue]
        > In VB6, we were encouraged never to use "+" to concatenate strings, due to
        > issues arisiing from Variants. We wre encouraged to use "&" instead.
        >
        > Since .NET doesn't allow Variants, in your opinion, is it still good
        > programming practice to use either:
        >
        > String.Concat(" a", "b")
        > or
        > "a" & "b"[/color]

        The recommendation of using '&' instead of '+' still stands. For matters of
        readability and potential compile time optimization I'd chosse the latter of
        the two samples you gave above. Note behavior differences with 'Option
        Strict On' and 'Option Strict Off'.

        --
        M S Herfried K. Wagner
        M V P <URL:http://dotnet.mvps.org/>
        V B <URL:http://classicvb.org/petition/>

        Comment

        • Carlos J. Quintero [VB MVP]

          #5
          Re: String Concatenation

          Hi Cor,

          Yes, I also recommend to use "&" to concatenate strings and in the same way
          "Option Strict On".

          --

          Best regards,

          Carlos J. Quintero

          MZ-Tools: Productivity add-ins for Visual Studio
          You can code, design and document much faster:
          MZ-Tools has a single goal: To make your everyday programming life easier. As an add-in to several Integrated Development Environment (IDEs) from Microsoft, MZ-Tools adds new menus and toolbars to them that provide many new productivity features.




          "Cor Ligthert [MVP]" <notmyfirstname @planet.nl> escribió en el mensaje
          news:%23H5lsEUP GHA.2628@TK2MSF TNGP15.phx.gbl. ..[color=blue]
          > However if that is Off you can not safe use it, therefore go on using that
          > &, it is probably more documentative for VB programmers as well.
          >
          > Just my thought,
          >
          > Cor[/color]


          Comment

          Working...