VB.Net/Language Basics/Data Type Convert
Содержание
Boolean value to Integer
Imports System
Imports System.Text
Imports System.Text.RegularExpressions
Public Class MainClass
Shared Sub Main( )
Dim blnTrue As Boolean = True
Dim blnOne As Boolean = 1
Dim blnNegOne As Boolean = -1
Dim blnFalse As Boolean = False
If blnOne = -1 Then
Console.WriteLine(blnTrue)
Console.WriteLine(blnOne.ToString)
Console.WriteLine(Convert.ToString(Convert.ToInt32(blnNegOne)))
End If
End Sub "Main
End Class
Convert Boolean value To Int16
Imports System
Imports System.Text
Imports System.Text.RegularExpressions
Public Class MainClass
Shared Sub Main( )
Dim blnTrue As Boolean = True
Dim blnOne As Boolean = 1
Dim blnNegOne As Boolean = -1
Dim blnFalse As Boolean = False
If Convert.ToInt16(blnNegOne) = 1 Then
Console.WriteLine(blnFalse)
Console.WriteLine(Convert.ToString(Convert.ToInt32(blnFalse)))
End If
End Sub "Main
End Class
Convert ToInt32 Demo
Imports System
Public Class MainClass
Shared Sub Main(ByVal args As String())
Console.WriteLine( Convert.ToInt32("12") )
End Sub
End Class
Convert ToInt32 Exception
Imports System
Public Class MainClass
Shared Sub Main(ByVal args As String())
Try
Dim numerator As Integer = Convert.ToInt32("00q")
Dim denominator As Integer = Convert.ToInt32("0")
Dim result As Integer = numerator \ denominator
Console.WriteLine (result)
Catch formattingException As FormatException
Console.WriteLine("You must enter two integers")
Catch dividingException As DivideByZeroException
Console.WriteLine(dividingException.Message)
End Try
End Sub
End Class
Explicit Conversions
Public Class MainClass
Public Shared Sub Main()
"Explicit Conversions
Dim shrShort As Short
Dim shrUInt16 As UInt16
Dim shrInt16 As Int16
Dim intInteger As Integer
Dim intUInt32 As UInt32
Dim intInt32 As Int32
Dim lngLong As Long
Dim lngInt64 As Int64
shrShort = 0
shrUInt16 = Convert.ToUInt16(shrShort)
shrInt16 = shrShort
intInteger = shrShort
intUInt32 = Convert.ToUInt32(shrShort)
intInt32 = shrShort
lngInt64 = shrShort
lngLong = Long.MaxValue
If lngLong < Short.MaxValue Then
shrShort = Convert.ToInt16(lngLong)
End If
"intInteger = CInt(lngLong)
Dim intMyShort As Integer = 200
Console.WriteLine(shrShort)
Console.WriteLine(shrUInt16)
Console.WriteLine(shrInt16)
Console.WriteLine(intInteger)
Console.WriteLine(intUInt32)
Console.WriteLine(intInt32)
Console.WriteLine(lngLong)
Console.WriteLine(lngInt64)
Console.WriteLine(Convert.ToInt32(intMyShort))
Console.WriteLine(Convert.ToDateTime("9/9/2001"))
End Sub
End Class
Implicit Conversions
Public Class MainClass
Public Shared Sub Main()
"Implicit Conversions
Dim shtShort As Short = 32767
Dim lnhLong As Long = shtShort
Console.WriteLine(shtShort)
Console.WriteLine(lnhLong)
End Sub
End Class
Int Long conversion
Imports System
Imports System.Data
Imports System.Collections
public class MainClass
Shared Sub Main()
Dim I As Integer, L As Long
I = 50
L = I
Console.WriteLine(L)
L = 50
I = CInt(L) " Explicit cast needed here
Console.WriteLine(I)
L = &H100000000
Try
I = CType(L, Integer)
Catch E As Exception
Console.WriteLine(E.Message)
End Try
Console.WriteLine(I)
End Sub
End Class
Using Val to convert string to integer
Module Module1
Sub Main()
Dim value As Integer
value = Val("1") + Val("2")
End Sub
End Module
Value convert: 32 to 16 and 16 to 32 (throw Exception)
Imports System
Imports System.Text
Imports System.Text.RegularExpressions
Public Class MainClass
Shared Sub Main( )
Dim shrShort As Short
Dim shrUInt16 As UInt16
Dim shrInt16 As Int16
Dim intInteger As Integer
Dim intUInt32 As UInt32
Dim intInt32 As Int32
Dim lngLong As Long
Dim lngInt64 As Int64
shrShort = 0
shrUInt16 = Convert.ToUInt16(shrShort)
shrInt16 = shrShort
intInteger = shrShort
intUInt32 = Convert.ToUInt32(shrShort)
intInt32 = shrShort
lngInt64 = shrShort
lngLong = lngLong.MaxValue
If lngLong > Short.MaxValue Then
shrShort = Convert.ToInt16(lngLong)
End If
intInteger = CInt(lngLong)
End Sub "Main
End Class