Welcome to WebmasterWorld Guest from 54.147.44.93

Forum Moderators: ocean10000

Message Too Old, No Replies

Addition in ASP

concatenating rather than adding

     

shmekkyl

4:24 pm on Aug 12, 2003 (gmt 0)

10+ Year Member



I have a simple ASP script I am trying to run. Forgive me if this is a real newbie question but I did some searches first and checked an ASP reference and it seems like I am doing everything right - but apparently not.

Heres the scenario:

A = 1
B = 2

Total = A + B

When it outputs the total, it outputs 12 rather than 3.

Any ideas?

txbakers

4:57 pm on Aug 12, 2003 (gmt 0)

WebmasterWorld Senior Member txbakers is a WebmasterWorld Top Contributor of All Time 10+ Year Member



In VBScript you will have to use the "cint" function to make numbers.

In Javascript use "Number(4) + Number(5)"

give those a try.

RossWal

6:05 pm on Aug 12, 2003 (gmt 0)

10+ Year Member



Hmmmm. I add stuff together all the time in vb script without a cint(). I don't recall seeing the plus sign behave as a concatenation operator. Shmekkyl, are you using Jscript by chance?

shmekkyl

6:16 pm on Aug 12, 2003 (gmt 0)

10+ Year Member



nope...asp

Here's my exact code:


<%
FrontColors = Request("FrontColors")
BackColors = Request("BackColors")

TotalColors = FrontColors + BackColors
%>

Total Colors = <%=TotalColors%>

bcolflesh

6:21 pm on Aug 12, 2003 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



The ampersand (&) is the concatenation operator in ASP.

Jack_Straw

6:39 pm on Aug 12, 2003 (gmt 0)

10+ Year Member



asp can use either jscript or vbscript. The ampersand is the concatenation operator for vbscript, the plus symbol is the concatenation operator for jscript. It sounds like you are inadvertently executing jscript with your asp rather than vbscript.

garann

6:41 pm on Aug 12, 2003 (gmt 0)

10+ Year Member



shmekkyl, any time you set a variable equal to request("anything"), you're defining it as a string. The '+' operator adds two variables together and adding two strings together is concatenation. Did you try txbakers suggestion to use cInt()? That oughta do it..

shmekkyl

8:44 pm on Aug 12, 2003 (gmt 0)

10+ Year Member



<%
FrontColors = Request("FrontColors")
BackColors = Request("BackColors")

FrontColors = cInt(FrontColors)
BackColors = cInt(BackColors)

TotalColors = FrontColors + BackColors
%>

Total Colors = <%=TotalColors%>

Still the same thing. Did i do it right?

txbakers

8:59 pm on Aug 12, 2003 (gmt 0)

WebmasterWorld Senior Member txbakers is a WebmasterWorld Top Contributor of All Time 10+ Year Member



FrontColors = Request("FrontColors")
BackColors = Request("BackColors")

dim TotalColors
TotalColors = cint(FrontColors) + cint(BackColors)

might work for you. give that a try.

shmekkyl

9:03 pm on Aug 12, 2003 (gmt 0)

10+ Year Member



That did it. I thought it uploaded but it didnt. Thanks for all your help.

txbakers

9:10 pm on Aug 12, 2003 (gmt 0)

WebmasterWorld Senior Member txbakers is a WebmasterWorld Top Contributor of All Time 10+ Year Member



Yeah - that was one of the trickiest things to get used to - that ALL web form data are strings. Even the numbers and dates. Once they get put into the request object, you need to manipulate them to make them the type you need.

shmekkyl

11:05 pm on Aug 12, 2003 (gmt 0)

10+ Year Member



its weird that it lets me multiply and divide without doing anything special.

aspdaddy

11:08 pm on Aug 12, 2003 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Thats because those operators cannot be overloaded for strings, so it casts. try multiplying text :)
 

Featured Threads

Hot Threads This Week

Hot Threads This Month