Skip to main content

Find minimum and maximum Number in Array using Function in VB.NET.

PROPERTIES:
Label1
TextEach Element Seprate With A Semicolon " , "
Button1
TextMinimum
Button2
TextMaximum
CODEING:
Public Class Form1
    Private Sub TextBox1_KeyPress(ByVal sender As System.Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles TextBox1.KeyPress
        If Asc(e.KeyChar) <> 8 Then //Restrict to enter a char
            If Not Asc(e.KeyChar) = 44 And Asc(e.KeyChar) < 48 Or Asc(e.KeyChar) > 57 Then
                e.Handled = True
            End If
        End If
    End Sub
    Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
        MsgBox("Minimum Number :" & search("min"))
    End Sub
    Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
        MsgBox("Maximum Number :" & search("max"))
    End Sub
    Function search(ByVal op As String) As Integer
        Dim c As Char = ","c
        Dim arry() As String = TextBox1.Text.Split(c)//Separate a data & assign string array
        Dim numArray(arry.Length - 1) As Integer
        For Each num In arry
            If Not num = "" Then
                numArray(i) = Val(num) //Convert string to int, after Assign Value
                i += 1
            End If
        Next
        If op = "min" Then
            Return numArray.Min
        Else
            Return numArray.Max
        End If
    End Function
End Class
DESIGNING:



Comments