IFfiness - nice to know

Today Claude gave me code I thought would not work in Xojo, but it did.

The follow IF statement is valid. Very nice for compact coding.

var s as string
var b as Boolean = false
s = if(b,“1”,“0”) // didn't know this is valid - but it is :-)
msgbox s
1 Like

But that’s practically the first example in the documentation :smiley:

If(condition, <result when expression is true>, <result when expression is false>)
2 Likes

Who reads documentation? :shushing_face:

2 Likes

in other languages its called via IIf
Immediate If

Who reads documentation

this description should be your origin of knowledge not Claude ..

3 Likes

Wrong thread. Fww the .child issue had zero todo with AI

I’ve known about it forever but never use it - hard to read, and can’t breakpoint on the branches.

3 Likes

Was happy when Xojo added this to the language natively because I’d been using my own iif function for years. Missed it when I converted over from VB.

1 Like

It’s a great feature. I use ternary operators in other languages for conditional text display, so was quite happy when it was added to Xojo. I tend to avoid using it for anything other than things like:

labelValue.Text = If(myBoolean, "Something", "Something Else")

As @Julia_Truchsess said, it can make code hard to read and manage otherwise.

But it makes this a lot shorter:

If myBoolean Then
  labelValue.Text = "Something"
Else
  labelValue.Text = "Something Else"
End If
3 Likes

Me

And this is very old. I don’t even remember when it was introduced. I use it extensively.

1 Like

I use it a lot for hard-coded dark mode colors (which I don’t find need to be five lines)

// Set contrasting color
g.DrawingColor = if(Color.IsDarkMode, SomethingLight, SomethingDark)
1 Like

I use the ternary operator much less frequently than the regular If but there are cases where it results in code that is easier to read.

I don’t generally strive for the most concise code because more often than not a more verbose version improves readability, but sometime shorter code is also clearer and its meaning easier to grasp at first sight.

1 Like

I love ternary operators. I first used them in Unity, and then found it in Xojo.

There are times when I use If...Else...Endif when I need to log stuff or add a break command.