Public Property Duration() As Integer
Get
Try
Return CType(Item(DurationColumn), Integer)
Catch e As Global.System.InvalidCastException
Throw New Global.System.Data.StrongTypingException("The value for column 'Duration' in table 'T_MembershipSale' is DBNull.", e)
End Try
End Get
Set(ByVal value As Integer)
Item(DurationColumn) = value
End Set
End Property
当用户想要将 ""
as Item(DurationColumn)
分配给整数时会发生什么?我得到一个异常(exception)。有什么干净的解决方案可以避免这种情况并为 ""
设置 0
?
最佳答案
使用Int32.TryParse :
Dim number as Integer
If Not Int32.TryParse(DurationColumn, number) Then number = 0
return number
这会处理“”以及用户可能输入的任何其他无效值(即非数字)的情况。
关于.net - CType 和类型转换异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5120445/