Type and Type Exception

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

What happens when a user wants to highlight ""as Item(DurationColumn)a whole? I get an exception. Any clean solution to avoid this and install 0for ""?

+3
source share
4 answers

Use Int32.TryParse :

Dim number as Integer
If Not Int32.TryParse(DurationColumn, number) Then number = 0
return number

This handles the case "", as well as any other invalid value (i.e. not a number) that the user can enter.

+3
source

Take a look TryCastand Int32.TryParse.

0
source

IsNumeric

    number = If(IsNumeric(DurationColumn), CType(DurationColumn, Integer), 0)
0

return , .

    Dim sRet As String = Item(DurationColumn)
    If Not String.IsNullOrEmpty(sRet) Then Convert.ToInt32(sRet)
0

Source: https://habr.com/ru/post/1794768/


All Articles