none
how to convert from string to integer RRS feed

  • Question

  • I have a strg that is 

      strg=textbox1.text

    and I want to use this string to add it to an integer

    num+=strg

    so any solution

    Wednesday, May 15, 2013 3:34 PM

Answers

  • Dim n As Integer
    If Integer.TryParse(TextBox1.Text, n) Then
        num += n
    Else
        MsgBox("Please type a whole number in TextBox1.")
    End If

    --
    Andrew

    Wednesday, May 15, 2013 7:36 PM
  • Dim n As Integer
    If Integer.TryParse(TextBox1.Text, n) Then
        num += n
    Else
        MsgBox("Please type a whole number in TextBox1.")
    End If

    --
    Andrew


    I offer a BMW and you offer a Mini Cooper ... now you watch ... he'll grab the Mini Cooper!

    Please call me Frank :)

    Wednesday, May 15, 2013 7:43 PM
  • I have a strg that is 

      strg=textbox1.text

    and I want to use this string to add it to an integer

    num+=strg

    so any solution

    This, of course, assumes the user is only typing integers and possibly a single decimal into TextBox1.

    Dim Test As Decimal = 255.543
    
    Test += CDec(TextBox1.Text)
    TextBox2.Text = Test.ToString



    You've taught me everything I know but not everything you know.


    Thursday, May 16, 2013 4:08 AM

All replies

  • I have a strg that is 

      strg=textbox1.text

    and I want to use this string to add it to an integer

    num+=strg

    so any solution

    If you're sure it can be converted, then simply CInt(strg), but are you sure?

    If not, there's a better way...


    Please call me Frank :)

    Wednesday, May 15, 2013 3:40 PM
  • To be more specific, I'd suggest that you use the TextBox's .Validating event. In that event, declare a variable (like tempInt) as integer then use Integer.TryParse as a test. That returns a boolean indicating whether it was successful at the conversion. If it returns false, you might then use an ErrorProvider to show the user that it failed and set e.Cancel = True to 'lock them in' to force them to change it to something which can be converted to an integer.

    Please call me Frank :)


    Wednesday, May 15, 2013 3:47 PM
  • I'll put together an example if you want me to?

    Please call me Frank :)

    Wednesday, May 15, 2013 4:02 PM
  • I put together something a few months ago for someone regarding validation:

    http://social.msdn.microsoft.com/Forums/en-US/vbgeneral/thread/f0470c0b-ca37-4053-9f0d-8089b0c4f7fa#4bf3e054-faa5-4e21-a0fc-1cc74b267952

    If that doesn't open to the correct spot, look for the thread by me dated Sunday, March 31, 3:32 PM.

    I hope that helps. :)


    Please call me Frank :)

    Wednesday, May 15, 2013 4:29 PM
  • strg = num & TextBox1.Text

    If all you want is a string then this will implicitly convert num into a string and add the string from the textbox to it.  Keep in mind that if you want only numeric values, then this will not work if the textbox contains any non-numeric characters.

    If you really need a numeric string, then you first need to convert the text to a number with TryParse:

    Dim num As Integer
    Integer.TryParse(TextBox1.Text, num)

    Then you can use the code shown above: strg = num & TextBox1.Text


    Solitaire

    Wednesday, May 15, 2013 5:23 PM
  • strg = num & TextBox1.Text

    If all you want is a string then this will implicitly convert num into a string and add the string from the textbox to it.  Keep in mind that if you want only numeric values, then this will not work if the textbox contains any non-numeric characters.

    If you really need a numeric string, then you first need to convert the text to a number with TryParse:

    Dim num As Integer
    Integer.TryParse(TextBox1.Text, num)

    Then you can use the code shown above: strg = num & TextBox1.Text


    Solitaire

    If num = 1 and TextBox1.Text = "1" then num & TextBox1.Text = "11"

    This concatenates the string representation of the number to the text.  The OP wants to add the numerical representation of the text to the number:

    "I want to use this string to add it to an integer

    num+=strg"

    Wednesday, May 15, 2013 5:57 PM
  • If you want to add a string number to an integer, and concatenate the result back to a string, then:

    Dim num1, num2 As Integer, strg1, strg2 As String
    Integer.TryParse(strg1, num1)
    Integer.TryParse(TextBox1, num2)
    strg2 = num1.ToString & num2.ToString


    Solitaire

    Wednesday, May 15, 2013 6:36 PM
  • If you want to add a string number to an integer, and concatenate the result back to a string, then:

    Dim num1, num2 As Integer, strg1, strg2 As String
    Integer.TryParse(strg1, num1)
    Integer.TryParse(TextBox1, num2)
    strg2 = num1.ToString & num2.ToString


    Solitaire

    That won't throw an exception if Integer.TryParse fails, but by definition, the integer which it cast it to will have a value of zero, so if it fails, the user needs to be notified that it did; not simply show the wrong result and continue on as though nothing is wrong.


    Please call me Frank :)

    Wednesday, May 15, 2013 6:41 PM
  • Dim n As Integer
    If Integer.TryParse(TextBox1.Text, n) Then
        num += n
    Else
        MsgBox("Please type a whole number in TextBox1.")
    End If

    --
    Andrew

    Wednesday, May 15, 2013 7:36 PM
  • Dim n As Integer
    If Integer.TryParse(TextBox1.Text, n) Then
        num += n
    Else
        MsgBox("Please type a whole number in TextBox1.")
    End If

    --
    Andrew


    I offer a BMW and you offer a Mini Cooper ... now you watch ... he'll grab the Mini Cooper!

    Please call me Frank :)

    Wednesday, May 15, 2013 7:43 PM
  • I offer a BMW and you offer a Mini Cooper ... now you watch ... he'll grab the Mini Cooper!

    Ha ha :) A Mini Cooper nowadays would at least have used MessageBox.Show(...). An ErrorProvider gives a much more professional feel.

    And congratulations for your daughter's marriage :)

    --
    Andrew

    Wednesday, May 15, 2013 7:57 PM
  • And congratulations for your daughter's marriage :)

    Yea, now she's someone else's problem. ;-)

    Teasing of course - and thanks. :)

    Did I tell you there were actually two (2) marriage ceremonies, not one? One week after they were 'officially married' here, we all went 650 miles away (where she was born and where I grew up) to get married again for the sake of my parents. My dad turned 94 about three weeks ago so ... you can understand why.

    I'm glad they agreed to doing that -- and I'm even more glad all that's done now!


    Please call me Frank :)

    Wednesday, May 15, 2013 8:02 PM
  • Here is a quick solution, but be careful: If the expression is not numerical it will be considered zero.

    num += Val(strg)

    Wednesday, May 15, 2013 8:06 PM
  • Here is a quick solution, but be careful: If the expression is not numerical it will be considered zero.

    num += Val(strg)

    The Val method is, in my opinion, something that should have been thrown away from dotNET a long time ago.

    It's very indiscriminate - and this is a good example. You cannot tell that the input was an integer, but moreover, you can't tell if the input was just part of some text like "123 Main Street".

    I don't mean to denigrate - but I think that's a horrible idea. This is, of course, only my opinion.


    Please call me Frank :)

    Wednesday, May 15, 2013 8:20 PM
  • I have a strg that is 

      strg=textbox1.text

    and I want to use this string to add it to an integer

    num+=strg

    so any solution

    This, of course, assumes the user is only typing integers and possibly a single decimal into TextBox1.

    Dim Test As Decimal = 255.543
    
    Test += CDec(TextBox1.Text)
    TextBox2.Text = Test.ToString



    You've taught me everything I know but not everything you know.


    Thursday, May 16, 2013 4:08 AM