?
Solved

Want addition, not concatenation

Posted on 2006-10-31
5
Medium Priority
?
181 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 200 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

This article discusses how to create an extensible mechanism for linked drop downs.
This article explains how to prepare an HTML email signature template file containing dynamic placeholders for users' Azure AD data. Furthermore, it explains how to use this file to remotely set up a department-wide email signature policy in Office …
In this tutorial viewers will learn how to style elements, such a divs, with a "drop shadow" effect using the CSS box-shadow property Start with a normal styled element, such as a div.: In the element's style, type the box shadow property: "box-shad…
Video by: Mark
This lesson goes over how to construct ordered and unordered lists and how to create hyperlinks.

800 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