Search code examples
c#vb.netcode-conversion

How would I shorten this If... Else... statement to check the state of a Date?


I am a C# programmer but dabbling in VB.Net because everybody else in my team uses it. In the interests of professional development I would like to shrink the following If... Else... statement.

If cmd.Parameters("@whenUpdated").Equals(DBNull.Value) Then
    item.WhenUpdated = Nothing
Else
    item.WhenUpdated = cmd.Parameters("@whenUpdated").Value
End If

I appreciate examples are already available however I can not get it working for this specific case.

Cheers, Ian.


Solution

  • Use If as a function rather than a statement:

    item.WhenUpdated = If(cmd.Parameters("@whenUpdated").Equals(DBNull.Value), cmd.Parameters("@whenUpdated").Value, Nothing)