Home All Groups Group Topic Archive Search About
Author
1 Mar 2006 1:31 PM
Joe
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

Author
1 Mar 2006 2:16 PM
Carlos J. Quintero [VB MVP]
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.StringBuilder
class instead.
--

Best regards,

Carlos J. Quintero

MZ-Tools: Productivity add-ins for Visual Studio
You can code, design and document much faster:
http://www.mztools.com


Show quoteHide quote
"Joe" <J**@discussions.microsoft.com> escribió en el mensaje
news:13866040-E1E9-4873-995C-1B27F622BAE4@microsoft.com...
> 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
Author
1 Mar 2006 3:04 PM
Cor Ligthert [MVP]
> If you set Option Strict On, you can use safely "+" since the compiler
> will
> spot the wrong operations.
>
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
Author
2 Mar 2006 10:06 AM
Carlos J. Quintero [VB MVP]
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:
http://www.mztools.com



Show quoteHide quote
"Cor Ligthert [MVP]" <notmyfirstn***@planet.nl> escribió en el mensaje
news:%23H5lsEUPGHA.2628@TK2MSFTNGP15.phx.gbl...
> 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
Author
1 Mar 2006 3:58 PM
Herfried K. Wagner [MVP]
"Joe" <J**@discussions.microsoft.com> schrieb:
> 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"

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/>