Solved

Want addition, not concatenation

Posted on 2006-10-31
5
178 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
AJAX Wordpress Not Reading Variable 2 59
CSS Style 8 43
w3c parsing errors 4 22
CSS question 4 29
SASS allows you to treat your CSS code in a more OOP way. Let's have a look on how you can structure your code in order for it to be easily maintained and reused.
When it comes to write a Context Sensitive Help (an online help that is obtained from a specific point in state of software to provide help with that state) ,  first we need to make the file that contains all topics, which are given exclusive IDs. …
The viewer will learn the benefit of using external CSS files and the relationship between class and ID selectors. Create your external css file by saving it as style.css then set up your style tags: (CODE) Reference the nav tag and set your prop…
Learn how to create flexible layouts using relative units in CSS.  New relative units added in CSS3 include vw(viewports width), vh(viewports height), vmin(minimum of viewports height and width), and vmax (maximum of viewports height and width).

730 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