James
James

Reputation: 23

vb.Net Checking to see what are the two Largest Numbers

I have 4 text box that accepts numbers from user and i want to check for what are the two largest numbers entered, can this be done using if then else statements

Dim Big_num_1 As Integer
Dim Big_num_2 As Integer

'Dim txtbox_1 As Integer
'Dim txtbox_2 As Integer
'Dim txtbox_3 As Integer
'Dim txtbox_4 As Integer

Private Sub btnShow2BigNum_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnShow2BigNum.Click  
    'if then else statements
End Sub

i am a beginer in Vb.net and i would really appreciate any help that i can get If possible i would like a If then else statement solution for this

Upvotes: 0

Views: 208

Answers (3)

Luke
Luke

Reputation: 848

This can be done with an if-statement like this..

Dim Big_num_1 As Integer = 0
Dim Big_num_2 As Integer = 0

'Dim txtbox_1 As Integer
'Dim txtbox_2 As Integer
'Dim txtbox_3 As Integer
'Dim txtbox_4 As Integer

Private Sub btnShow2BigNum_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnShow2BigNum.Click  
    Dim numList(4) as Integer
    numList(0) = txtbox_1
    numList(1) = txtbox_2
    numList(2) = txtbox_3
    numList(3) = txtbox_4
    For x as Integer = 0 to numList.count - 1
        If numList(x) > Big_num_1 Then
            Big_num_2 = Big_num_1
            Big_num_1 = numList(x)
        Else If numList(x) > Big_num_2 Then
            Big_num_2 = numList(x)
        End If
    Next    
    
End Sub

Upvotes: 1

Joel Coehoorn
Joel Coehoorn

Reputation: 415971

You could use If/Else statements, but that would be the hard way to do this:

Private Sub btnShow2BigNum_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnShow2BigNum.Click
    Dim Numbers = { TextBox1.Text, TextBox2.Text, TextBox3.Text, TextBox4.Text }
    Dim results = Numbers.Select(Function(s) Integer.Parse(s)).OrderByDescending().Take(2).ToList()

    Big_num_1 = results(0)
    Big_num_2 = results(1)
End Sub

Upvotes: 0

SiriSch
SiriSch

Reputation: 190

Private Sub Docheck()
    Dim Numbers As String() = {TextBox1.Text, TextBox2.Text, TextBox3.Text}
    Dim Ordered As String() = Numbers.OrderByDescending(Function(x) CInt(x)).ToArray
    Dim Highest As Integer = CInt(Ordered(0))
    Dim SecondHighest As Integer = CInt(Ordered(1))
    MessageBox.Show(String.Concat(Highest , "  " , SecondHighest))
End Sub

And in your button click event you call

Docheck()

Upvotes: 0

Related Questions