Kellan Dietrich
Kellan Dietrich

Reputation: 15

How to find the maximum and minimum numbers in a dynamic array through a button event

I'm trying to get buttons btnMaximum and btnMinimum to display the correct Max and Min numbers to a label. Each button is a separate button and when clicked will display either the max or min number.

I've got the array going but I dont know the proper functions for finding a Max and a Min.

Here is what I have so far:

Public Class Form1

Private Sub btnGenerate_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnGenerate.Click
    Dim intNumbers(-1) As Integer
    Dim intCounter As Integer
    ReDim intNumbers(14)

    For intCounter = 0 To intNumbers.Length - 1
        intNumbers(intCounter) = Int((100 - 1 + 1) * Rnd()) + 1
        Me.lstNumbers.Items.Add(intNumbers(intCounter))
    Next
    ReDim Preserve intNumbers(14)
    lstNumbers.Items.Clear()
    For intCounter = 0 To intNumbers.Length - 1
        Me.lstNumbers.Items.Add(intNumbers(intCounter))
    Next


End Sub

Private Sub btnMaximum_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnMaximum.Click


End Sub

Private Sub btnMinimum_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnMinimum.Click

End Sub
End Class

I would really appreciate the help.

Upvotes: 0

Views: 1822

Answers (1)

RianBattle
RianBattle

Reputation: 898

Firstly, you will need to move the declaration of your integer array outside of the click event for btnGenerate. This will allow you to access the array itself outside of that particular function (IE: inside your min and max button functions).

Arrays have two handy functions, Min and Max.

Dim intNumbers() As Integer = New Integer() {0, 1, 2, 3, 4, 5}
Dim intMin As Integer = intNumbers.Min          'will contain value 0
Dim intMax As Integer = intNumbers.Max          'will contain value 5

Upvotes: 3

Related Questions