tags:

views:

436

answers:

4

I have a nullable property, and I want to return a null value. How do I do that in VB.NET ?

Currently I use this solution, but I think there might be a better way.

    Public Shared ReadOnly Property rubrique_id() As Nullable(Of Integer)
        Get
            If Current.Request.QueryString("rid") <> "" Then
                Return CInt(Current.Request.QueryString("rid"))
            Else
                Return (New Nullable(Of Integer)).Value
            End If
        End Get
    End Property
+5  A: 

Are you looking for the keyword "Nothing"?

Jon
Hum, actually yes. I see that actually Nothing is equivalent to C# null, while I thought it was only used to see if an object was instantiated.
cosmo0
+2  A: 

Yes, it's Nothing in VB.NET, or null in C#.

The Nullable generic datatype give the compiler the possibility to assign a "Nothing" (or null" value to a value type. Without explicitally writing it, you can't do it.

Nullable Types in C#

volothamp
+1  A: 
Public Shared ReadOnly Property rubrique_id() As Nullable(Of Integer)
    Get
        If Current.Request.QueryString("rid") <> "" Then
            Return CInt(Current.Request.QueryString("rid"))
        Else
            Return Nothing
        End If
    End Get
End Property
gregmac
A: 

Or this is the way i use, to be honest ReSharper has taught me :)

finder.Advisor = ucEstateFinder.Advisor == "-1" ? (long?)null : long.Parse(ucEstateFinder.Advisor);

On the assigning above if i directly assign null to finder.Advisor*(long?)* there would be no problem. But if i try to use if clause i need to cast it like that (long?)null.

Barbaros Alp