mskeel
Well-known member
Is there a construct similiar to the C/C++ ?: (ternary) operator for VB.Net?
Thanks.
Thanks.
Diesel said:The IIF function
Dim b as Boolean = True
Dim d As DialogResult = IIF(b, MessageBox.Show("b is true"), MessageBox.Show("b is false"))
and bear in mind, if you run into complex conditional scenarios, you should probably apply polymorphic refactoring.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.
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?
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
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