homepage Welcome to WebmasterWorld Guest from 23.20.63.27
register, free tools, login, search, pro membership, help, library, announcements, recent posts, open posts,
Become a Pro Member

Visit PubCon.com
Home / Forums Index / Microsoft / Microsoft IIS Web Server and ASP.NET
Forum Library, Charter, Moderators: ocean10000

Microsoft IIS Web Server and ASP.NET Forum

    
Addition in ASP
concatenating rather than adding
shmekkyl




msg:948902
 4:24 pm on Aug 12, 2003 (gmt 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?

 

txbakers




msg:948903
 4:57 pm on Aug 12, 2003 (gmt 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.

RossWal




msg:948904
 6:05 pm on Aug 12, 2003 (gmt 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?

shmekkyl




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

nope...asp

Here's my exact code:


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

TotalColors = FrontColors + BackColors
%>

Total Colors = <%=TotalColors%>

bcolflesh




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

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

Jack_Straw




msg:948907
 6:39 pm on Aug 12, 2003 (gmt 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.

garann




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

shmekkyl




msg:948909
 8:44 pm on Aug 12, 2003 (gmt 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?

txbakers




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

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

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

might work for you. give that a try.

shmekkyl




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

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

txbakers




msg:948912
 9:10 pm on Aug 12, 2003 (gmt 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.

shmekkyl




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

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

aspdaddy




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

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

Global Options:
 top home search open messages active posts  
 

Home / Forums Index / Microsoft / Microsoft IIS Web Server and ASP.NET
rss feed

All trademarks and copyrights held by respective owners. Member comments are owned by the poster.
Home ¦ Free Tools ¦ Terms of Service ¦ Privacy Policy ¦ Report Problem ¦ About ¦ Library ¦ Newsletter
WebmasterWorld is a Developer Shed Community owned by Jim Boykin.
© Webmaster World 1996-2014 all rights reserved