Funcion para controlar los valores NULOS en VB.NET

0 comentarios
Aquí os dejo una función muy util para controlar los campos de tabla o variables nulas de nuestro codigo fuente.


Shared Function NoNull(ByVal ArVar As Object, ByVal arTipo As String) As Object
       Try
            'para columnas vacias sin datos
            If ArVar.Equals(System.DBNull.Value) Then
                Select Case arTipo
                    Case "A"
                        NoNull = ""
                    Case "N"
                        NoNull = 0
                    Case "D"
                        NoNull = 0
                    Case "F"
                        NoNull = CDate("00:00:0000")
                    Case "DT"
                        NoNull = New DateTime(1, 1, 1)
                    Case Else
                        NoNull = " "
                End Select
                Exit Function
            End If

            If Len(ArVar) > 0 Then
                Select Case arTipo
                    Case "A"
                        NoNull = ArVar
                    Case "N"
                        NoNull = Val(ArVar)
                    Case "D"
                        NoNull = CDec(ArVar)
                    Case "F"
                        If ArVar = "00/00/0000" Then
                            NoNull = ""
                        Else
                            If InStr(ArVar, "/") > 0 Then
                                NoNull = ArVar
                            Else
                                NoNull = Format(ArVar, "00/00/0000")
                            End If
                        End If
                    Case Else
                        NoNull = ArVar
                End Select
            Else
                Select Case arTipo
                    Case "A"
                        NoNull = ""
                    Case "N"
                        NoNull = 0
                    Case "D"
                        NoNull = 0
                    Case "F"
                        NoNull = CDate("00:00:0000")
                    Case Else
                        NoNull = " "
                End Select
            End If
        Catch ex As Exception
            Select Case arTipo
                Case "A"
                    NoNull = ""
                Case "N"
                    NoNull = 0
                Case "D"
                    NoNull = 0
                Case "F"
                    NoNull = CDate("00:00:0000")
                Case Else
                    NoNull = " "
            End Select
        End Try
    End Function


Donde "ArVar" es el campo o variable a chequear y "arTipo" es el tipo de dato de la variable.
Devuelve un object con el valor por defecto (si fuera Nulo) o el valor del campo o variable pasada.

Por ejemplo, si teneis una variable de tipo integer y fuera nula, con esta función devolveria 0.

0 comentarios:

Publicar un comentario

Related Posts Plugin for WordPress, Blogger...
Copyright 2011 INFORMATICUCHO.
Blogger Template by Noct.