mskeel Posted July 28, 2004 Posted July 28, 2004 Is there a construct similiar to the C/C++ ?: (ternary) operator for VB.Net? Thanks. Quote
Jaco Posted July 29, 2004 Posted July 29, 2004 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. Quote
*Experts* Nerseus Posted July 29, 2004 *Experts* Posted July 29, 2004 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 Quote "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
Joe Mamma Posted July 29, 2004 Posted July 29, 2004 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 Quote 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.
Wile Posted July 29, 2004 Posted July 29, 2004 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 Quote Nothing is as illusive as 'the last bug'.
Jaco Posted July 29, 2004 Posted July 29, 2004 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. Quote
*Experts* Nerseus Posted July 29, 2004 *Experts* Posted July 29, 2004 Wile said I was thinking (but said wrong). I got confused on the IIF versus the new "AndAlso" keyword. -ner Quote "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
ThePentiumGuy Posted July 30, 2004 Posted July 30, 2004 How do you use the AndAlso and OrElse keywords? Wow .NET is becoming very simplified... like the old Cobalt. Quote My VB.NET Game Programming Tutorial Site (GDI+, Direct3D, Tetris [coming soon], a full RPG.... you name it!) vbprogramming.8k.com My Project (Need VB.NET Programmers) http://workspaces.gotdotnet.com/ResolutionRPG
mskeel Posted July 30, 2004 Author Posted July 30, 2004 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. Quote
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.