Jump to content
Xtreme .Net Talk

Recommended Posts

Posted
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.

  • *Experts*
Posted

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:

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

 

You'll get both messageboxes. I can't 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 that's "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 that's searching a LOT of code.

 

-ner

"I want to stand as close to the edge as I can without going over. Out on the edge you see all the kinds of things you can't see from the center." - Kurt Vonnegut
Posted

Anyway, I would worry about using IIF except in extremely simple scenarios, never something that might cause a problem like the above. Anything that's "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

Joe Mamma

Amendment 4: The right of the people to be secure in their persons, houses, papers, and effects, against unreasonable searches and seizures, shall not be violated, and no warrants shall issue, but upon probable cause, supported by oath or affirmation, and particularly describing the place to be searched, and the persons or things to be seized.

Amendment 9: The enumeration in the Constitution, of certain rights, shall not be construed to deny or disparage others retained by the people.

Posted
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

Nothing is as illusive as 'the last bug'.
Posted
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:

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

 

You'll get both messageboxes. I can't 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 that's "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 that's searching a LOT of code.

 

-ner

 

 

Actually, the reason why IIF always evaluates everything is that it's 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 don't think it is possible for Microsoft to change this.

  • *Experts*
Posted

Wile said I was thinking (but said wrong). I got confused on the IIF versus the new "AndAlso" keyword.

 

-ner

"I want to stand as close to the edge as I can without going over. Out on the edge you see all the kinds of things you can't see from the center." - Kurt Vonnegut
Posted

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:

 

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

if x = 10 AndAlso y = 10 then
  ''check the first statement, if it's 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 I'm 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.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...