|
-
Sep 22nd, 2005, 07:01 AM
#1
Thread Starter
Addicted Member
subscript out of range.... what am i doing wrong?
VB Code:
Option Explicit
Dim Numbers() As Integer
Dim Index As Integer
Private Sub cmdAddToArray_Click()
Dim Number As Integer
If Index = 0 Then
ReDim Numbers(0)
Else
ReDim Preserve Numbers(UBound(Numbers) + 1)
End If
Index = Index + 1
Number = txtNumber.Text
txtNumber.Text = ""
txtNumber.SetFocus
End Sub
Private Sub CmdDisplay_Click()
Dim Element As Integer
lstNumbers.Clear
For Element = 0 To Index
lstNumbers.AddItem Numbers(Element)
Next Element
End Sub
Private Sub cmdFindNumber_Click()
Dim Element As Integer
Dim Found As Boolean
Dim SearchNumber As Integer
Element = 1
Found = False
SearchNumber = txtSearchNumber.Text
Do While (Found = False) And (Element <= Index)
If Numbers(Element) = SearchNumber Then
Found = True
Else
Element = Element + 1
End If
Loop
If Found Then
lblDisplaySearch.Caption = "This number IS in the array"
Else
lblDisplaySearch.Caption = "This number is NOT in the array"
End If
End Sub
hey guys, i have this code but whenever i click "display", which shows the numbers i entered into the array i get a "subscript out of range" error on the 'lstNumbers.AddItem Numbers(Element)' line.
can you see whats wrong?
thanks
-
Sep 22nd, 2005, 07:06 AM
#2
Re: subscript out of range.... what am i doing wrong?
Please, indent your code ... 
I presume you have populated the array first before you click Display. I'm not sure what your Index variable is for but you could just use UBound instead.
VB Code:
Private Sub CmdDisplay_Click()
Dim i As Long
lstNumbers.Clear
For i = 0 To UBound(Numbers)
lstNumbers.AddItem Numbers(i)
Next i
End Sub
-
Sep 22nd, 2005, 07:10 AM
#3
Thread Starter
Addicted Member
Re: subscript out of range.... what am i doing wrong?
Please, indent your code ...
lol sorry, will do.
i have populated the array. i tried your code and it stops the erro messagwe although all the numbers in the display are now zero...
-
Sep 22nd, 2005, 07:13 AM
#4
Re: subscript out of range.... what am i doing wrong?
That's because in your population routine, you don't actually assign values to any of the array elements.
Assigning a value to an element would be like this:
Since you haven't done that, all your numbers are zero.
-
Sep 22nd, 2005, 07:18 AM
#5
Thread Starter
Addicted Member
Re: subscript out of range.... what am i doing wrong?
i was trying to make an unlimited length dynamic array.
i had this line in it before
but i was getting the same subscript out of range error
-
Sep 22nd, 2005, 07:23 AM
#6
Re: subscript out of range.... what am i doing wrong?
here is some handy code to test if the array is empty or has data...
VB Code:
Private Declare Function SafeArrayGetDim Lib "oleaut32.dll" (ByRef saArray() As Any) As Long
Public Function ArrayIsFilled(ary() As String) As Boolean
If (SafeArrayGetDim(ary) > 0) Then
ArrayIsFilled = True
Else
ArrayIsFilled = False
End If
End Function
so you could change it to this:
VB Code:
If ArrayIsFilled(Numbers) Then
ReDim Preserve Numbers(UBound(Numbers) + 1)
Else
ReDim Numbers(0)
End If
and u dont have to track index anymore
JPnyc rocks!! (Just ask him!)
If u have your answer please go to the thread tools and click "Mark Thread Resolved"
-
Sep 22nd, 2005, 07:37 AM
#7
Thread Starter
Addicted Member
Re: subscript out of range.... what am i doing wrong?
i'm getting a type mismatch error now.
how would i assign the values i type in to an element?
-
Sep 22nd, 2005, 07:41 AM
#8
Re: subscript out of range.... what am i doing wrong?
type into a textbox?
you need to make sure that its a number and convert to a number
like:
if IsNumeric(Text1) then
number(x) = cint(text)
end if
JPnyc rocks!! (Just ask him!)
If u have your answer please go to the thread tools and click "Mark Thread Resolved"
-
Sep 22nd, 2005, 07:52 AM
#9
Thread Starter
Addicted Member
Re: subscript out of range.... what am i doing wrong?
lol i'm confused....
i populate my array by typing numbers into a textbox. i can do that. my problem now is that when i want to display what i have in my array it comes up as zero. from penagates' post i somehow have to assign a value to an element. can you shpow me how to do this? i don't understand what
numbers(x) = val means.
-
Sep 22nd, 2005, 08:41 AM
#10
Re: subscript out of range.... what am i doing wrong?
If Numbers is an array with 5 elements (0 through 4) and you want to assign a value to element 2, you would do
VB Code:
If IsNumber(Text1.Text) Then
Numbers(2) = CInt(Text1.Text)
End If
-
Sep 22nd, 2005, 08:51 AM
#11
Thread Starter
Addicted Member
Re: subscript out of range.... what am i doing wrong?
thanks guys. i think what i'm trying to do is to advanced for my knowledge of arrays. anything i seem to do i get the "subscript out of range error". i'm fed up!
thanks anyway
-
Sep 22nd, 2005, 08:49 AM
#12
Re: subscript out of range.... what am i doing wrong?
here is a whole example:
VB Code:
Private Declare Function SafeArrayGetDim Lib "oleaut32.dll" (ByRef saArray() As Any) As Long
Dim Numbers() As Integer
Public Function ArrayIsFilled(ary() As Integer) As Boolean
If (SafeArrayGetDim(ary) > 0) Then
ArrayIsFilled = True
Else
ArrayIsFilled = False
End If
End Function
Private Sub Command1_Click()
If IsNumeric(text1) Then
If ArrayIsFilled(Numbers()) Then
ReDim Preserve Numbers(UBound(Numbers) + 1)
Else
ReDim Numbers(0)
End If
Numbers(UBound(Numbers)) = CInt(text1)
End If
End Sub
JPnyc rocks!! (Just ask him!)
If u have your answer please go to the thread tools and click "Mark Thread Resolved"
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|