Results 1 to 4 of 4

Thread: [RESOLVED] Displaying Highest Number in an Array

  1. #1

    Thread Starter
    Addicted Member oldmcgroin's Avatar
    Join Date
    Jul 2005
    Location
    Manchester, UK
    Posts
    182

    Resolved [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:
    1. Private Sub cmdHighest_Click()
    2. Dim Highest As Integer
    3. Dim Numbers(1 To 5) As Integer
    4.  
    5.  
    6. For Index = 1 To UBound(Numbers)
    7.        If Numbers(Index) > Highest Then
    8.        Highest = Numbers(Index)
    9.     End If
    10. Next Index
    11.  
    12.  
    13. lblHighest.Caption = Highest
    14.  
    15.  
    16. End Sub

  2. #2

    Thread Starter
    Addicted Member oldmcgroin's Avatar
    Join Date
    Jul 2005
    Location
    Manchester, UK
    Posts
    182

    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:
    1. Private Sub cmdHighest_Click()
    2. Dim Highest As Integer
    3. ReDim Preserve Numbers(1 To 5) As Integer
    4.  
    5.  
    6. For Index = 1 To UBound(Numbers)
    7.     If Numbers(Index) > Highest Then
    8.     Highest = Numbers(Index)
    9.   End If
    10. Next Index
    11.  
    12.  
    13. lblHighest.Caption = Highest
    14.  
    15.  
    16. End Sub

  3. #3
    I'm about to be a PowerPoster!
    Join Date
    Jan 2005
    Location
    Everywhere
    Posts
    13,647

    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.

  4. #4

    Thread Starter
    Addicted Member oldmcgroin's Avatar
    Join Date
    Jul 2005
    Location
    Manchester, UK
    Posts
    182

    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
  •  



Click Here to Expand Forum to Full Width