none
vb.net CInt

    Question

  • Hi everyone.

    Please could someone help me with this.

    I have read and watch so many videos and threads, but cant work this out, I am trying to add value to a variable called ( Value1 ) with no luck.

      Public Sub Values()
            Dim TextInt As String = ToolStripComboBox1.Text               'I know this part works         
            Dim NumberInt As Integer
            NumberInt = CInt(TextInt)
            Value1 = NumberInt        
        End Sub

    Really sorry if this is so simple but I am new to programming.

    I also Tried Covert.ToInt32 that doesn't work either


    Thursday, March 8, 2018 1:56 PM

Answers

  • NOTE: the function will always return a valid Integer. If the string passed to it cannot be changed to an Integer the function will return 0 (zero). The advantage in using a Function is that anywhere you want to change a string to an Integer then the Function can be used. (other numeric types are available too)

    Function GetInteger(s As String) As Integer
     Dim v As Integer = 0
     If Integer.TryParse(s, v) Then Return v
     Return 0
    End Function


    Regards Les, Livingston, Scotland

    You could also return a nullable integer to handle string conversion errors without having a "default" value:

    Function GetInteger(s As String) As Integer?
        Dim v As Integer = 0
        If Integer.TryParse(s, v) Then Return v
        Return Nothing
    End Function
    


    Reed Kimble - "When you do things right, people won't be sure you've done anything at all"

    • Marked as answer by Justin Shears Thursday, March 8, 2018 4:14 PM
    Thursday, March 8, 2018 3:41 PM
    Moderator
  • Hi everyone.

    Please could someone help me with this.

    I have read and watch so many videos and threads, but cant work this out, I am trying to add value to a variable called ( Value1 ) with no luck.

      Public Sub Values()
            Dim TextInt As String = ToolStripComboBox1.Text               'I know this part works         
            Dim NumberInt As Integer 'WHAT is in TextInt here
            NumberInt = CInt(TextInt)
            Value1 = NumberInt        
        End Sub

    Really sorry if this is so simple but I am new to programming.

    I also Tried Covert.ToInt32 that doesn't work either


    What is in TextInt after the assignment executes?


    "Those who use Application.DoEvents() have no idea what it does and those who know what it does never use it" - MSDN User JohnWein

    • Marked as answer by Justin Shears Thursday, March 8, 2018 4:15 PM
    Thursday, March 8, 2018 2:14 PM
  • Cint and all number converter methods work only with text that can be seen as numerals. 

    Be aware that it can also be numbers with for instance exponents. But for instance characters like spaces are not allowed. 

    In past there was in VB the Var function. But I think there is less more insecure to use than that. 

    You can test for numeric with the IsNumeric function from VB or with the TryParse from .Net.


    Success
    Cor

    • Marked as answer by Justin Shears Thursday, March 8, 2018 4:15 PM
    Thursday, March 8, 2018 2:34 PM
  • What is the exception (error) and what is the value of TextInt?

    Paul ~~~~ Microsoft MVP (Visual Basic)

    • Marked as answer by Justin Shears Thursday, March 8, 2018 4:15 PM
    Thursday, March 8, 2018 2:37 PM
  • Thanks for the replies

    System.InvalidCastException: 'Conversion from string "" to type 'Integer' is not valid.'

    TextInt Value is "48" it is always going to be a number, 

    • Marked as answer by Justin Shears Thursday, March 8, 2018 4:15 PM
    Thursday, March 8, 2018 2:47 PM
  • Thanks for the replies

    System.InvalidCastException: 'Conversion from string "" to type 'Integer' is not valid.'

    TextInt Value is "48" it is always going to be a number, 

    Hi

    As suggested above, one of the ways to deal with text representations of a number is to use Integer.TryParse.

    Here is a Function that encapsulates that method. You use if by for example:   Dim value1 as Integer = GetInteger(TextInt), and then the variable value1 would contain the numerical number (Integer) NOTE: the function will always return a valid Integer. If the string passed to it cannot be changed to an Integer the function will return 0 (zero). The advantage in using a Function is that anywhere you want to change a string to an Integer then the Function can be used. (other numeric types are available too)

    Function GetInteger(s As String) As Integer
     Dim v As Integer = 0
     If Integer.TryParse(s, v) Then Return v
     Return 0
    End Function


    Regards Les, Livingston, Scotland

    Thursday, March 8, 2018 3:20 PM

