Page 1 of 1

Please allow: Dim MyArray(-1)

Posted: Mon Mar 18, 2013 6:39 pm
by Josh
Please allow:

Code: Select all

Dim MyArray(-1)
;of course also for ReDim
This would be the same like this:

Code: Select all

Dim MyArray(0)
FreeArray (MyArray())
Background:
I often work with COM functions witch I combine in procedures. The COM arguments are in arrays. Sometimes there are arguments, sometimes there are no arguments:

Code: Select all

Procedure Proc1 (Array TestArray(1))

  Debug "cntParams = " + Str(ArraySize (TestArray()) + 1)
 ;Some COM functions

EndProcedure 

cntParams = 0

;Code needed now
If cntParams = 0
  Dim MyArray(0)
  FreeArray (MyArray())
Else
  Dim MyArray(cntParams - 1)
EndIf

;Code needed in future
;Dim MyArray(cntParams - 1)


Proc1 (MyArray())
Yes, I know, thats only for comfort, but it would make the code more clear

Re: Please allow: Dim MyArray(-1)

Posted: Sat Mar 23, 2013 7:11 am
by charvista
+1
But also, it could be extended to have a range.

Code: Select all

Dim A(-4:4)
meaning from A(-4) to A(4) so it would always have a parallelism with what we need, instead of adjustments.

Arrays with 2 dimensions or more, could be identical:

Code: Select all

Dim A(4:9,1:2)

Re: Please allow: Dim MyArray(-1)

Posted: Sat Mar 23, 2013 7:59 am
by MachineCode

Code: Select all

;Code needed now
If cntParams = 0
  Dim MyArray(0)
  FreeArray (MyArray())
Else
  Dim MyArray(cntParams - 1)
EndIf
The above is what ReDim is for. ;)

Re: Please allow: Dim MyArray(-1)

Posted: Sat Mar 23, 2013 9:28 am
by Little John
Josh wrote:Please allow:

Code: Select all

Dim MyArray(-1)
;of course also for ReDim
This would be the same like this:

Code: Select all

Dim MyArray(0)
FreeArray (MyArray())
What is the purpose of firstly calling Dim MyArray(0) ?
Just calling

Code: Select all

FreeArray(MyArray())
has the same effect, hasn't it?
Josh wrote:Yes, I know, thats only for comfort, but it would make the code more clear
Since the index -1 would have a special meaning, not consistent with the meaning of all other indexes, this would decrease the readability of the code. In contrast, FreeArray() clearly tells the reader of the code what's going on.
So my vote is -1.

Re: Please allow: Dim MyArray(-1)

Posted: Sat Mar 23, 2013 10:02 am
by Josh
MachineCode wrote:

Code: Select all

;Code needed now
If cntParams = 0
  Dim MyArray(0)
  FreeArray (MyArray())
Else
  Dim MyArray(cntParams - 1)
EndIf
The above is what ReDim is for. ;)
What? Have you read my posting? Did you really had a look at my example?
Little John wrote:
Josh wrote:What is the purpose of firstly calling Dim MyArray(0) ?
Just calling

Code: Select all

FreeArray (MyArray())
has the same effect, hasn't it?
Do you know, what you are writing? Apart from the fact, that FreeArray without Dim isn't working, this would break all rules.
Little John wrote:Since the index -1 would have a special meaning, not consistent with the meaning of all other indexes.
There is no special meaning:
Dim MyArray(2) defines an array with 3 elements
Dim MyArray(1) defines an array with 2 elements
Dim MyArray(0) defines an array with 1 element
Dim MyArray(-1) defines an array with 0 elements
Little John wrote:...this would decrease the readability of the code.
If you don't like it, don't use it. For me it would make a better readable code, if I have one line instead of six.

Re: Please allow: Dim MyArray(-1)

Posted: Sat Mar 23, 2013 11:26 am
by Little John
Josh wrote:
Little John wrote:
Josh wrote:What is the purpose of firstly calling Dim MyArray(0) ?
Just calling

Code: Select all

FreeArray (MyArray())
has the same effect, hasn't it?
Do you know, what you are writing?
In case you want a serious discussion, better stop stupid polemics ...
Josh wrote:Apart from the fact, that FreeArray without Dim isn't working,
You had posted only a short code snippet. I have assumed that the array had been Dimed before somewhere in the code.

Sorry, I was mistaken concerning the effect of FreeArray(), because of its description in the documentation:
Documentation of FreeArray() wrote:Free the specified 'Array()' and release all its associated memory. To use it again Dim has to be called.
I see now that the sentence which I wrote in red here is wrong: After calling FreeArray(), the array can still be used (e.g. as parameter for self-written procedures or as argument for ArraySize()).