How do I set Nothing to a DateTime variable?

2

I have a function that gets a string and is converted to date time, I made a check for when empty string , return Nothing . But when it exits the function it is returning the date in this format #12:00:00 AM# , what I want is that it only comes with Nothing or something of the type.

Here is my variable that returns:

Dim data as DateTime? = RetornaDataFormatada(strValor)

And here my reduced function:

Public Function RetornaDataFormatada(ByVal strValor As String) As DateTime

        If strValor = "" Then

            Return Nothing

        End If

        Return Nothing

End Function
    
asked by anonymous 13.07.2015 / 18:31

1 answer

2

For DateTime is not possible, this is a value type that does not accept null values. However it is possible to return Nothing to a type DateTime? . It seems to me that there would be no problem for you since you will use it in a variable of this type.

Note that the query makes all the difference, with and without are different types, with different semantics.

In this way just change the type of returns, like this:

Public Function RetornaDataFormatada(ByVal strValor As String) As DateTime?
    If strValor = "" Then
        Return Nothing
    End If
    Return Nothing
End Function

Of course this function does nothing useful except to return Noting in all situations. This is not important for the problem of the questions but the tip is if you did not notice.

    
13.07.2015 / 18:36