By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
459,253 Members | 1,652 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 459,253 IT Pros & Developers. It's quick & easy.

VB -> Adding Numbers

P: n/a
Scenerio:

label1.text = 0
textbox1.text = 0

textbox2.text = textbox1.text + label1.text

It turns out it isnt adding the numbers, it attaches the new number next to
the old number

if label1.text = 1 then textbox1 is = 2 then 1+2 = 3 ....it writes 1+2= 12
INSTEAD of 3
Nov 21 '05 #1
Share this Question
Share on Google+
6 Replies


P: n/a
Hi,

The + operator in VB adds variables according to their types, not their
values. You are adding two strings together so it concatenates them. To do
what you want use:

textbox2.text = CStr(CInt(textbox1.text) + CInt(label1.text))

Be sure to specifiy Option Explicit On and Option Strict On at the top of
your code file so the IDE can tell you when you are doing something illegal
or weird. Like trying to add a string to an integer. Good luck! Ken.

--
Ken Dopierala Jr.
For great ASP.Net web hosting try:
http://www.webhost4life.com/default.asp?refid=Spinlight
If you sign up under me and need help, email me.

"Hareth" <ab******@hotmail.com> wrote in message
news:Ot**************@TK2MSFTNGP11.phx.gbl...
Scenerio:

label1.text = 0
textbox1.text = 0

textbox2.text = textbox1.text + label1.text

It turns out it isnt adding the numbers, it attaches the new number next to the old number

if label1.text = 1 then textbox1 is = 2 then 1+2 = 3 ....it writes 1+2= 12
INSTEAD of 3

Nov 21 '05 #2

P: n/a
"Hareth" <ab******@hotmail.com> schrieb:
label1.text = 0
textbox1.text = 0

textbox2.text = textbox1.text + label1.text

It turns out it isnt adding the numbers, it attaches the new number next to the old number


Add these lines on top of your source file:

\\\
Option Explicit On
Option Strict On
///

Then use this code:

\\\
Label1.Text = CStr(0)
TextBox1.Text = CStr(0)
TextBox2.Text = CStr(CInt(TextBox1.Text) + CInt(Label1.Text))
///

The labels' 'Text' property is of type string, and '+' will be used as
string concatenation operator when dealing with strings.

--
Herfried K. Wagner [MVP]
<URL:http://dotnet.mvps.org/>
Nov 21 '05 #3

P: n/a
Thanx.....

The posts helped alot........
Nov 21 '05 #4

P: n/a
This is probably the most fundamental thing to understand about data types.
When you type into a textbox, no matter what you type, it is treated as text
(string) since you typed it into a "text"box. When you use the "+" operand,
it is treated as string addition (concatenation), rather than numeric
addition.

You need to convert the values to a numeric type before doing the math. You
should also check to see IF the values typed into the textboxes are, in
fact, numeric BEFORE you attempt to convert them, since "Green" doesn't
convert to a numeral. VS.NET's range or regular expression validators can
ensure that only numerals are entered.
"Hareth" <ab******@hotmail.com> wrote in message
news:Ot**************@TK2MSFTNGP11.phx.gbl...
Scenerio:

label1.text = 0
textbox1.text = 0

textbox2.text = textbox1.text + label1.text

It turns out it isnt adding the numbers, it attaches the new number next
to the old number

if label1.text = 1 then textbox1 is = 2 then 1+2 = 3 ....it writes 1+2= 12
INSTEAD of 3

Nov 21 '05 #5

P: n/a
Hey Herfried,

Something tells me our code looks a lot alike! Ken.

"Herfried K. Wagner [MVP]" <hi***************@gmx.at> wrote in message
news:uT**************@tk2msftngp13.phx.gbl...
"Hareth" <ab******@hotmail.com> schrieb:
label1.text = 0
textbox1.text = 0

textbox2.text = textbox1.text + label1.text

It turns out it isnt adding the numbers, it attaches the new number next

to
the old number


Add these lines on top of your source file:

\\\
Option Explicit On
Option Strict On
///

Then use this code:

\\\
Label1.Text = CStr(0)
TextBox1.Text = CStr(0)
TextBox2.Text = CStr(CInt(TextBox1.Text) + CInt(Label1.Text))
///

The labels' 'Text' property is of type string, and '+' will be used as
string concatenation operator when dealing with strings.

--
Herfried K. Wagner [MVP]
<URL:http://dotnet.mvps.org/>

Nov 21 '05 #6

P: n/a
"Ken Dopierala Jr." <kd*********@wi.rr.com> schrieb:
Something tells me our code looks a lot alike! Ken.


:-)

--
Herfried K. Wagner [MVP]
<URL:http://dotnet.mvps.org/>
Nov 21 '05 #7

This discussion thread is closed

Replies have been disabled for this discussion.