Plateforme Level Extreme
Abonnement
Profil corporatif
Produits & Services
Support
Légal
English
Proper way of moving from Double to Integer
Message
De
09/02/2012 03:34:58
 
 
À
08/02/2012 15:29:03
Information générale
Forum:
ASP.NET
Catégorie:
Autre
Versions des environnements
Environment:
VB 9.0
OS:
Windows 7
Network:
Windows 2003 Server
Database:
MS SQL Server
Application:
Web
Divers
Thread ID:
01534895
Message ID:
01534950
Vues:
42
>>I think it's always better to be explicit. In your case you will reply on implicit cast.
>
>Here is the method:
>
>
>        ' Return a numeric value
>        ' If it is corrupted, we redirect to the main page
>        ' expC1 HTLM form variable
>        Public Function GetProcessNumeric(ByVal tcField As String) As Integer
>            Dim lcPrimaryKey As String = ""
>            Dim lnPrimaryKey As Integer = 0
>            Dim lnPrimaryKeyTemp As Double = 0
>
>            ' If the key exists
>            If oProcess.IsProcess(tcField) Then
>                
>                ' Get the value
>                lcPrimaryKey = oProcess.GetProcess(tcField)
>
>                ' If we have a value
>                If lcPrimaryKey.Length > 0 Then
>
>                    ' If we have digit only
>                    If oProcess.oApp.IsFilter09(lcPrimaryKey) Then
>
>                        ' If we have less than 11 characters
>                        If lcPrimaryKey.Length < 11 Then
>                            lnPrimaryKeyTemp = Val(lcPrimaryKey)
>
>                            ' If this is under or equal 2147483647
>                            If lnPrimaryKeyTemp <= 2147483647 Then
>                                lnPrimaryKey = lnPrimaryKeyTemp
>                            Else
>                                oProcess.RedirectMain(oProcess.cHttp)
>                                Return 0
>                            End If
>
>                        Else
>                            oProcess.RedirectMain(oProcess.cHttp)
>                            Return 0
>                        End If
>
>                    Else
>                        oProcess.RedirectMain(oProcess.cHttp)
>                        Return 0
>                    End If
>
>                Else
>                    oProcess.RedirectMain(oProcess.cHttp)
>                    Return 0
>                End If
>
>            End If
>
>            Return lnPrimaryKey
>        End Function
>
>
>I also like it like that as I can negotiate with it in any way I want and follows the path of the framework to not use too much of in-dept functionality of the .NET Framework as it makes it much difficult to migrate to another environment after.

As Naomi suggests you should use TryParse. Safer, simpler, shorter, faster:
lcPrimaryKey = oProcess.GetProcess(tcField):
Dim lnPrimaryKey As Integer = 0
If Not Integer.TryParse(lcPrimaryKey, lnPrimaryKey) Then
     oProcess.RedirectMain(oProcess.cHttp)
End If
Return lnPrimaryKey
Nothing else required....
Précédent
Suivant
Répondre
Fil
Voir

Click here to load this message in the networking platform