By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
424,853 Members | 1,563 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 424,853 IT Pros & Developers. It's quick & easy.

Implicit Data Type Converson

P: n/a
RN1
Consider the following code snippet:

<%
Dim intA, strA, strB

intA=5
strA="Hello World"
strB=strA+intA
Response.Write(strB)
%>

The above code generates the "Type mismatch" error pointing to the
line strB=strA+intA but why doesn't VBScript implicitly cast the value
of intA into String when intA is concatenated with strA?

I am aware that VBScript supports only one data type which is Variant.
Feb 24 '08 #1
Share this Question
Share on Google+
3 Replies


P: n/a
Because it's trying to cast "Hello World" into an integer.

'+' is typically a numeric operator. Use '&' if you wish to concatenate
values.

Bob Lehmann

"RN1" <rn**@rediffmail.comwrote in message
news:20**********************************@n58g2000 hsf.googlegroups.com...
Consider the following code snippet:

<%
Dim intA, strA, strB

intA=5
strA="Hello World"
strB=strA+intA
Response.Write(strB)
%>

The above code generates the "Type mismatch" error pointing to the
line strB=strA+intA but why doesn't VBScript implicitly cast the value
of intA into String when intA is concatenated with strA?

I am aware that VBScript supports only one data type which is Variant.

Feb 24 '08 #2

P: n/a
Hi RN1

Use "&" I/o "+" to concatenate strings. Implicit casting will be determined
by the operand, not the data types of the arguments.

eg. 5 + 5 <5 & 5

Regards.

"RN1" <rn**@rediffmail.comwrote in message
news:20**********************************@n58g2000 hsf.googlegroups.com...
Consider the following code snippet:

<%
Dim intA, strA, strB

intA=5
strA="Hello World"
strB=strA+intA
Response.Write(strB)
%>

The above code generates the "Type mismatch" error pointing to the
line strB=strA+intA but why doesn't VBScript implicitly cast the value
of intA into String when intA is concatenated with strA?

I am aware that VBScript supports only one data type which is Variant.
Feb 24 '08 #3

P: n/a
"RN1" <rn**@rediffmail.comwrote in message
news:20**********************************@n58g2000 hsf.googlegroups.com...
Consider the following code snippet:

<%
Dim intA, strA, strB

intA=5
strA="Hello World"
strB=strA+intA
Response.Write(strB)
%>

The above code generates the "Type mismatch" error pointing to the
line strB=strA+intA but why doesn't VBScript implicitly cast the value
of intA into String when intA is concatenated with strA?
The + operator is overloaded to perform string concatenation when both
operands are strings (or one is a string the other is Empty).

It might have been preferable that it didn't even do that however when the
VB syntax was first intoduced in VB 1.0 the predecessor dialect of BASIC,
Microsoft Basic, used + to concatenate strings.

Apart from that + is a numerical plus operator and if either of the operands
is numerical in nature (even a boolean) both operands are coerced to a best
fit numerical type usually the type of the operand that has the widest
domain (e.g. Long + Double results in a Double).

Hence in the case of String + Integer or Integer + String the String will be
coerced to an Integer. Had strA been "10" the result would be 15.

As others have pointed out use & which is the string concatenation operator
and treat + as a purely numerical operator.
I am aware that VBScript supports only one data type which is Variant.
Thats a oversimplification.

VBScript supports a wide range of data types namely:- Null, Boolean, Byte,
Integer, Long, Float, Double, String, Decimal, Currency, Object (IDispatch),
Array of Variants.

A _variable_ in VB can have one of two types Variant or Array of Variants.

Its a mistake to think of VB only having one data type. It has many its
just that a single variable may hold a value of different types at various
points in the execution of code.

--
Anthony Jones - MVP ASP/ASP.NET
Feb 26 '08 #4

This discussion thread is closed

Replies have been disabled for this discussion.