What is best way to reset an array back to -1 elements?

Hello all,

Is there a better way to reset an array back to empty (-1)?

For z As Integer =RemoteResponseData.Ubound Downto -1
If z > -1 Then RemoteResponseData.Remove z
Next z

Thanks,
Tim

Redim

1 Like

To expand on Markus’ reply,

Redim RemoteResponseData(-1)

Redim even though it is a property?

The answers are there:
https://documentation.xojo.com/api/language/redim.html

1 Like

Since API2 it’s MyArray.ResizeTo(-1)

2 Likes

Yes, why not? The Redim function applies to a property (the array), nothing’s weird.

You can also do MyArray.RemoveAll

ResizeTo (-1)

ReDim is a method - what gave you the idea it is a property?

You can treat a property just the same as a local variable.

1 Like

Probably one point where API 2 brings indeed a bit more clarity.
“Redim” compared to “Dim” sounds indeed like you would declare a variable twice. In fact Redim is/was exclusive for arrays, and ResizeTo explains its function a bit better: Resize an array to the UBound you specify.

3 Likes

Hmm, I don’t agree with that. This feels like one of those 2.0 things that didn’t need help for noobs or old hands and was changed just because things were being changed. If you want clarity, then why not TheArray.Empty()? You empty a box of crackers, you don’t resize the box the crackers came in.

As for ReDim I think of it like Refill, or Rebuild. When you refill your car’s tank, you’re not creating a new tank. Likewise, when you rebuild you garage, you’re not creating a second, new garage. In the same vein, when I redim an array, I reset it’s diminsions.

Now, I do agree that refilling my car’s tank does not “empty it”, and Redim is in that same arena, but ResizeTo implies that I’m changing the volumetric properties of the tank, not removing its contents: MyTank.ResizeTo(1000L) :slight_smile:

3 Likes

… is more general than .empty (which is easy enough to implement yourself)

You are correct in that - I could redefine every changed method/function/event to anything that I want, but that defeats having a language to begin with.

3 Likes

The point is that the language should be general (eg ResizeTo), thereby allowing specific cases to be implemented by the user, eg (untested)

Sub empty( extends arr() as variant ) as variant 
  return arr.ResizeTo(-1))
End Sub

Good example is Joe Strout’s StringUtils library

To me resize does makes sense. But am old, still use redim in code (like I have for decades).

Especially when I set to the capacity of how many items it will hold, then set about filling those items without using .append.

There is a .RemoveAll method in API2.

https://documentation.xojo.com/api/language/arrays.html#arrays-removeAll

2 Likes

Well, it’s aimed at beginners after all that do not understand the meaning of append … :wink:

I’m still on API1 most of the time (though I TRY to use API2 for my xDev articles … and add everywhere drives me bonkers)

I don’t agree with all the additions, but RemoveAll does make for clearer code.

1 Like

Yes. But that could have been added / contributed quite easily by anyone … I’d rather the engineers deal with things we can’t do.

1 Like