02-05-2024, 11:54 PM
(02-05-2024, 04:30 PM)Dimster Wrote: Where I have 3 different arrays with the following Dimensions
Dim Array1(1 To 0)
Dim Array2(1 To 1)
Dim Array3(0 To 1)
Is that middle Array2 the same as Dim Array2(0) or would it be the same as Dim Array2(1)?
Thanks
Let's go over your 3 cases one-by-one, and change them slightly to something else which is easir to understand:
FOR Array1 = 1 TO 0
FOR Array2 = 1 TO 1
FOR Array3 = 0 TO 1
Now, with those FOR statements, what do you expect is going to happen?
FOR Array1 = 1 to 0 <-- This one isn't going to do anything as the lowerbound is already higher than the upperbound. The moment it runs, it's just going to kick out and ignore that FOR loop.
FOR Array2 = 1 TO 1 <-- This loop is going to start at a value of 1, and stop at a value of one. It's going to do whatever it's going to do once, and that's it.
FOR Array3 = 0 TO 1 <-- This loop is going to start at a value of 0 and stop at a value of one. It's going to do whatever it does twice.
Your arrays are more-or-less exactly the same.
DIM Array(1 TO 0) <-- The lower bound is greater than the higher bound. You're ending reserving memory for it, before you even start. This ***IS NOT*** going to run well, nor work very good for you. (From what I recall, it causes a seg fault at run time perhaps? Or did we fix that at some point so that it tosses an error in the IDE now? I'd have to test to check to be certain -- either way, it's not something that I'd count on working at all.)
DIM Array2(1 TO 1) <-- This is going to create an array in memory called Array2, and it's going to save 1 element for it, and that element index is going to start at 1 and end at 1. The *ONLY* real element this array has is Array2(1). If you try Array2(0) or Array2(2), or anything else, you'll get Subscript Out Of Range ERROR.
Dim Array3(0 TO 1) <-- This is going to create an array in memory called Array3, and it's going to have 2 elements in it, starting at element 0 and ending at element 1. You can access it via Array3(0) and Array3(1). Anything besides those two indexes are going to be Subscript Out Of Range Errors.
So for your question of, " Is that middle Array2 the same as Dim Array2(0) or would it be the same as Dim Array2(1)? ", the answer is:
NEITHER!!
Dim Array2(0) would be the same as DIM Array2(0 TO 0) and create a single element array named Array2, but it'd only have one element to it with an index of 0. If you try to use Array2(1), it'd cause a Subscript Out of Range ERROR.
Dim Array2(1) would be the same as DIM Array2(0 TO 1) and create an array with 2 elements, which are referenced with indexes starting at 0 and ending at 1.
Neither of which is the same as an array with only one index starting at one and ending at one.
Think of it as:
FOR i = 0 TO 0
FOR i = 1 TO 1
FOR i = 0 TO 1
Now, of the first and the last, which one matches the 2nd? Your answer to the above would be the same with:
(Dim Array2(0))
(Dim Array2(1 TO 1))
(Dim Array2(1))
Which of those match the middle one?