In-line IF()

I can’t find an in-line IF() like I can use in Delphi

nameLabel.text = npChristianName + if(len(npPrefix)>0;npPrefix;chr(32)) + npFamilyName

Does it not exist?

No, you have to write:

nameLabel.text = npChristianName
if(len(npPrefix)>0;npPrefix;chr(32)) + npFamilyName then
   nameLabel.text = nameLabel.text + npFamilyName
end if

It is not quite the same, the op wants to assign conditionally in one line of code

Yes, that’s the normal route, but an in-line IF() is very convenient…

Oh - I see you have changed your reply :slight_smile:

As a guy coming from C I agree is very convenient as the ? operator and many other things.
However not very readable.

Mmm… In-line IF()'s could speed up things… too bad.

[quote=22113:@Alexander van der Linden]I can’t find an in-line IF() like I can use in Delphi

nameLabel.text = npChristianName + if(len(npPrefix)>0;npPrefix;chr(32)) + npFamilyName

Does it not exist?[/quote]
Subjectives apart, I think you will need to make your own function(s) to achieve the assignment in one line of code in the neatest way.

The closest you can probably get is:

  dim s as string
  dim i as integer
  
  dim name as string = "carl"
  dim suffix1 as string = " :-)"
  dim suffix2 as string = " :-("
  i = 8
  
  if i > 9 then s = name + suffix1 else s = name + suffix2
  
  dim mb as new MessageDialog
  mb.Message = s
  call mb.ShowModal()

Carl, this will possibly spoil optimizations made from the compiler and introduce function call overhead, albeit negligible.

Your inline if could be written as a method easily enough, if Xojo would inline methods as an optimization (maybe it does?) then it would be no overhead either, here is an untested example, iif = Inline If

Function Iif(condition As Boolean, trueCase As Variant, falseCase As Variant) As Variant If condition Then Return trueCase Else Return falseCase End If End Function

If you wanted to get rid of variant uses, you could create several as overloaded functions.

Now, in addition to the overhead of a method call, the above function has the problem of not short circuiting its true and false values, for example this would cause runtime issues:

Dim people() As Array Dim firstPerson As String = Iif(people.UBound > -1, people(0), Nil)

because people(0) would be evaluated regardless of what your condition evaluates to.

Anyway, just something to think about.

My code is almost identical, been using it for years.

Function IIF(Conditional As Variant, Option1 as Variant, Option2 as Variant) As Variant

  if Conditional = True then
    return Option1
  else
    return Option2
  end
  
End Function

BTW, I’m a creature of the old FoxBase/FoxPro world where it was built in. I just got in the habit of using it a lot.

I love ternary operators like (condition? true : false), and the find it very readable. VisualBasic had IIF(), and VB.Net introduced the optimized inline IF(condition, then, else). Ternary operations are present in more than 20 languages, including newer versions of basic via IIF() or IF(). Should be on the list of the “Things missing needing to add to the language”.

“and the find it very readable” = and I find them very readable

You can edit your posts, there is a bug that makes the Edit icon not immediately visible after posting a message. Hit refresh, then you will see an edit icon by the Quote icon. The trick is, you have to do it within a certain time frame (not sure what that time is) and the thread can not contain any posts after yours, like this one… it just blocked you from editing your posts.

I know. I tried. Sometimes this “feature” just fails, as it did, again. :slight_smile: Interestingly, my errata post was editable, I’ve refreshed it too, just for checking. :frowning:

I’m pretty sure there is no time frame, it’s just a question of any posts that follow yours.

Sometime yes, sometimes no. There were no posts after mine for 2 minutes. I’ve tried to edit, refreshed, reloaded. Gone home and returned. Nothing. So, I needed to write an errata.

That’s happened to me too, but I don’t think it’s time-based, I think it’s part of the same bug. Just my opinion based on mine and others’ tests and comments.

In any case, I’m sure we agree that it should be fixed.

I think I read that you can edit until the post has be viewed. Which makes sense, except it doesn’t work well yet.