TheSwamp

Code Red => VB(A) => Topic started by: LE on November 16, 2006, 11:30:22 PM

Title: A quick one - Concatenation operators + & - When to use them?
Post by: LE on November 16, 2006, 11:30:22 PM
I am porting a code in VB to C++ and would like to know when or why a VB programmer use the concatenation operators + or & for the Strings...


Thanks!
Title: Re: A quick one - Concatenation operators + & - When to use them?
Post by: LE on November 16, 2006, 11:48:12 PM
It is mostly curiosity, since I noticed that in the VB code they are using &

i.e:
Code: [Select]
S1 = sTotalSize & StrReverse(sPath) & sTotalSize

I end up using CString and did:
Code: [Select]
S1 = sTotalSize + sPath.MakeReverse() + sTotalSize;

Thanks!
Title: Re: A quick one - Concatenation operators + & - When to use them?
Post by: Bryco on November 16, 2006, 11:56:55 PM
Good question, I've wondered that as well.   & looks cooler though
Title: Re: A quick one - Concatenation operators + & - When to use them?
Post by: Dnereb on November 17, 2006, 02:19:14 AM
No it is usefull.

With the ampersant (&) you will glue to strings to gether without any mathematical process of adding or subtracting.
The + can and will do an addition in certain circumstances. The danger lies especcially in variants because they can hold strings as well as Numeric data.

Imagine a street with a sligtly odd numbering system (due to ancient locaL customs)
in this street (oddstreet) houses are nubered 1 to100 but a appartment buiolding was build later in the middle  with the numbers 50-1 to 50-80
It can happen you divide the numer and suffix in your database (pretty normal)
but if you retrieve those numbers into variants and put them together with the + houseumber 50 + -80 can become -30 because they were interpreted as two numbers to add up.

So the & is for adding string together and the + for adding numbers together.




 
Title: Re: A quick one - Concatenation operators + & - When to use them?
Post by: Bryco on November 17, 2006, 11:10:42 AM
Nice, Peanuts in order Berend
Title: Re: A quick one - Concatenation operators + & - When to use them?
Post by: LE on November 17, 2006, 11:52:13 AM
Thank you, it makes more sense now - !