0

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.

Ian Roke
  • 1,774
  • 1
  • 19
  • 27
  • 1
    While not very obvious, this is almost a duplicate of http://stackoverflow.com/questions/576431/is-there-a-conditional-ternary-operator-in-vb-net – Jørn Schou-Rode Apr 02 '10 at 22:42
  • 1
    Isn't that the wrong way around? – Mark Byers Apr 02 '10 at 22:42
  • Thank you I saw that question but didn't understand how to apply it in this situation. I see you have voted to close. That's fine I have the answer I need. – Ian Roke Apr 02 '10 at 22:43
  • @Mark it is I am working very late I apologise I will turn it round. – Ian Roke Apr 02 '10 at 22:44
  • @Ian: I am sorry about that. The "I am a C# programmer" preface of your question made me conclude that you probably knew about the `?:` syntax and wanted something similar. – Jørn Schou-Rode Apr 02 '10 at 22:45
  • Thanks @Cresults I am finding it very interesting and not dissimilar to VBA! :-D – Ian Roke Apr 02 '10 at 22:46
  • @Jørn, sure I know how to do it in C# but for some reason it wasn't directly copying over to VB.Net. As I have said I am working very late I need to sleep... or get coffee! 8-) – Ian Roke Apr 02 '10 at 22:48

5 Answers5

6

Use If as a function rather than a statement:

item.WhenUpdated = If(cmd.Parameters("@whenUpdated").Equals(DBNull.Value), cmd.Parameters("@whenUpdated").Value, Nothing)
Dan Story
  • 9,985
  • 1
  • 23
  • 27
  • You still have to put this twice `cmd.Parameters("@whenUpdated").Value`. Why not write a DBNull2Nothing function? Make it much shorter & self documenting? – MarkJ Apr 03 '10 at 17:43
3

Similar to the ternary operator in C#, VB has the IIF function.

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

If the first argument (the boolean expression) evaluates to true, then the second argument is returned from the function. If the first argument is false, then the third argument is returned.

womp
  • 115,835
  • 26
  • 236
  • 269
2
item.WhenUpdated = Nothing
If cmd.Parameters("@whenUpdated").Equals(DBNull.Value) Then
    item.WhenUpdated = cmd.Parameters("@whenUpdated").Value
End If

Only 1 line, but still shorter.

The IF function is definitely the shortest, but not the most readable.

Dustin Laine
  • 37,935
  • 10
  • 86
  • 125
0

One less line

item.WhenUpdated = Nothing
If cmd.Parameters("@whenUpdated").Equals(DBNull.Value) Then
    item.WhenUpdated = cmd.Parameters("@whenUpdated").Value
End If
Samuel Carrijo
  • 17,449
  • 12
  • 49
  • 59
0

Use the operator If(...) if you want short-circuiting behavior.

Use the function IIf(...) if you don't want short-circuiting behavior.

Chris
  • 2,959
  • 1
  • 30
  • 46