Integer.Parse() 関数は値を直接変換します。実際の値をテストしておらず、パラメーターが整数でない場合はエラーが発生します。
したがって、次のように integer.TryParse を使用できます。if Integer.TryParse(txtkey.Text,key) then '整数の変換に成功し、値がキーに格納されます。else 'txtkey.Text に有効な整数がありません。したがって、キーには 0 が格納されます。終了する場合
すべてのデータ型変換関数を持つクラスを作成し、必要に応じてそのクラスから関数を呼び出すことをお勧めします。コードで使用するために次のクラスを作成しました
--------------------------私のコードのクラス-------------------- ----------------
Public Class NullValue
'''''''''''''''''' 1. BYTE type convertion
Public Shared Function getByte(ByVal mValue As String) As Byte
Dim mNumber As Byte
If Byte.TryParse(mValue, mNumber) Then Return mNumber Else Return 0
End Function
'''''''''''''''''' 2. Int16/Short Integer type convertion
Public Shared Function getInt16(ByVal mValue As String) As Int16
Dim mNumber As Int16
If Int16.TryParse(mValue, mNumber) Then Return mNumber Else Return 0
End Function
'''''''''''''''''' 3. Int32/Integer type convertion
Public Shared Function getInteger(ByVal mValue As String) As Int32
Dim mNumber As Int32
If Int32.TryParse(mValue, mNumber) Then Return mNumber Else Return 0
End Function
'''''''''''''''''' 4. Int64/Long Integer type convertion
Public Shared Function getInt64(ByVal mValue As String) As Int64
Dim mNumber As Int64
If Int64.TryParse(mValue, mNumber) Then Return mNumber Else Return 0
End Function
'''''''''''''''''' 5. Single type convertion
Public Shared Function getSingle(ByVal mValue As String) As Single
Dim mNumber As Single
If Single.TryParse(mValue, mNumber) Then Return mNumber Else Return 0
End Function
'''''''''''''''''' 6. Double type convertion
Public Shared Function getDouble(ByVal mValue As String) As Double
Dim mNumber As Double
If Double.TryParse(mValue, mNumber) Then Return mNumber Else Return 0
End Function
'''''''''''''''''' 8. Integer(Int32 type convertion
Public Shared Function getInt32(ByVal mValue As String) As Integer
Dim mNumber As Int32
If Int32.TryParse(mValue, mNumber) Then Return mNumber Else Return 0
End Function
'''''''''''''''''' 9. Short Date type convertion to String '''in Regional format
Public Shared Function getDateString(ByVal mValue As String) As String
Dim mDate As Date, strDate As String = ""
If Date.TryParse(mValue, mDate) Then
strDate = Format(mDate, "dd/MM/yyyy")
Else
strDate = ""
End If
Return strDate
End Function
'''''''''''''''''' 10. Short Date type convertion
Public Shared Function getDate(ByVal mValue As String) As Date
Dim mDate As Date
Try
Date.TryParse(mValue, mDate)
Catch ex As Exception
mDate = Date.MinValue
End Try
Return mDate
End Function
'''''''''''''''''' 11. Short Date type convertion
Public Shared Function getDateTime(ByVal mValue As String) As DateTime
Dim mDate As DateTime
Try
Date.TryParse(mValue, mDate)
Catch ex As Exception
mDate = Date.MinValue
End Try
Return mDate
End Function
'''''''''''''''''' 12. Short Date type convertion
Public Shared Function getBoolean(ByVal mValue As String) As Boolean
Dim mBool As Boolean
If Boolean.TryParse(mValue, mBool) Then Return mBool Else Return False
End Function
'''''''''''''''''' 11. Time Convertion
Public Shared Function getTime(ByVal mValue As String) As String
Dim mDate As DateTime, mTime As String = ""
Try
mDate = Convert.ToDateTime(mValue)
If mDate.Year > 1950 Then mTime = mDate.ToShortTimeString
Catch ex As Exception
End Try
Return mTime
End Function
End Class