5 Replies - 1322 Views - Last Post: 11 February 2012 - 03:45 PM Rate Topic: -----

#1 LetMeFinclOut  Icon User is offline

  • D.I.C Head

Reputation: 11
  • View blog
  • Posts: 115
  • Joined: 14-May 09

A Case for Option Strict

Posted 10 February 2012 - 10:39 AM

Original credit goes to CodeProject, but it's too good not to share.

Quote

Names changed to protect the innocent...

I'm working with some VB.net projects and I decided to turn on Option Strict. It threw an error for this line:
If someProp Is Nothing OrElse String.IsNullOrEmpty(someProp.Value OrElse someProp.Value = "0") Then



This is what was intended:
If someProp Is Nothing OrElse String.IsNullOrEmpty(someProp.Value) OrElse someProp.Value = "0" Then



:bananaman: for option explicit! :cry2: for the fact that I have several projects to go and I'm working in the order of best code to worst code.


The fact that VB.Net even considers this valid code is just batcrap insane.

This post has been edited by LetMeFinclOut: 10 February 2012 - 03:45 PM


Is This A Good Question/Topic? 0
  • +

Replies To: A Case for Option Strict

#2 tlhIn`toq  Icon User is offline

  • Please show what you have already tried when asking a question.
  • member icon

Reputation: 5436
  • View blog
  • Posts: 11,661
  • Joined: 02-June 10

Re: A Case for Option Strict

Posted 10 February 2012 - 12:07 PM

I'm not sure I see why you have a problem with it.
Looks like value validation to me.

If some property is null or if the property value is empty or '0'

The is probably for something like: If the user didn't check a box, or the user didn't enter a valid value.

Are you saying you don't range-check and validate the values entered by your users before you trying to work with them? THAT would be batcrap insane!
Was This Post Helpful? 0
  • +
  • -

#3 CodeGrappler  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 41
  • View blog
  • Posts: 120
  • Joined: 29-November 10

Re: A Case for Option Strict

Posted 10 February 2012 - 01:01 PM

View PosttlhIn`toq, on 10 February 2012 - 12:07 PM, said:

I'm not sure I see why you have a problem with it.
Looks like value validation to me.

If some property is null or if the property value is empty or '0'

The is probably for something like: If the user didn't check a box, or the user didn't enter a valid value.

Are you saying you don't range-check and validate the values entered by your users before you trying to work with them? THAT would be batcrap insane!


I think you missed the issue here tlhIn`toq. Also possible that I am missing it but I don't think so.

I believe he was trying to point out
(someProp.Value OrElse someProp.Value = "0")

Where it should be
(someProp.Value) OrElse someProp.Value = "0"


Note the placement of the closing parenthesis.
Was This Post Helpful? 0
  • +
  • -

#4 LetMeFinclOut  Icon User is offline

  • D.I.C Head

Reputation: 11
  • View blog
  • Posts: 115
  • Joined: 14-May 09

Re: A Case for Option Strict

Posted 10 February 2012 - 01:20 PM

It's not doing what you think it's doing.

By default, VB.Net will do best-guess conversions unless Option Scrict is turned on. Looking at it again...
If someProp Is Nothing OrElse String.IsNullOrEmpty(someProp.Value OrElse someProp.Value = "0") Then 


As CodeGrappler said, there's a misplacement of parentheses, which is causing some conversions which should never be allowed.
  • someProp.Value is converted to bool for use by OrElse
  • The result of the OrElse operation is then converted back to string for the IsNullOrEmpty check

So the if statement will always resolve to false since it only tests against the strings "True" or "False".

This post has been edited by LetMeFinclOut: 10 February 2012 - 02:08 PM

Was This Post Helpful? 2
  • +
  • -

#5 JackOfAllTrades  Icon User is offline

  • Saucy!
  • member icon

Reputation: 6039
  • View blog
  • Posts: 23,435
  • Joined: 23-August 08

Re: A Case for Option Strict

Posted 10 February 2012 - 02:24 PM

Similar to something I did this week on a SQL right join through an ORM. Accidentally was placing the conditions of the WHERE clause in the JOIN conditions, rather than the WHERE. That frustrated the hell out of me for a couple of hours. Had I been writing straight SQL (which is where I built the query) rather than operating within the framework, it wouldn't have happened. Boy did I feel stupid when I figured that out.
Was This Post Helpful? 0
  • +
  • -

#6 dawmail333  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 27
  • View blog
  • Posts: 174
  • Joined: 02-July 07

Re: A Case for Option Strict

Posted 11 February 2012 - 03:45 PM

Hey! I saw this one on the CodeProject! :P
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1