Be nice

E

Evertjan.

George Hester wrote on 02 nov 2003 in
microsoft.public.inetserver.asp.general:
It may help if someone at Microsoft knows how to write VBScript.
Since when was "+" a string concatenation character in VBScript?

"+" always was a string concatenation operator in VBScript
and in most earlier Basixc dialects.

The "&" as an more specific alternative is a latecomer in Basic.

Stopping the "+" operator having this behavour and reserving it for
numerical adding and optional unary operator behavour, would be a good
thing, IMHO.
 
B

Bob Barrows

George said:
It may help if someone at Microsoft knows how to write VBScript.
Since when was "+" a string concatenation character in VBScript?

http://support.microsoft.com/default.aspx?scid=kb;en-us;253604&Product=asp


http://msdn.microsoft.com/library/en-us/script56/html/vsidxconcatenation.asp

+ is both a concatenation and an addition operator. The only difference in
vbscript between + and & is that & will force concatenation. If both
operands can be coerced to numbers, addition will occur. If either operand
cannot be coerced to numeric, concatenation will occur. When & is used,
concatenation is the only operation allowed, which means that both operands
are coerced to strings before the operation is done. This is
well-documented.

Bob Barrows
 
A

Aaron Bertrand [MVP]

+ is both a concatenation and an addition operator. The only difference in
vbscript between + and & is that & will force concatenation. If both
operands can be coerced to numbers, addition will occur. If either operand
cannot be coerced to numeric, concatenation will occur. When & is used,
concatenation is the only operation allowed, which means that both operands
are coerced to strings before the operation is done. This is
well-documented.

I don't think George is trying to state that this isn't well-documented. I
think he is just implying (and I tend to agree) that the default
recommendation for string operations be + ... I think they should be using &
in all cases where they are not intending to perform an addition operation
(and I think it's poor that this dual construct was ever there in the first
place). Just because you *can* do something, and that it *is* well
documented, doesn't mean it's the way you *should* do it...

Now, of course, you could argue from the other side, e.g. that the +
operator has multiple functions in other languages, such as T-SQL, JScript,
C#, etc. But when an alternative that is better defined is there, why not
use it instead?
 
B

Bob Barrows

Aaron said:
I don't think George is trying to state that this isn't
well-documented. I think he is just implying (and I tend to agree)
that the default recommendation for string operations be +

:)
No, that is your interpretation of what George was saying. I think "It may
help if someone at Microsoft knows how to write VBScript." is a pretty clear
statement. And a very incorrect statement in this case. :)

Bob Barrows
 
J

Joe Fawcett

Ken Schaefer said:
Apparently since version 1 of VBScript according to my WSH Help file.

Well, it seems to be an overloaded operator that does different things
depending what you use it on.

Cheers
Ken
There's also this behaviour:

Dim sPlus, sAmpersand
sPlus = "Hello " + Null
sAmpersand = "Hello " & Null
WScript.echo sPlus & ""
WScript.echo sAmpersand & ""

Addition sign and Null gives Null, ampersand and Null treats Null as empty
string.
 
C

CJM

Bob Barrows said:
:)
No, that is your interpretation of what George was saying. I think "It may
help if someone at Microsoft knows how to write VBScript." is a pretty clear
statement. And a very incorrect statement in this case. :)

Actually, I agree with you both.

I agree George said what Bob said, but I think George meant what Aaron said
he meant...! George...??

Personally agree with Aaron. And Bob.

....er, I think.

Yes '+' is overloaded, and thus can be used fairly in this example. But
surely '&' would be the better default choice.

Chris
 
R

Roland Hall

It's overloaded and confusing.

<%@ Language=VBScript %>
<%
Option Explicit
dim an, a, eq, e, plus
an = "&" + "&"
a = "&"
eq = "=" & "="
e = "="
plus = "+"
%>
<script type="text/vbscript">
sub alertit(a, b)
msgbox "" & a + "<%=eq%>" + b & "<%=e%>TRUE", 64, "TRUE"
end sub
</script>
<script type="text/javascript">
var a<%=e%>"<%=a%>" <%=plus%> "<%=a%>", b<%=e%>"<%=an%>";
if(a <%=eq%> '&&' <%=an%> b <%=eq%> '&&') {
alert("a" <%=plus%> "<%=eq%>" <%=plus%> "b" <%=plus%> "<%=e%>" <%=plus%>
"TRUE");
alertit(a, b);
}
</script>

Bob Barrows said:
:)
No, that is your interpretation of what George was saying. I think "It may
help if someone at Microsoft knows how to write VBScript." is a pretty clear
statement. And a very incorrect statement in this case. :)

Actually, I agree with you both.

I agree George said what Bob said, but I think George meant what Aaron said
he meant...! George...??

Personally agree with Aaron. And Bob.

....er, I think.

Yes '+' is overloaded, and thus can be used fairly in this example. But
surely '&' would be the better default choice.

Chris
 
K

keyur shah

This sounds crazy... when did developers @ microsoft started mixing
languages... which is even not permitted in .NET :)



Keyur Shah
Verizon Communications
732-423-0745
 
B

Bob Barrows

keyur said:
This sounds crazy... when did developers @ microsoft started mixing
languages... which is even not permitted in .NET :)

Again: they didn't "+" is a perfectly good concatenation character in
vbscript. I've already posted a link to the documentation in this thread.

That said, I always use "&" for concatenation in vbscript. This habit really
burns me when I'm writing jscript code ...

Bob Barrows
 
R

Ray at

Again: they didn't "+" is a perfectly good concatenation character in
vbscript. I've already posted a link to the documentation in this thread.

That said, I always use "&" for concatenation in vbscript. This habit really
burns me when I'm writing jscript code ...

Have you coded in php at all? That's the stupidest concatenator I've ever
seen. . It goes against all my instincts, but that's just because I was
raised in a Microsoft family, I guess.

Ray at work
 
B

Bob Barrows

Ray at said:
Have you coded in php at all?
Nope.

That's the stupidest concatenator I've
ever seen. .

Uuuum - what concatenator are you talking about?

Bob
 
R

Ray at

exactly ;o)

$var1="Bob";
$var2="Barrows";

$var3 = $var1;
$var3 .= " ";
$var3 .= $var2;

produces the same result

This further exposes my mother tongue. I never think of the += type of
things, even when I'm doing a set /a in a batch file. I suck.

Ray at work
 
G

George Hester

Nope. No nulls.

--
George Hester
__________________________________
As previously noted in this thread, "+" and "&" handle Nulls differently. Is there a possibility that any of the fields in your recordset contain Null?

Bob Barrows

--
Microsoft MVP -- ASP/ASP.NET
Please reply to the newsgroup. The email account listed in my From header is my spam trap, so I don't check it very often. You will get a quicker response by posting to the newsgroup.
Response.Write iCnt + " <a href=""http://" + rs(1) + """ target=_new>" + rs(1) + "</a><BR>"


Response.Write iCnt & " <a href=""http://" & rs(1) & """ target=_new>" & rs(1) & "</a><BR>"
No problem.

Note there are enough strings in here to show that coersion should not be an issue.

--
George Hester
__________________________________
It may help if someone at Microsoft knows how to write VBScript. Since when was "+" a string concatenation character in VBScript?

http://support.microsoft.com/default.aspx?scid=kb;en-us;253604&Product=asp
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,020
Latest member
GenesisGai

Latest Threads

Top