|
-
Sep 22nd, 2005, 11:35 AM
#1
Thread Starter
Addicted Member
[RESOLVED] Displaying Highest Number in an Array
hey guys, me again. attempt number 2 on trying to get to the bottom of these arrays. i'm trying to display the highest number in an array and i am using the following code. can anyone tell me why it keeps giving me a result of zero. i have been searching through the forum and this was basically what i came out with.
(I can fill the array as i have a button that displays the numbers inputted into the array)
VB Code:
Private Sub cmdHighest_Click()
Dim Highest As Integer
Dim Numbers(1 To 5) As Integer
For Index = 1 To UBound(Numbers)
If Numbers(Index) > Highest Then
Highest = Numbers(Index)
End If
Next Index
lblHighest.Caption = Highest
End Sub
-
Sep 22nd, 2005, 11:44 AM
#2
Thread Starter
Addicted Member
Re: Displaying Highest Number in an Array
I've worked it out. Hurrah.
I had to do ReDim Preserve Numbers(1 to 5)
VB Code:
Private Sub cmdHighest_Click()
Dim Highest As Integer
ReDim Preserve Numbers(1 To 5) As Integer
For Index = 1 To UBound(Numbers)
If Numbers(Index) > Highest Then
Highest = Numbers(Index)
End If
Next Index
lblHighest.Caption = Highest
End Sub
-
Sep 22nd, 2005, 11:48 PM
#3
Re: [RESOLVED] Displaying Highest Number in an Array
If that code works it means you have already declared and populated your Numbers array in which case you should remove the Redim Preserve line altogether, as it is a rather expensive operation to perform.
-
Sep 23rd, 2005, 06:20 AM
#4
Thread Starter
Addicted Member
Re: [RESOLVED] Displaying Highest Number in an Array
if i remove it then it comes out as zero...
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
|