Adding numbers

Algar

Freshman
Joined
Jun 17, 2003
Messages
28
Location
Toronto
I have 9 Textboxes which hold numbers.
I want to add the 9 numbers together and put the total in a 10th Testbox

Visual Basic:
Dim R1 As Long

R1 = Trim(CInt(Trim(txtH1R.Text))) + Trim(CInt(Trim(txtH2R.Text))) + Trim(CInt(Trim(txtH3R.Text))) + Trim(CInt(Trim(txtH4R.Text))) + Trim(CInt(Trim(txtH5R.Text))) + Trim(CInt(Trim(txtH6R.Text))) + Trim(CInt(Trim(txtH7R.Text))) + Trim(CInt(Trim(txtH8R.Text))) + Trim(CInt(Trim(txtH9R.Text)))

txtTotal.Text = Trim(CStr(R1))

As far as I know "+" only appends when theres 2 strings.
The CInt should take care of that, but for some reason its appending all 9 numbers togther.

Anyone have any ideas ?
 
Ok, everything works now after I removed the outside Trim.

Visual Basic:
CInt(Trim(txtH1R.Text))
rather then:
Visual Basic:
Trim(CInt(Trim(txtH1R.Text)))

Not sure why exactly though, does the Trim return strings only ?

If thats the case how would it allow putting a string into a Long variable ?
 
Yes Trim returns a String, but you should be using the Trim method of the String object rather than the old VB6 method.

The cast from String to Long is invalid, do you have Option Strict On when compiling?
 
Last edited:
You can remove the Trim call and the effect will be the same:

Visual Basic:
CInt(txtH1R.Text)

Instead of CInt, I prefer to use Convert.ToInt32 because you can use it in any .NET language.

Visual Basic:
Convert.ToInt32(txtH1R.Text)
 
Back
Top