Jump to content
Xtreme .Net Talk

Recommended Posts

Posted

I have 9 Textboxes which hold numbers.

I want to add the 9 numbers together and put the total in a 10th Testbox

 

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 ?

Posted

Ok, everything works now after I removed the outside Trim.

 

CInt(Trim(txtH1R.Text))

rather then:

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 ?

  • *Experts*
Posted (edited)

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?

Edited by mutant
Posted

You can remove the Trim call and the effect will be the same:

 

CInt(txtH1R.Text)

 

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

 

Convert.ToInt32(txtH1R.Text)

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...