Skip to content

Instantly share code, notes, and snippets.

@jimschubert
Created March 31, 2012 14:24
Show Gist options
  • Save jimschubert/2265475 to your computer and use it in GitHub Desktop.
Save jimschubert/2265475 to your computer and use it in GitHub Desktop.
Trying out CInt in response to a site contact email

Description

I was contacted through my website with a question about VB.NET and generated IL. Because I'm not familiar with VB.NET, and it's been a very long time since I used VB 5, I thought I would throw together a small console app to see how CInt works. That's what this is.

Public Class Application
Public Shared Sub Main()
System.Console.WriteLine("Trying out CInt")
Try
' CInt Float
Dim FLOAT_VAL As Single = 99.09
Dim i As Integer = CInt(FLOAT_VAL)
Console.WriteLine("CInt(FLOAT_VAL): {0}", i)
' CInt Double
Dim DOUBLE_VAL As Double = 999.99
i = CInt(DOUBLE_VAL)
Console.WriteLine("CInt(DOUBLE_VAL): {0}", i)
' CInt Double from string
' The following will throw "Conversion from string "0.989" to type 'Integer' is not valid."
Dim STRING_VAL As String = "0.989"
i = CInt(STRING_VAL)
Console.WriteLine("CInt(STRING_VAL): {0}", i)
' Convert.ToInt32
' Uncomment the following code for "Input string was not in the correct format" exception
' i = Convert.ToInt32(STRING_VAL)
' Console.WriteLine("Convert.ToInt32(STRING_VAL): {0}", i)
Catch ex As System.Exception
System.Windows.Forms.MessageBox.Show(ex.Message)
end Try
End Sub
End Class
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment