Solved

Want addition, not concatenation

Posted on 2006-10-31
5
175 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

Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

Join & Write a Comment

Suggested Solutions

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.
Not sure what the best email signature size is? Are you worried about email signature image size? Follow this best practice guide.
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …

708 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

15 Experts available now in Live!

Get 1:1 Help Now