Get an empty array

How could I get an empty array?
The array()-function doesn’t work without without any parameter.
ReDim does only work on array that is not Nil.

Here is some Code:

Dim tmpObjectNames() As String = RaiseEvent getObjects
If tmpObjectNames = Nil Then tmpObjectNames = array()

If the Event “getObjects” is not implemented, tmpObjectNames is Nil. In this case it should be set to an empty array. So do I really have to do something like:

If tmpObjectNames = Nil Then 
Dim bla() as String
tmpObjectNames = bla

Like this: (?)

Dim tmpObjectNames(10) As String 

It is not permitted to re-instantiate an object that has already been instantiated

I don’t do that !

If you prefer, do:
Dim i(10) As Integer

Just like in this sample from the Documentation:

Yes, ok. But unfortunately that has nothing to do with my question.


I’m not sure if I understand your question correctly.

Initiate an empty array with Var arsContent() As String. Alternatively, you can also write Var arsContent(-1) As String. Does it help? A nil array = empty array in my eyes.

I can’t test it right now, that’s why i have to come up with one of my Quick&Dirty Solutions again…

Replace your above Code with:

If tmpObjectNames = Nil Then
  tmpObjectNames = Array("")
End If

This may look stupid (because it is) but it could work… :slight_smile:

1 Like

Unfortunately it is not

Empty array is: String(-1) (This can be used, for example, to execute a ReDim or .Add or other array functions)

If it is Nil, you could not do all this.

Make it a variant that can be nil or an array (not variant array)

Yes, that’ll work. It’s just not pretty. I like nice code. :face_with_raised_eyebrow:
Thanks for the code.

Okay, we agree on that. I was hoping I had overlooked something. It would be too easy if array() simply worked without parameters. But let’s be honest, it’s a Xojo feature (not a bug) that I just don’t understand and after all, there is a workaround… :laughing:

I’m trying to understand why you need a Nil Array? You can query the number of elements in Array.Count to find out whether the array is empty.

I don’t see how significantly complicating my code should bring anything positive here. It makes things worse, not better.

I don’t need that, and I don’t want that. There are simply situations where Xojo gives me that back.

If it is Nil you could not do .count()… But even if you could, that qould not change anything, because there is no problem finding out if the array is nil. The problem is to return it back to a functioning state.

But you could create an empty Array and use it in such cases.
If you have an unused empty Array Property somewhere or something like

Var emptyArray() As String

you could later simply do this:

If tmpObjectNames = Nil Then tmpObjectNames = emptyArray

? You asked how you can do it. Not a percectly nice method to do so.

How could I get an empty array?
The array()-function doesn’t work without without any parameter.
ReDim does only work on array that is not Nil.

Sorry for helping!

Yes, thank you. Now we are at the “what workarounds can we find” level. To be honest, I can think of a few variants myself. I just don’t like workarounds and I was (once again) hoping that “it must work out of the box”

I understand you. But I’m afraid that, as you said, it’s a quirk of Xojo that we have to live with.
But maybe it’s worth a feature request to you? :slight_smile:

But my question was how to get an empty array in a “proper” way without having to create one :wink:
It has simply become completely normal here in the forum for some people to immediately help with a workaround for every problem.
That’s the real problem, you create workarounds all day long.
But there would be no need for a workaround if (as in other languages I know) you could simply make a variable = array().
My hope was that someone would say: Sure it works, you just overlooked the function XYZ.

Translated with DeepL Translate: The world's most accurate translator (free version)

I am very pleased that you wanted to help! It’s just a difference whether I’m looking for a workaround to be able to continue working, or whether I just wanted confirmation that I haven’t overlooked anything :wink:

I apologise if I was a little unfriendly.

1 Like

If only it were the only one :wink: :handshake: :joy: