Out of subscript range error! 
Author Message
 Out of subscript range error!

        I am having a problem working with arrays. I keep getting an out of
range subscript error. Here is an example of what I am trying to
accomplish.

        listbox1.additem "this"
        listbox1.additem "is"
        listbox1.additem "test"

        for i = 1 to listbox1.listcount
            myArray(i) = listbox1.list(i)
        next

I would appreciate any help!

                Thank You



Sun, 21 Mar 1999 03:00:00 GMT  
 Out of subscript range error!

Quote:

>    I am having a problem working with arrays. I keep getting an out of
>range subscript error. Here is an example of what I am trying to
>accomplish.
>    listbox1.additem "this"
>    listbox1.additem "is"
>    listbox1.additem "test"
>    for i = 1 to listbox1.listcount
>        myArray(i) = listbox1.list(i)
>    next
>I would appreciate any help!

>            Thank You

From the On line Help under listcount:

ListCount is always one more than the largest ListIndex value.
This should solve your problem.



Sun, 21 Mar 1999 03:00:00 GMT  
 Out of subscript range error!

Quote:


> >       I am having a problem working with arrays. I keep getting an out of
> >range subscript error. Here is an example of what I am trying to
> >accomplish.

> >       listbox1.additem "this"
> >       listbox1.additem "is"
> >       listbox1.additem "test"

> >       for i = 1 to listbox1.listcount
> >           myArray(i) = listbox1.list(i)
> >       next

> >I would appreciate any help!

> >               Thank You

> From the On line Help under listcount:

> ListCount is always one more than the largest ListIndex value.
> This should solve your problem.

Jim,

You should also concider giving the reason for this.
The reason that the ListCount is 1 greater than ListIndex is
because ListIndex Starts at 0 not 1.

Gary



Sun, 21 Mar 1999 03:00:00 GMT  
 Out of subscript range error!

Quote:



>> >       I am having a problem working with arrays. I keep getting an out of
>> >range subscript error. Here is an example of what I am trying to
>> >accomplish.

>> >       listbox1.additem "this"
>> >       listbox1.additem "is"
>> >       listbox1.additem "test"

>> >       for i = 1 to listbox1.listcount
>> >           myArray(i) = listbox1.list(i)
>> >       next

>> >I would appreciate any help!

>> >               Thank You

>> From the On line Help under listcount:

>> ListCount is always one more than the largest ListIndex value.
>> This should solve your problem.
>Jim,
>You should also concider giving the reason for this.
>The reason that the ListCount is 1 greater than ListIndex is
>because ListIndex Starts at 0 not 1.
>Gary

Gary:

You are correct. I meant to do a code snippet and got into a hurry and
forgot.

 for i = 0 to listbox1.listcount -1
         myArray(i) = listbox1.list(i)
 next

Be sure to dim myArray accordingly too....



Sun, 21 Mar 1999 03:00:00 GMT  
 Out of subscript range error!


Quote:
(Jim Huguley) writes:
>Subject:    Re: Out of subscript range error!

>Date:       Wed, 02 Oct 1996 14:06:03 GMT


>>        I am having a problem working with arrays. I keep getting an out
of
>>range subscript error. Here is an example of what I am trying to
>>accomplish.

>>        listbox1.additem "this"
>>        listbox1.additem "is"
>>        listbox1.additem "test"

>>        for i = 1 to listbox1.listcount
>>            myArray(i) = listbox1.list(i)
>>        next

>>I would appreciate any help!

>>                Thank You

If a MYLIST is a list box with 4 values then:

MYLIST.LIST(0) = val1
MYLIST.LIST(1) = val2
MYLIST.LIST(2) = val3
MYLIST.LIST(3) = val4

MYLIST.LISTCOUNT = 4, but MYLIST.LIST(4) = undefined

M{*filter*}of this story:  VB indexes just about everything from zero, not one.



Sun, 21 Mar 1999 03:00:00 GMT  
 Out of subscript range error!


Quote:
>    I am having a problem working with arrays. I keep getting an out of
> range subscript error. Here is an example of what I am trying to
> accomplish.

>    listbox1.additem "this"
>    listbox1.additem "is"
>    listbox1.additem "test"

>    for i = 1 to listbox1.listcount
>        myArray(i) = listbox1.list(i)
>    next

        try an offset like this:

        for i = 1 to listbox.listcount
            myArray(i - 1) = listbox1.list(i - 1)
        next  '  i

        remember all arrays start at subscript 0 (default)
        unless the "Option Base 1" compiler directive
        statement is used.  Now go build something?!?  ;-)

Regards,

The Doktor

--
"Remember - you can pick your nose and you can pick your    friends,  but
you can't wipe your friends off on the end of the  couch!"



Mon, 22 Mar 1999 03:00:00 GMT  
 Out of subscript range error!

Listbox.Item is 0-based.  You need to vary your loop variable from
0 to Count - 1, not 1 - Count.

Arrays are, by default 0-based as well.  eg "Dim Array(10) as ..." gives
you
access to Array(0), Array(1)...Array(9).   You can make it 1 based by
either:
Dim Array(1 to 10) as ...
   or
In the declarations section,
Option Base 1
--
Don Brown



Quote:

> >       I am having a problem working with arrays. I keep getting an out of
> >range subscript error. Here is an example of what I am trying to
> >accomplish.

> >       listbox1.additem "this"
> >       listbox1.additem "is"
> >       listbox1.additem "test"

> >       for i = 1 to listbox1.listcount
> >           myArray(i) = listbox1.list(i)
> >       next

> >I would appreciate any help!

> >               Thank You



Mon, 22 Mar 1999 03:00:00 GMT  
 Out of subscript range error!

Also, the first entry in the list is "0", not "1".
If your problem is still with arrays the make sure you use the Redim
Preserve to up the size when needed.

Shelly



Quote:

> >       I am having a problem working with arrays. I keep getting an out of
> >range subscript error. Here is an example of what I am trying to
> >accomplish.

> >       listbox1.additem "this"
> >       listbox1.additem "is"
> >       listbox1.additem "test"

> >       for i = 1 to listbox1.listcount
> >           myArray(i) = listbox1.list(i)
> >       next

> >I would appreciate any help!

> >               Thank You

> From the On line Help under listcount:

> ListCount is always one more than the largest ListIndex value.
> This should solve your problem.



Mon, 22 Mar 1999 03:00:00 GMT  
 Out of subscript range error!


Quote:

>        I am having a problem working with arrays. I keep getting an
out of
>range subscript error. Here is an example of what I am trying to
>accomplish.

>        listbox1.additem "this"
>        listbox1.additem "is"
>        listbox1.additem "test"

>        for i = 1 to listbox1.listcount
>            myArray(i) = listbox1.list(i)
>        next

No good... Arrays start at 0 and end at ListCount - 1
What you should write is :

        for i = 0 to listbox1.listcount - 1
                ...

See ya,

                Chris



Sat, 27 Mar 1999 03:00:00 GMT  
 
 [ 9 post ] 

 Relevant Pages 

1. Subscript out of range error

2. Subscript out of range error

3. Subscript out of range error

4. AddShapeRecordDetected Error: Subscript out of range

5. Defining an array and getting Subscript Out of Range Error

6. help with subscript out of range error

7. Problems With FlexGrid And Subscript Out Of Range Error

8. runtime error 9, subscript out of range

9. Subscript out of range (Error 9)

10. Redim causes Subscript Out of Range (Error 9)

11. Subscript out of range error problem

12. Help! Error # 9 - Subscript out of range

 

 
Powered by phpBB® Forum Software