Just curious: is the new If operator evaluating both the true and the false part or only the “chosen one”?
NO! I tested just that in the beta phase, and that’s why it’s better than anything you can do yourself in code. It acts exactly the same as
If condition Then
x = value
Else
x = otherValue
End If
.
It is unclear to me (and I did not find the info) what the “New If operator” quoted in the release note means.
Can you give me an example ?
thanks,
Franck
Thanks Paul. But what is new there ? I was already using “if condition then return” on one line. Is it that now you can also have an ‘else’ statement as long as it is a single statement both for the ‘then’ and the ‘else’ part ?
You can now do this:
x = if( condition, y, z )
[quote=70672:@Kem Tekinay]You can now do this:
x = if( condition, y, z )
[/quote]
Can’t find any documentation on that?
I suspect x is a boolean?
Seems weird to me, so you have to test x after you did that if statement?
Ah, there I see what is new !
that’s very compact indeed while staying quite clear.
Paul : there is no mention of this in http://documentation.xojo.com/index.php/If
No, I guess that x if of the same type than y and z
aString = if( aVar>10, “OK”, “Not OK”)
(edit: the link indicated by Jason seems to confirm this)
What is new is that this is an operator, not a statement. So you can do something like:
Dim resultMessage As String
resultMessage = If(count > 1, Str(count) + " results found", "1 result found")
Instead of:
Dim resultMessage As String
If count > 1 Then
resultMessage = Str(count) + " results found"
Else
resultMessage = "1 result found"
End If
Since this returns a result, you can use it in expressions and even pass it as a parameter:
ShowMessage(If(count > 1, Str(count) + " results found", "1 result found"))
No, x can be anything, a string, integer, double, object, etc., but both y and z have to be assignable to x or there will be a compiler error. (y and z can also be functions that return some value that can be assigned to x.)
For example, this is the old and new way to do the same code:
dim x, y, z as string
y = "now"
z = "forever"
if someCondition then
x = y
else
x = z
end if
Now:
x = if( someCondition, y, z )
Sorry, I had to clear the cache. Hopefully it is OK now.
Interesting. This is the IIF () operator I use a lot in Microsoft Access.
actually it is a FUNCTION, not an operator. and yes it is very much like IIF() [wish Xojo had called it that]
and as I will probably get flack for saying that… here is MY interpetation (perhaps old school, perhaps not)
An Operator is a symbol or command key word that performs a mathematical or string “operation”… such as + for add, * for mult etc.
while a FUNCTION is either system supplied (in the case of IF(), LEFT(), MID() etc) or defined by the user, and performs some function that may be one to “N” lines of code, returning a value of some type.
[quote=70720:@Dave S]actually it is a FUNCTION, not an operator. and yes it is very much like IIF() [wish Xojo had called it that]
and as I will probably get flack for saying that… here is MY interpetation (perhaps old school, perhaps not)
An Operator is a symbol or command key word that performs a mathematical or string “operation”… such as + for add, * for mult etc.
while a FUNCTION is either system supplied (in the case of IF(), LEFT(), MID() etc) or defined by the user, and performs some function that may be one to “N” lines of code, returning a value of some type.[/quote]
There’s not a particularly concrete definition of what an operator is, but there’s nothing stopping them from being syntactically function-like. The term “function” has some implications which are definitely not present here. The If
expression does not incur function call overhead or semantics (there are no overloads for it, it does not evaluate all arguments, it does not require stack frames, or even branch dispatch). So calling it an “operator” has more accurate connotations in a programming context.
As for the name If vs IIf, If was chosen because it was already a keyword and we didn’t need to break any existing user code.
curious question… in what way would IIF break existing user code, as opposed to what I would assume would be a more complex compiler modification to evaluate “IF” in two different syntax scenarios?
Only way I can see would be if someone created an IIF function of their own… which I suppose has happened
Any function, variable, and such that happened to be named ‘IIF’ would become a syntax error.
It’s actually not complex at all to disambiguate.
It’s much more common than you think.
I have a library of “helper” functions (a module) I include in all new projects (by adding them to the templates). Iif was one of them that I can now remove.