Results 1 to 11 of 11

Thread: adding up 2 strings

  1. #1

    Thread Starter
    Member
    Join Date
    Jul 2005
    Posts
    35

    adding up 2 strings

    i have strA and strB
    strA = 1
    strB = 1
    i try text1.text = strA + strB

    but it doesn't work the result is 11 not 2 why?

  2. #2
    I'm about to be a PowerPoster! Hack's Avatar
    Join Date
    Aug 2001
    Location
    Searching for mendhak
    Posts
    58,333

    Re: adding up 2 strings

    Quote Originally Posted by vbcooler
    i have strA and strB
    strA = 1
    strB = 1
    i try text1.text = strA + strB

    but it doesn't work the result is 11 not 2 why?
    VB Code:
    1. Text1.Text = Val(strA) + Val(strB)
    Also, if you are going to be adding these together, they should be declared as integers
    VB Code:
    1. Dim intA As Integer
    2. Dim intB As Integer
    Then, you wouldn't need to Val them.

  3. #3

    Thread Starter
    Member
    Join Date
    Jul 2005
    Posts
    35

    Re: adding up 2 strings

    Quote Originally Posted by Hack
    VB Code:
    1. Text1.Text = Val(strA) + Val(strB)
    Also, if you are going to be adding these together, they should be declared as integers
    VB Code:
    1. Dim intA As Integer
    2. Dim intB As Integer
    Then, you wouldn't need to Val them.
    THEY CAN'T BE DECLER AS AN INTEGER SINCE SOME OF THE NUMBERS COULD BE
    1.3
    5.5
    ETC..

  4. #4
    Raging swede Atheist's Avatar
    Join Date
    Aug 2005
    Location
    Sweden
    Posts
    8,018

    Re: adding up 2 strings

    Declare them as Long, single or double then
    Rate posts that helped you. I do not reply to PM's with coding questions.
    How to Get Your Questions Answered
    Current project: tunaOS
    Me on.. BitBucket, Google Code, Github (pretty empty)

  5. #5
    I'm about to be a PowerPoster! Hack's Avatar
    Join Date
    Aug 2001
    Location
    Searching for mendhak
    Posts
    58,333

    Re: adding up 2 strings

    Quote Originally Posted by Atheist
    Declare them as Long, single or double then
    A Single or a Double would return decimals. A Long will round to an even number.

  6. #6
    Raging swede Atheist's Avatar
    Join Date
    Aug 2005
    Location
    Sweden
    Posts
    8,018

    Re: adding up 2 strings

    Quote Originally Posted by Hack
    A Single or a Double would return decimals. A Long will round to an even number.
    oh yes youre right
    Rate posts that helped you. I do not reply to PM's with coding questions.
    How to Get Your Questions Answered
    Current project: tunaOS
    Me on.. BitBucket, Google Code, Github (pretty empty)

  7. #7
    Addicted Member
    Join Date
    Oct 2005
    Posts
    149

    Re: adding up 2 strings

    Long will truncate to an integer, not round to an even number!

    Right?

  8. #8
    PowerPoster
    Join Date
    Oct 2002
    Location
    British Columbia
    Posts
    9,758

    Re: adding up 2 strings

    Wrong. If you have this statement

    Long = Double + Double

    The Double + Double would be rounded to the nearest integer.

  9. #9
    Addicted Member
    Join Date
    Oct 2005
    Posts
    149

    Re: adding up 2 strings

    Not all "Wrong"...

    Long does indeed round a number (I thought it truncated it ), but it doesn't necessarily round to an EVEN number (1.3 gets rounded to 1 - an ODD number).

    Now I think we're straight!

  10. #10
    I'm about to be a PowerPoster! Hack's Avatar
    Join Date
    Aug 2001
    Location
    Searching for mendhak
    Posts
    58,333

    Re: adding up 2 strings

    Quote Originally Posted by Set me As newtype
    Not all "Wrong"...

    Long does indeed round a number (I thought it truncated it ), but it doesn't necessarily round to an EVEN number (1.3 gets rounded to 1 - an ODD number).

    Now I think we're straight!
    Ok, I misspoke. Long doesn't round to an even number it rounds to a whole number (which is actually what I meant but not what I typed.)

  11. #11
    INXSIVE Bruce Fox's Avatar
    Join Date
    Sep 2001
    Location
    Melbourne, Australia
    Posts
    7,429

    Re: adding up 2 strings

    Quote Originally Posted by vbcooler
    i have strA and strB
    strA = 1
    strB = 1
    i try text1.text = strA + strB

    but it doesn't work the result is 11 not 2 why?
    ..... the result is 11 not 2 why?[/
    The '+' serves to join (concatenate) Strings. However, If you want to concatenate Strings, then
    the appropriate operator is the '&' (ampersand), noting the '+' will do the same. Hence, you got '11' and not a mathematecal add equaling 2.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width