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.

Re: Implicit Data Type Converson by Bob

Bob
Sun Feb 24 16:31:19 CST 2008

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" <rn5a@rediffmail.com> wrote in message
news:208260f7-0e17-4d47-8d2f-97f0791e8d0c@n58g2000hsf.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.



Re: Implicit Data Type Converson by Adrienne

Adrienne
Sun Feb 24 16:32:18 CST 2008

Gazing into my crystal ball I observed RN1 <rn5a@rediffmail.com> writing
in news:208260f7-0e17-4d47-8d2f-
97f0791e8d0c@n58g2000hsf.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.
>

Because you are trying to add a number to a character, what you want is
strB = strA & intA

--
Adrienne Boswell at Home
Arbpen Web Site Design Services
http://www.cavalcade-of-coding.info
Please respond to the group so others can share


Re: Implicit Data Type Converson by rob^_^

rob^_^
Sun Feb 24 16:31:05 CST 2008

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" <rn5a@rediffmail.com> wrote in message
news:208260f7-0e17-4d47-8d2f-97f0791e8d0c@n58g2000hsf.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.


Re: Implicit Data Type Converson by Anthony

Anthony
Tue Feb 26 04:23:12 CST 2008

"RN1" <rn5a@rediffmail.com> wrote in message
news:208260f7-0e17-4d47-8d2f-97f0791e8d0c@n58g2000hsf.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