Strange effect with ReDim

  • Thread starter Thread starter bpsdgnews
  • Start date Start date
B

bpsdgnews

Hi,

I declared two arrays like this:

Dim Tussentijden(0, 0) As Integer
Dim Intervals(0, 0) As Integer

They both stay unused for a while and then somewhere in code I ReDim
them like this:

ReDim Preserve Tussentijden(psmi.iTotalFiles - 1, AantalTussentijden -
1)
ReDim Preserve Intervals(psmi.iTotalFiles - 1, AantalTussentijden - 1)

The first line works well, both dimensions are changed from 0 to
respectively 3 and 13.
The second line prioduces an error saying ReDim can only change the
righthandside dimension.

If that is so, why wasn't it a problem in the first line. The are
exactly alike and there is no code in between.

???????????
 
A couple of thoughts here:

Have you checked the incoming values of psmi.iTotalFiles and
AantalTussentijden to see what they are at runtime and that they are both,
in fact, returning Integers?

Have you tried doing the initial declarations like this:

Dim Tussentijden() As Integer
Dim Intervals() As Integer

Since there is no requirement to declare the size of the array if you are
just going to ReDim the array later.

-Scott
 
bpsdgnews said:
Hi,

I declared two arrays like this:

Dim Tussentijden(0, 0) As Integer
Dim Intervals(0, 0) As Integer

They both stay unused for a while and then somewhere in code I ReDim
them like this:

ReDim Preserve Tussentijden(psmi.iTotalFiles - 1, AantalTussentijden -
1)
ReDim Preserve Intervals(psmi.iTotalFiles - 1, AantalTussentijden - 1)

The first line works well, both dimensions are changed from 0 to
respectively 3 and 13.
The second line prioduces an error saying ReDim can only change the
righthandside dimension.

If that is so, why wasn't it a problem in the first line. The are
exactly alike and there is no code in between.

???????????

Something is wrong with the info you posted, as the following code
errors on the first redim:

Option Strict On
Module Module1
Sub Main()
Dim A(0, 0) As Integer
Dim B(0, 0) As Integer
ReDim Preserve A(3, 13)
ReDim Preserve B(3, 13)
Console.WriteLine("Done")
Console.ReadKey()
End Sub
End Module
 
Hello, bpsdgnews,

Is it possible that somewhere between the times of the original "Dim
Tussentijden" and the "Redim Preserve Tussentijden" that seems to work there
has been another Redim without the "Preserve" keyword that has set the first
dimension to 3?

What do you get if you place the line:

Debug.WriteLine("Tussentijden.GetUpperBound(0) = " &
Tussentijden.GetUpperBound(0))

directly in front of "Redim Preserve Tussentijden"?

Groetjes,
Randy
 
Back
Top