Jump to content

Recommended Posts

Posted (edited)

Here's a good one.

 

I had a line of code:

  user.Initials = IIf(NewUser.MiddleName Is Nothing, "", NewUser.MiddleName.Chars(0))

 

Looks pretty straightforward.

If the NewUser.Middlename is Nothing, then user.Initials = "".

If the NewUser.Middlename is not Nothing, then user.Initials is the first character of NewUser.Middlename.

 

It kept throwing exceptions.

 

Well, it turns out that the IIF functions, rather than being the concise and efficient method I thought it was, is a bit rubbish.

It actually evaluates all three of the arguments every single time. Of course, if the NewUser.Middlename was Nothing, then NewUser.Middlename.Chars(0) will throw an exception, giving me my error. I presumed that it wouldn't get that far, but lo and behold it evaluated it every time giving me my exceptions.

 

Back to IF blocks.

 

https://msdn.microsoft.com/en-us/library/27ydhh0d%28v=vs.90%29.aspx

Edited by jinnantonnixx

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now



×
×
  • Create New...