Solved

Want addition, not concatenation

Posted on 2006-10-31
5
176 Views
Last Modified: 2006-11-18
In my VBScript I have the code line:

                    frm.Text3.value = frm.Text1.value + frm.Text2.value

If Text1.value = 7 and Text2.value = 8, then when the line is executed the entry in Text3 is "78" (concatenation), not 15 (addition).  How do I get the sum into Text3.

Note:  Text3 does display the correct mathematical result if the operator is *, - or / instead of +.
0
Comment
Question by:rlsokoloff
  • 2
  • 2
5 Comments
 
LVL 18

Expert Comment

by:Morcalavin
ID: 17843789
frm.Text3.value = parseInt(frm.Text1.value) + parseInt(frm.Text2.value)
0
 
LVL 13

Accepted Solution

by:
jmundsack earned 50 total points
ID: 17843897
^Not sure what parseInt() is--did you mean CInt()?

rlsokoloff, what's happening is VBScript is trying too hard to be helpful by making some assumptions about the data type your variables contain.  As you may or may not know, all variables in VBScript are "Variants" which are a special type of variable that include both the data, and the "subtype" of the data (i.e., the data type VB will treat the variant as).  When VBScript evaluates frm.Text1.value, it sees the result as a String, so the result is a Variant expression with a String subtype.  When using the + operator on Strings, the default behavior for VBScript is to concatenate.  So you, have to be heavy-handed about variables sometimes in VBScript, and force VBScript to see your expression as a specific Variant subtype--thus the use of CInt() or some other Cxxx() conversion function.

So, Morcalvin's response is correct in its intent--force the variables to be seen as integers--but I'm not quite sure what parseInt()--if CInt() works for you please give the points to Morcalvin because I'm sure that's what he/she meant.
0
 
LVL 18

Expert Comment

by:Morcalavin
ID: 17843959
I mean CInt.  I was in javascript land(parseInt is the parse integer function in javascript)
0
 

Author Comment

by:rlsokoloff
ID: 17844025
Thank you,

I appreciate Morcalavin's response, but jmundsack provided the solution that I could actually implement.

I've used Cint() in Visual Basic, but didn't realize the various C-conversion functions were also part of the VBScripting engine.  Works fine!
0
 

Author Comment

by:rlsokoloff
ID: 17844061
Morcalavin,

Thanks again.  I'll remember that when working in javascript--with which I'm much less familiar.

0

Featured Post

Live: Real-Time Solutions, Start Here

Receive instant 1:1 support from technology experts, using our real-time conversation and whiteboard interface. Your first 5 minutes are always free.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Building a website can seem like a daunting task to the uninitiated but it really only requires knowledge of two basic languages: HTML and CSS.
Find out what you should include to make the best professional email signature for your organization.
In this tutorial viewers will learn how to position items using CSS's three positioning types Create a new HTML document with an internal stylesheet.: Create another div in CSS and name it Absolute : Type "position:absolute;" and "top:10px; left:50p…
In this tutorial viewers will learn how to code links for mobile sites that, once clicked, send a call or text to a specified number. For a telephone link (once clicked, calls a number), begin with a normal "<a href=" link tag. For the href, specify…

813 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now