All replies

  • Hi everyone.

    Please could someone help me with this.

    I have read and watch so many videos and threads, but cant work this out, I am trying to add value to a variable called ( Value1 ) with no luck.

      Public Sub Values()
            Dim TextInt As String = ToolStripComboBox1.Text               'I know this part works         
            Dim NumberInt As Integer 'WHAT is in TextInt here
            NumberInt = CInt(TextInt)
            Value1 = NumberInt        
        End Sub

    Really sorry if this is so simple but I am new to programming.

    I also Tried Covert.ToInt32 that doesn't work either


    What is in TextInt after the assignment executes?


    "Those who use Application.DoEvents() have no idea what it does and those who know what it does never use it" - MSDN User JohnWein

    • Marked as answer by Justin Shears Thursday, March 8, 2018 4:15 PM
    Thursday, March 8, 2018 2:14 PM
  • Cint and all number converter methods work only with text that can be seen as numerals. 

    Be aware that it can also be numbers with for instance exponents. But for instance characters like spaces are not allowed. 

    In past there was in VB the Var function. But I think there is less more insecure to use than that. 

    You can test for numeric with the IsNumeric function from VB or with the TryParse from .Net.


    Success
    Cor

    • Marked as answer by Justin Shears Thursday, March 8, 2018 4:15 PM
    Thursday, March 8, 2018 2:34 PM
  • What is the exception (error) and what is the value of TextInt?

    Paul ~~~~ Microsoft MVP (Visual Basic)

    • Marked as answer by Justin Shears Thursday, March 8, 2018 4:15 PM
    Thursday, March 8, 2018 2:37 PM
  • Thanks for the replies

    System.InvalidCastException: 'Conversion from string "" to type 'Integer' is not valid.'

    TextInt Value is "48" it is always going to be a number, 

    • Marked as answer by Justin Shears Thursday, March 8, 2018 4:15 PM
    Thursday, March 8, 2018 2:47 PM
  • Thanks for the replies

    System.InvalidCastException: 'Conversion from string "" to type 'Integer' is not valid.'

    TextInt Value is "48" it is always going to be a number, 

    Hi

    As suggested above, one of the ways to deal with text representations of a number is to use Integer.TryParse.

    Here is a Function that encapsulates that method. You use if by for example:   Dim value1 as Integer = GetInteger(TextInt), and then the variable value1 would contain the numerical number (Integer) NOTE: the function will always return a valid Integer. If the string passed to it cannot be changed to an Integer the function will return 0 (zero). The advantage in using a Function is that anywhere you want to change a string to an Integer then the Function can be used. (other numeric types are available too)

    Function GetInteger(s As String) As Integer
     Dim v As Integer = 0
     If Integer.TryParse(s, v) Then Return v
     Return 0
    End Function


    Regards Les, Livingston, Scotland

    Thursday, March 8, 2018 3:20 PM
  • Find this programming so hard to learn online, as I'm just starting out, and answers most of the time go way above my head, think I need to forget this problem, and find a book to read if there is one.
    Thursday, March 8, 2018 3:35 PM
  • NOTE: the function will always return a valid Integer. If the string passed to it cannot be changed to an Integer the function will return 0 (zero). The advantage in using a Function is that anywhere you want to change a string to an Integer then the Function can be used. (other numeric types are available too)

    Function GetInteger(s As String) As Integer
     Dim v As Integer = 0
     If Integer.TryParse(s, v) Then Return v
     Return 0
    End Function


    Regards Les, Livingston, Scotland

    You could also return a nullable integer to handle string conversion errors without having a "default" value:

    Function GetInteger(s As String) As Integer?
        Dim v As Integer = 0
        If Integer.TryParse(s, v) Then Return v
        Return Nothing
    End Function
    


    Reed Kimble - "When you do things right, people won't be sure you've done anything at all"

    • Marked as answer by Justin Shears Thursday, March 8, 2018 4:14 PM
    Thursday, March 8, 2018 3:41 PM
    Moderator
  • Thanks for the replies

    System.InvalidCastException: 'Conversion from string "" to type 'Integer' is not valid.'

    TextInt Value is "48" it is always going to be a number, 


    If TextInt is always going to be an integer then there should not be the error from the code you posted.

    "Those who use Application.DoEvents() have no idea what it does and those who know what it does never use it" - MSDN User JohnWein

    Thursday, March 8, 2018 5:06 PM