addition?

  • Thread starter Thread starter James
  • Start date Start date
J

James

This must be the dumbest of problems I ever came up with
but I just cannot remember how to add????

I am using frontpage and getting some results from a
database which then i need to manipulate.

Let us assume database returned values No1 = 8 and No2 = 3

If I then try a subtraction:
<%test=No1-No2%>
it would result with test = 5. Perfect

Similarly multiplication and division also work well. Also
perfect.

Even complex formulas involving several multiplications
and divisions work fine.

But if I try a simple addition:
<%test=No1+No2%>
I get test = 83 (result is concatenated not added)

Can anyone help me with a simple solution for this. I do
not wish to have to resort to script for a simple addition.

Thanks.
 
test = CInt(No1) + CInt(No2)

It has to do with data types and VBScript syntax. In VBScript, the "+"
operator can be used interchangeably with numbers (addition) and strings
(concatenation). Because variables in VBScript are variants (non-typed),
VBScript uses some intelligence to figure out what data type you're working
with. If your data is string data, and you use the "-" (minus) operator,
which only can be used with numbers, VBScript will cast the strings as
numbers (if possible) and subtract. However, if they are strings, and you
use the "+" operator, it will assume that you are concatenating the strings.
The CInt() function casts the values as numbers, which informs the compiler
that you want to do math, not string manipulation.

--
HTH,

Kevin Spencer
Microsoft MVP
..Net Developer
http://www.takempis.com
Big Things are made up of
Lots of Little Things.
 
Back
Top