Jim Rogers

Lives in Baton Rouge, LA, with two dogs, one cat, and one lovely wife. I'm a lead developer for GCR Incorporated.

Katrin and Jim

Month List

ViewState Property Problems in VB

by Jim Nov 17, 2009 3:50 PM

Quick, what’s wrong with this property? It doesn’t return the value I was expecting.

Public Property ContractIsDeletable() As Boolean
    Get
        If ViewState("ContractDeletable") = Nothing Then
            Return True
        Else
            Return ViewState("ContractDeletable")
        End If
    End Get
    Set(ByVal value As Boolean)
        ViewState("ContractDeletable") = value
    End Set
End Property

Perfectly normal implementation of a ViewState-backed property, right? The following code will print both messages:

ViewState!BooleanCheck = False
If ViewState!BooleanCheck = Nothing Then
    Debug.WriteLine("false is not nothing!!")
End If

ViewState!IntegerCheck = 0
If ViewState!IntegerCheck = Nothing Then
    Debug.WriteLine("zero is not nothing!!")
End If

You can (and I apparently did) get away with the construct at top, if the default value returned in the case of a Nothing happens to be zero, or false. But that evaluation is incorrect. The correct way to make this property is:

    Public Property ContractIsDeletable() As Boolean
        Get
            If IsNothing(ViewState("ContractDeletable")) Then
                Return True
            Else
                Return ViewState("ContractDeletable")
            End If
        End Get
        Set(ByVal value As Boolean)
            ViewState("ContractDeletable") = value
        End Set
    End Property

Because bools and ints aren’t nullable types, the = comparison doesn’t work property. IsNothing does the trick.

Tags:

Code

Windows Explorer Shortcut

by Jim Nov 17, 2009 6:25 AM

To put the focus in the file list, from anywhere else in windows explorer: CTRL+TAB. Once you’re there, spacebar selects the first item.

I’ve always been a fan of keyboard shortcuts, and staying away from the mouse. At work we have "mouse free Friday" to encourage people to learn to use the keyboard whenever possible.

I use Windows Explorer all the time, and have been looking for the above shortcut for years – I only discovered it by chance. I’ve never seen it documented anywhere, but here’s a great list of Windows 7 shortcuts, including many for Windows Explorer.

Tags:

Code