jinnantonnixx Posted May 24, 2016 Posted May 24, 2016 (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 May 24, 2016 by jinnantonnixx
Willott Posted May 24, 2016 Posted May 24, 2016 Depending on the language (if c#), could you use the ? operator: https://msdn.microsoft.com/en-gb/library/ty67wk28.aspx Or ternary operator if(): https://blogs.msdn.microsoft.com/vbteam/2008/03/12/orcas-introduces-the-if-operator-a-new-and-improved-iif-sophia-salim/
jinnantonnixx Posted May 24, 2016 Author Posted May 24, 2016 (edited) This project is in VB.NET. It's a minefield. I re-coded it so that the logic is unambiguous. Nothing surprises me in VB.NET Edited May 24, 2016 by jinnantonnixx
Recommended Posts
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 accountSign in
Already have an account? Sign in here.
Sign In Now