.NET, Code

VB.NET, Inline If statement

I’m much more of a C# guy than VB.net .  I haven’t worked steady in VB.net since 1.1 (around 10 years or so).  However before .NET, I was a VB4-6 guy and spent many hours working in classic VB.  Having originally come from a C background, the switch to C# was natural.  One thing that had always bothered me about VB was the lack of short circuit evaluation.  I won’t explain that here, but if you want to learn more, follow that last link.  C and it’s derivatives offered it, while VB did not. 

Not a huge deal, but it often lead to some nested IF’s if you handled it, or some unsuspecting bugs if you didn’t.  Recently, I’ve been working on a project for a client that has requested the project be done in VB.net.  While working on some code, I ran across an old friend, the IIF function.

IIf(PrinterDevice Is Nothing, “<Device not setup yet>”, PrinterDevice.ToString())

Basically, just want to print “<Device not setup yet>” if the PrinterDevice object is nothing, otherwise printout the results of the ToString() on the PrinterDevice.  The problem is, if PrinterDevice is nothing, then the PrinterDevice.ToString() gives a nullreference exception.  As I was preparing to nest my If statements, I discovered something new was added to .NET 2.0 that I hadn’t run across.  For you VB guys, you’ve probably seen this already, but interestingly enough, I had not.

If(PrinterDevice Is Nothing, “<Device not setup yet>”, PrinterDevice.ToString())

That simple change (notice the IF vs IIF) makes the difference.  The IIF call is a method call, so all parameters are evaluated before the call is made.  In the case of the IF, the IF() is a statement, so it first evaluates the condition (PrinterDevice Is Nothing) and if it’s true, it prints “Device not setup yet” and never evaluates the ToString().  It performs the short circuiting that I’m used to.

Nothing new for you VB guys, but for use C# guys that are stepping into the VB world from time to time, this is a nice change.

 

 

Links

Links to Code by #3

A rainy Friday night, so catching up on my blog reading again.

Found this link on CSS tools, some I’ve run across but a few new ones also.    http://www.catswhocode.com/blog/super-useful-tools-for-css-coding,

  • CSSTrashman – This was really interesting to me as CSS has a way of getting away from you.  Took a 88k CSS file on a sample site and brought it down to 8k.
  • Pure – I’ve seen this before, but never used it. 
  • Glue – I’ve used CSS sprites before and realized the benefits, but it was always such a pain to put together.  Glue seems to automate this…very nice.
  • TopCoat – nice and simple, but looks handy.
  • Normalize.css
  • Bear CSS – need to play with this some.

 

Other stuff of interest: