Dim and ReDim? 
Author Message
 Dim and ReDim?

what's going wrong?

in a module i have made a userdefined type:

Type Test
        x as Integer
        y as String
End Type

in the general Declarations section i have initialized the array:
'just according to the help-files and examples

Dim TestArray() As Test

'and a variable i need later

Dim Counter

So now i have an array of zero and i can use ReDim to change the size.

In the Form_load event i set the Counter to zero:

Counter=0

In a procedure i have to change the size, i coded like this:

Sub Command1_click()
        Counter=Counter+1
        ReDim TestArray(Counter) As Test        
        TestArray(Counter).x=1
        TestArray(Counter.y="a"
end Sub

When i run this i get the error Subscipt Out of Range on the line
where if want to fill the array (TestArray(Counter).x=1).

So i tryed an search but it will not work just as it just to be.

The only way it works is when i give the array a size:

Dim TestArray(5) As Test

Then it works fine but thats not what i want.

So if anyone can help????
thanks in advance

gr. walther

------------------- cut here ---------------
Walther Musch
Tilburg University Holland



Fri, 11 Dec 1998 03:00:00 GMT  
 Dim and ReDim?

Quote:

> In a procedure i have to change the size, i coded like this:

> Sub Command1_click()
>         Counter=Counter+1
>         ReDim TestArray(Counter) As Test
>         TestArray(Counter).x=1
>         TestArray(Counter.y="a"
> end Sub

> When i run this i get the error Subscipt Out of Range on the line
> where if want to fill the array (TestArray(Counter).x=1).

Remember that when you Dimension something to X the subscripts go from 0 to X-1.  In the
above case the reference to TestArray(Counter) will always be one higher than the highest
available subscript!
Randy Riesenberg



Fri, 11 Dec 1998 03:00:00 GMT  
 Dim and ReDim?

Quote:

> what's going wrong?

> in a module i have made a userdefined type:

> Type Test
>         x as Integer
>         y as String
> End Type

> in the general Declarations section i have initialized the array:
> 'just according to the help-files and examples

> Dim TestArray() As Test

> 'and a variable i need later

> Dim Counter

> So now i have an array of zero and i can use ReDim to change the size.

> In the Form_load event i set the Counter to zero:

> Counter=0

> In a procedure i have to change the size, i coded like this:

> Sub Command1_click()
>         Counter=Counter+1
>         ReDim TestArray(Counter) As Test
>         TestArray(Counter).x=1
>         TestArray(Counter.y="a"
> end Sub

> When i run this i get the error Subscipt Out of Range on the line
> where if want to fill the array (TestArray(Counter).x=1).

> So i tryed an search but it will not work just as it just to be.

> The only way it works is when i give the array a size:

> Dim TestArray(5) As Test

> Then it works fine but thats not what i want.

> So if anyone can help????
> thanks in advance

> gr. walther

> ------------------- cut here ---------------
> Walther Musch
> Tilburg University Holland

Walther,

Try one of the following:

         ReDim TestArray(Counter+1) As Test
         TestArray(Counter).x=1
         TestArray(Counter.y="a"

-or-

         ReDim TestArray(Counter) As Test
         TestArray(Counter-1).x=1
         TestArray(Counter-1).y="a"

Barry Scott
Huntsville, AL



Fri, 11 Dec 1998 03:00:00 GMT  
 Dim and ReDim?


Quote:


>> In a procedure i have to change the size, i coded like this:

>> Sub Command1_click()
>>         Counter=Counter+1
>>         ReDim TestArray(Counter) As Test
>>         TestArray(Counter).x=1
>>         TestArray(Counter.y="a"
>> end Sub

>> When i run this i get the error Subscipt Out of Range on the line
>> where if want to fill the array (TestArray(Counter).x=1).

>Remember that when you Dimension something to X the subscripts go from 0
to
>X-1.  In the
>above case the reference to TestArray(Counter) will always be one higher
than
>the highest
>available subscript!

Actually the subscipts when you dimension something as X go from 0 to X.
You get one extra, 1 - X and 0.  Also you don't have to specify As Test in
the ReDim it uses it by Default.  I tested the code you had and it worked
fine.  Did you try using the UBound Function to check to see what your
highest subscript is to make sure that the array is redimensioning the way
you think?  Hope this helps.

                                                     M.D.



Fri, 11 Dec 1998 03:00:00 GMT  
 Dim and ReDim?

Quote:


> > In a procedure i have to change the size, i coded like this:

> > Sub Command1_click()
> >         Counter=Counter+1
> >         ReDim TestArray(Counter) As Test
> >         TestArray(Counter).x=1
> >         TestArray(Counter.y="a"
> > end Sub

> > When i run this i get the error Subscipt Out of Range on the line
> > where if want to fill the array (TestArray(Counter).x=1).

> Remember that when you Dimension something to X the subscripts go from 0 to X-1.  In the
> above case the reference to TestArray(Counter) will always be one higher than the highest
> available subscript!
> Randy Riesenberg


This is incorrect. When you Dimension something as X, e.g., Dim A(10) As Integer, valid
subscripts are 0 to X (10 in this case) - not X-1.

The following code works:

Defintion in a code module:
Type Test
    A As String
    B As Integer
End Type

Definition in a command button:
Private Sub Command1_Click()
Dim X() As Test, K As Integer
  K = 10
  Beep
  ReDim X(K)
  X(K).A = "test"
  X(K).B = 12
  Beep
End Sub

William Burrows

University of Washington



Fri, 11 Dec 1998 03:00:00 GMT  
 Dim and ReDim?

Quote:
>in a module i have made a userdefined type:

>Type Test
>    x as Integer
>    y as String
>End Type

>in the general Declarations section i have initialized the array:
>'just according to the help-files and examples

>Dim TestArray() As Test

>'and a variable i need later

>Dim Counter

>So now i have an array of zero and i can use ReDim to change the size.

>In the Form_load event i set the Counter to zero:

>Counter=0

>In a procedure i have to change the size, i coded like this:

>Sub Command1_click()
>    Counter=Counter+1
>    ReDim TestArray(Counter) As Test        
>    TestArray(Counter).x=1
>    TestArray(Counter.y="a"
>end Sub

>When i run this i get the error Subscipt Out of Range on the line
>where if want to fill the array (TestArray(Counter).x=1).

Change

        ReDim TestArray(Counter) As Test        

to:

        ReDim Preserve TestArray(Counter)

and it should work.

And someone else said:

Quote:
>Remember that when you Dimension something to X the subscripts
>go from 0 to X-1.

In C, yes, but not in Visual Basic!  The subscripts range from 1 to
the specified number (unless you specify otherwise with the Option
Base statement or by explicity specifying the bounds).

-- Roger
 * Give Kids the World: http://www.magicnet.net/~tudlp/gktw.html



Fri, 11 Dec 1998 03:00:00 GMT  
 Dim and ReDim?

Quote:
> And someone else said:

> >Remember that when you Dimension something to X the subscripts
> >go from 0 to X-1.

> In C, yes, but not in Visual Basic!  The subscripts range from 1 to
> the specified number (unless you specify otherwise with the Option
> Base statement or by explicity specifying the bounds).

You are right...boy you learn something new every day... :)
Randy



Sat, 12 Dec 1998 03:00:00 GMT  
 Dim and ReDim?


Quote:
(Roger Smith) writes:
>>in a module i have made a userdefined type:

>>Type Test
>>        x as Integer
>>        y as String
>>End Type

>>in the general Declarations section i have initialized the array:
>>'just according to the help-files and examples

>>Dim TestArray() As Test

>>'and a variable i need later

>>Dim Counter

>>So now i have an array of zero and i can use ReDim to change the size.

>>In the Form_load event i set the Counter to zero:

>>Counter=0

>>In a procedure i have to change the size, i coded like this:

>>Sub Command1_click()
>>        Counter=Counter+1
>>        ReDim TestArray(Counter) As Test        
>>        TestArray(Counter).x=1
>>        TestArray(Counter.y="a"
>>end Sub

>>When i run this i get the error Subscipt Out of Range on the line
>>where if want to fill the array (TestArray(Counter).x=1).

>Change

>    ReDim TestArray(Counter) As Test        

>to:

>    ReDim Preserve TestArray(Counter)

>and it should work.

>And someone else said:

The only thing with using Preserve is that the array will retain any
information that it contained in the subscripts below Counter, so if this
is what you want then use Preserve, if this isn't then you shouldn't.
Hope this helps.

                                                                   M.D.



Sat, 12 Dec 1998 03:00:00 GMT  
 Dim and ReDim?

Quote:

>what's going wrong?
>in a module i have made a userdefined type:
>Type Test
>    x as Integer
>    y as String
>End Type
>in the general Declarations section i have initialized the array:
>'just according to the help-files and examples
>Dim TestArray() As Test
>'and a variable i need later
>Dim Counter
>So now i have an array of zero and i can use ReDim to change the size.
>In the Form_load event i set the Counter to zero:
>Counter=0
>In a procedure i have to change the size, i coded like this:
>Sub Command1_click()
>    Counter=Counter+1
>    ReDim TestArray(Counter) As Test        
>    TestArray(Counter).x=1
>    TestArray(Counter.y="a"
>end Sub
>When i run this i get the error Subscipt Out of Range on the line
>where if want to fill the array (TestArray(Counter).x=1).
>So i tryed an search but it will not work just as it just to be.
>The only way it works is when i give the array a size:
>Dim TestArray(5) As Test
>Then it works fine but thats not what i want.
>So if anyone can help????
>thanks in advance
>gr. walther
>------------------- cut here ---------------
>Walther Musch
>Tilburg University Holland

Try ...

Sub Command1_click()
        Counter=Counter+1
        ReDim Preserve TestArray(Counter)
        TestArray(Counter).x=1
        TestArray(Counter).y="a"
end Sub

Claude



Sun, 13 Dec 1998 03:00:00 GMT  
 Dim and ReDim?

Quote:

>>what's going wrong?
>>in a module i have made a userdefined type:
>>Type Test
>>        x as Integer
>>        y as String
>>End Type
>>in the general Declarations section i have initialized the array:
>>'just according to the help-files and examples
>>Dim TestArray() As Test
>>'and a variable i need later
>>Dim Counter
>>So now i have an array of zero and i can use ReDim to change the size.
>>In the Form_load event i set the Counter to zero:
>>Counter=0
>>In a procedure i have to change the size, i coded like this:
>>Sub Command1_click()
>>        Counter=Counter+1
>>        ReDim TestArray(Counter) As Test        
>>        TestArray(Counter).x=1
>>        TestArray(Counter.y="a"
>>end Sub
>>When i run this i get the error Subscipt Out of Range on the line
>>where if want to fill the array (TestArray(Counter).x=1).
>>So i tryed an search but it will not work just as it just to be.
>>The only way it works is when i give the array a size:
>>Dim TestArray(5) As Test
>>Then it works fine but thats not what i want.
>>So if anyone can help????
>>thanks in advance
>>gr. walther
>>------------------- cut here ---------------
>>Walther Musch
>>Tilburg University Holland
>Try ...
>Sub Command1_click()
>    Counter=Counter+1
>    ReDim Preserve TestArray(Counter)
>    TestArray(Counter).x=1
>    TestArray(Counter).y="a"
>end Sub
>Claude

Actually the dimming should probably read

Redim Preserve TestArray(1 to Counter)
Claude



Sun, 13 Dec 1998 03:00:00 GMT  
 Dim and ReDim?
Jim's right of course. If you declare array(15) and test the upper bound
you will get 15 regardless of your Options Base setting. To save wasting
bytes and for safety I always declare both bounds ie. array(0 to 14).

--
Stuart Mitchell -- Principal Analyst/Programmer
*** Aquarius Itech  "New Age Solutions, Age Old Service" ***

Brisbane Australia   ph +61 (0)7 3841 1339 fax +61 (0)7 3841 1909



Wed, 16 Dec 1998 03:00:00 GMT  
 
 [ 11 post ] 

 Relevant Pages 

1. Dim and ReDim

2. Dim and ReDim

3. dim and redim allocate memory?

4. Efficient use of strings (Dim, Redim, and all that)

5. ReDim a 2 dim. array

6. dim string to dim form

7. Newbie Question: Dim A() as String /Dim A as String()

8. Dim obj As New Class crt Dim Obj As Class = New Class

9. dynamically dim a variable ie dim "variable"&n

10. dim myObject as Object versus dim myObject as myProject.class1

11. dim myObject as Object versus dim myObject as myProject.class1

12. clarify understanding on redim statement with dynamic arrays

 

 
Powered by phpBB® Forum Software