advanced web statistics

VB.NET Conditional Operators Or Lack Thereof

3/18/2008 4:41:58 PM

I almost forgot how to login to this damn thing! Anyway, good to be back.  As you have probably gathered I have been extremely busy.  Busy doesn't even do justice to how busy I've been lately.  Anyway, I have been working on a large project for the past couple of weeks that just so happens to be developed in..... VB.NET!  It's been interesting. Soooo interesting in fact that I decided to make a post about it.  Here's a quick snippet.

Dim foo As Nullable(Of Decimal) = IIf(String.IsNullOrEmpty(textFoo.Text), CType(Nothing, Nullable(Of Decimal)), Convert.ToDecimal(textFoo.Text)) 

If there is no text in the textbox I want a null value; else cast the value to decimal. Easy right?  The stored procedure allows for the updating of certain fields as NULL.  No big deal.

Response.Write(IIf(foo.HasValue, foo.Value, "NOTHING")) 

If there is a value, write it. If not, write something else. Easy right? This actually works.  Keeping with the logic of this example take a function that accepts a Nullable(Of Decimal) parameter. This should be a no-brainer...

parameters.Add(command, "@foo", IIf(foo.HasValue, foo.Value, CType(Nothing, Nullable(Of Decimal))) 

Do you think that works? It doesn't.  Hopefully I am missing something completely stupid here.  The exception I am getting is:

Nullable object must have value.

Let's discuss this.  As you can see the expression in my conditional actually checks to see if I have a value.  If I do have a value, I want to set my parameter to this value.  If I don't have a value, my parameter's value is set to NULL. This is elementary.

Why is it that I am receiving an "object must have value" error when I am checking first for a value?  Does that make sense to anyone else?  You want to know what works?

If foo.HasValue Then
   
parameters.Add(command, "@foo", foo.Value)
Else
   
parameters.Add(command, "@foo"Nothing)
End If 

Insert Gob Bluth's "Come On!" here.  One of the reasons I love C# so much is because I find that I don't have to write code like this.  I really like the idea of writing succinct code (i.e. precise, accomplishing a lot with as few words as possible, one-lining it, etc...).  I think even VB.NET developers have to agree that the C# language lends itself beautifully to this type of coding.  Other examples include Generics, NullableTypes (decimal?), ??, a ? b : c, stacking using constructs & switch cases, optional curly braces on if statements / using constructs / for / while loops, etc, etc).

The code above that "works" doesn't really work for me.  I know that this is personal preference.  I have said time and time again how I miss regions when coding VB.NET.  This is another prime example of why.  Imagine having 6 parameters that are nullable.  Instead of region'ing out this wordy code, I have to scroll a screen and a half before I get to my catch block.  When I inherit C# code with this coding style the first thing I do is hope for the best. Then I refactor the hell out of it.  With ReSharper this is easy as all I have to do is place my cursor to the left of the else and ALT+ENTER the ugliness away  (I've gotten Gene Hackman-quick at this).

I digress. Can anyone fluent in VB please help me with conditional statements?  I know that the IIf function is a huge reset and probably looked down upon, but I just can't bring myself to write out a complete If Then Else. I just can't. C# has spoiled to the point where I can't accept anything less!

VB.NET

kick it on DotNetKicks.com

Leave a Comment

   

  Enter the text to proceed!