Possible bug in array-related syntax

The last line of code:

var i1 as Integer = Array("A", "B", "C").IndexOf("B")

var s as String = "B"
var i2 as Integer = Array("A", "B", "C").IndexOf(s)

Does not compile on Win Xojo 2020R2.

The first line does compile and produces the correct result.

Is this a bug or intentional?

Gerard

I meant 2022R2

This DOES work:

var s As String = “B”
var a() As String = Array(“A”,“B”,“C”)
var i1 As Integer = a.IndexOf(“B”)
var i2 As Integer = a.IndexOf(s)

What you are describing seems like a bug or inconsistency to me but in practice the above code is more likely to be what you’d actually use. Otherwise you’re creating an array just to throw it away.

1 Like

Yeh, but something like

dim s as string = "("
dim isPartOfANumber as boolean = array("0", "1", "2", "3", "4", "5", "6", _
    "7", "8", "9", "+", "-", ".", ",").IndexOf(s) >= 0

would be most useful in a lot of places.

True enough but on a hot path I would define that array once in advance of the loop or outside a very frequently called method. And I have a tendency to do that if it’s going to be called more than once since it’s easy to do.

All that said … I can see no reason that the OP’s code shouldn’t work fine.

It could also be argued that array would serve better being a static but then statics are a whole other world of why is my code misbehaving…