?: in visual basic .net

Diesel said:
The IIF function

Yes, but be aware that the IIF function always evaluates all parameters, where the "?" operator only evaluates the true or false value, but never both. This may result in more errors using the IIF function.
 
I thought I read that VB.NET 2003 introduced a new way to do IIF that only evaluated the true/false part instead of both?

For example:
Code:
Dim b as Boolean = True
Dim d As DialogResult = IIF(b, MessageBox.Show("b is true"), MessageBox.Show("b is false"))

Youll get both messageboxes. I cant remember the new syntax.

Anyway, I would worry about using IIF except in extremely simple scenarios, never something that might cause a problem like the above. Anything thats "too complex" should likely be written as a full "If...Then...Else" for readability.

I scanned my code at work, and I saw less than 10 occurrences of "( ? : )" syntax and thats searching a LOT of code.

-ner
 
Anyway, I would worry about using IIF except in extremely simple scenarios, never something that might cause a problem like the above. Anything thats "too complex" should likely be written as a full "If...Then...Else" for readability.
and bear in mind, if you run into complex conditional scenarios, you should probably apply polymorphic refactoring.

when I get to work tomorrow I will post an example
 
Nerseus said:
I thought I read that VB.NET 2003 introduced a new way to do IIF that only evaluated the true/false part instead of both?

I only know of the keywords you can use for or and and constructions in a If statement (again vb evaluates everything, C++ stops when it already knows the result is false / true)
for And it is AndAlso
for Or it is OrElse
 
Nerseus said:
I thought I read that VB.NET 2003 introduced a new way to do IIF that only evaluated the true/false part instead of both?

For example:
Code:
Dim b as Boolean = True
Dim d As DialogResult = IIF(b, MessageBox.Show("b is true"), MessageBox.Show("b is false"))

Youll get both messageboxes. I cant remember the new syntax.

Anyway, I would worry about using IIF except in extremely simple scenarios, never something that might cause a problem like the above. Anything thats "too complex" should likely be written as a full "If...Then...Else" for readability.

I scanned my code at work, and I saw less than 10 occurrences of "( ? : )" syntax and thats searching a LOT of code.

-ner


Actually, the reason why IIF always evaluates everything is that its a function - when you call any function all parameters are evaluated. So as long as IIF is a function and not an operator (like ?:), all parameters will be evaluated. I dont think it is possible for Microsoft to change this.
 
Wile said I was thinking (but said wrong). I got confused on the IIF versus the new "AndAlso" keyword.

-ner
 
AndAlso, OrElse

They cause VB.net boolean logic to work just like you would expect them to work if you were a c/c++ programmer.

for example:

Code:
Dim x as Integer = 3
Dim y as Integer = 10

if x = 10 AndAlso y = 10 then
   check the first statement, if its true, check
   the second statement, else the if statement is false.
   It will actually drop out of the statement at the first false it gets.
   So in this case, if y=10 is never actually checked.
end if

if y = 10 OrElse x = 3 then
  this will mark the if statement true the first time it comes
  to something true
end if

It all works just like C/C++ does. I use it to check VB "pointers" to make sure Im not derefrencing a null pointer.

Thanks for all the help guys. Too about about the lack of a true ?: operator. In the right situation it makes for a very elegant solution.
 
Back
Top