Welcome to WebmasterWorld Guest from 54.145.120.69

Forum Moderators: ocean10000

Message Too Old, No Replies

Addition in ASP

concatenating rather than adding

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

Junior Member

10+ Year Member

joined:May 14, 2003
posts:93
votes: 0


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?

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

Senior Member

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

joined:Sept 1, 2001
posts:4392
votes: 0


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

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

give those a try.

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

Preferred Member

10+ Year Member

joined:May 24, 2002
posts:482
votes: 0


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?
6:16 pm on Aug 12, 2003 (gmt 0)

Junior Member

10+ Year Member

joined:May 14, 2003
posts:93
votes: 0


nope...asp

Here's my exact code:


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

TotalColors = FrontColors + BackColors
%>

Total Colors = <%=TotalColors%>

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

Senior Member

WebmasterWorld Senior Member 10+ Year Member

joined:Feb 21, 2003
posts:2355
votes: 0


The ampersand (&) is the concatenation operator in ASP.
6:39 pm on Aug 12, 2003 (gmt 0)

Junior Member

10+ Year Member

joined:Apr 18, 2002
posts:126
votes: 0


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.
6:41 pm on Aug 12, 2003 (gmt 0)

Preferred Member

10+ Year Member

joined:Sept 4, 2002
posts:508
votes: 0


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..
8:44 pm on Aug 12, 2003 (gmt 0)

Junior Member

10+ Year Member

joined:May 14, 2003
posts:93
votes: 0


<%
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?

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

Senior Member

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

joined:Sept 1, 2001
posts:4392
votes: 0


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

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

might work for you. give that a try.

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

Junior Member

10+ Year Member

joined:May 14, 2003
posts:93
votes: 0


That did it. I thought it uploaded but it didnt. Thanks for all your help.
9:10 pm on Aug 12, 2003 (gmt 0)

Senior Member

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

joined:Sept 1, 2001
posts:4392
votes: 0


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.
11:05 pm on Aug 12, 2003 (gmt 0)

Junior Member

10+ Year Member

joined:May 14, 2003
posts:93
votes: 0


its weird that it lets me multiply and divide without doing anything special.
11:08 pm on Aug 12, 2003 (gmt 0)

Senior Member

WebmasterWorld Senior Member 10+ Year Member

joined:June 13, 2002
posts:2162
votes: 0


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