VB.NET School Assignment? Project Due Tomorrow? Chat LIVE With A Programming Expert!

Welcome to Dream.In.Code
Become a VB.NET Expert!

Join 307,214 VB.NET Programmers for FREE! Get instant access to thousands of VB.NET experts, tutorials, code snippets, and more! There are 1,558 people online right now. Registration is fast and FREE... Join Now!




Help on how to determine 1st, 2nd, 3rd place by time

 

Help on how to determine 1st, 2nd, 3rd place by time

Boyet728

4 Nov, 2009 - 07:37 AM
Post #1

New D.I.C Head
*

Joined: 22 Mar, 2008
Posts: 6


My Contributions
Hi everyone,
I am having hard time determining how to get 1st, 2nd, and 3rd place determined by time. Could someone please point me in the right direction. I was thinking about bubble sort through an array, but since I am determining lowest to highest based on numbers = runner it just sounds more confusing. I admit this is an assignment, I dont need the code just an advice of what am I doing wrong. The code to determine second is not that efficient, I know there is an easier way. That is the part where I am stuck. crazy.gif

CODE

Public Class Form1

    Private Sub btnCalculate_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCalculate.Click

        'Local variable declaration
        Dim num1 As Integer = 0
        Dim num2 As Integer = 0
        Dim num3 As Integer = 0

        If runnerTextBox1.Text = "" Or runnerTextBox2.Text = "" Or runnerTextBox3.Text = "" Then
            MessageBox.Show("Please do not leave a field blank, please enter a name.", Application.ProductName, _
                            MessageBoxButtons.OK, MessageBoxIcon.Information)
            If runnerTextBox1.Text = "" Then
                runnerTextBox1.Focus()
                Exit Sub
                If runnerTextBox2.Text = "" Then
                    runnerTextBox2.Focus()
                    Exit Sub
                    If runnerTextBox3.Text = "" Then
                        runnerTextBox3.Focus()
                        Exit Sub
                    End If
                End If
            End If
            Exit Sub
        End If

        If timeTextBox1.Text = "" Or timeTextBox2.Text = "" Or timeTextBox3.Text = "" Then
            MessageBox.Show("Please do not leave a field blank, please enter finishing time.", Application.ProductName, _
                MessageBoxButtons.OK, MessageBoxIcon.Information)
            If timeTextBox1.Text = "" Then
                timeTextBox1.Focus()
                Exit Sub
                If timeTextBox2.Text = "" Then
                    timeTextBox2.Focus()
                    Exit Sub
                    If timeTextBox3.Text = "" Then
                        timeTextBox3.Focus()
                        Exit Sub
                    End If
                End If
            End If
            Exit Sub
        End If
        'Initializing variable
        num1 = Integer.Parse(timeTextBox1.Text)
        num2 = Integer.Parse(timeTextBox2.Text)
        num3 = Integer.Parse(timeTextBox3.Text)

        'If statement to determine who's first
        If num1 < num2 And num1 < num3 Then
            lbl_output_1st.Text = runnerTextBox1.Text
        ElseIf num1 > num2 And num3 > num2 Then
            lbl_output_1st.Text = runnerTextBox2.Text
        ElseIf num1 > num3 And num2 > num3 Then
            lbl_output_1st.Text = runnerTextBox3.Text
        End If

        'If statement to determine who's second
        If num1 < num2 And num1 > num3 Then
            lbl_output_2nd.Text = runnerTextBox1.Text
        ElseIf num2 < num3 And num2 > num1 And num1 < num3 Then
            lbl_output_2nd.Text = runnerTextBox2.Text
        ElseIf num2 < num3 And num1 > num2 And num3 < num1 Then
            lbl_output_2nd.Text = runnerTextBox3.Text
        End If

        'If statement to determine who's third
        If num3 < num1 And num2 < num1 Then
            lbl_output_3rd.Text = runnerTextBox1.Text
        ElseIf num2 < num3 And num2 > num1 Then
            lbl_output_3rd.Text = runnerTextBox2.Text
        ElseIf num3 < num2 And num3 > num1 Then
            lbl_output_3rd.Text = runnerTextBox3.Text
        End If
    End Sub

End Class


User is offlineProfile CardPM
+Quote Post


Boyet728

RE: Help On How To Determine 1st, 2nd, 3rd Place By Time

4 Nov, 2009 - 08:10 AM
Post #2

New D.I.C Head
*

Joined: 22 Mar, 2008
Posts: 6


My Contributions
Don't worry about it I got it. Just had to step away from it and think. I saw alot of if statements that were not right. Here is what I did.

CODE

        'If statement to determine who's first
        If num1 < num2 And num1 < num3 Then
            lbl_output_1st.Text = runnerTextBox1.Text
        ElseIf num1 > num2 And num3 > num2 Then
            lbl_output_1st.Text = runnerTextBox2.Text
        ElseIf num1 > num3 And num2 > num3 Then
            lbl_output_1st.Text = runnerTextBox3.Text
        End If

        'If statement to determine who's second
        If num1 < num2 And num1 > num3 Or num1 < num3 And num1 > num2 Then
            lbl_output_2nd.Text = runnerTextBox1.Text
        ElseIf num2 < num3 And num2 > num1 Or num2 < num1 And num2 > num3 Then
            lbl_output_2nd.Text = runnerTextBox2.Text
        ElseIf num3 < num1 And num3 > num2 Or num3 < num2 And num3 > num1 Then
            lbl_output_2nd.Text = runnerTextBox3.Text
        End If

        'If statement to determine who's third
        If num1 > num2 And num1 > num3 Then
            lbl_output_3rd.Text = runnerTextBox1.Text
        ElseIf num2 > num1 And num2 > num3 Then
            lbl_output_3rd.Text = runnerTextBox2.Text
        ElseIf num3 > num1 And num3 > num2 Then
            lbl_output_3rd.Text = runnerTextBox3.Text
        End If


Although I do have another problem, The text box focus would go through the first if statement and if that box is empty it focus on it. If there is an input it should go to the next elseif statement, but if the second text box is empty it will not focus on the second text box, or the third text box if its not empty. I am looking at the code and it seems right, any suggestions?


This post has been edited by Boyet728: 4 Nov, 2009 - 08:20 AM
User is offlineProfile CardPM
+Quote Post

Metitron

RE: Help On How To Determine 1st, 2nd, 3rd Place By Time

4 Nov, 2009 - 08:48 AM
Post #3

New D.I.C Head
*

Joined: 13 Oct, 2009
Posts: 33



Thanked: 2 times
My Contributions
the reason for this problem is that you only send into that if statment on the click event so if tb1 is empty when the button is clicked then it stops and is never sent back into the statement. looks like you may be able to do this with some kind of loop but it would be messy just easier to have it check each button click and if empty then that box gets focus.
User is offlineProfile CardPM
+Quote Post

mark.bottomley

RE: Help On How To Determine 1st, 2nd, 3rd Place By Time

4 Nov, 2009 - 09:14 AM
Post #4

D.I.C Addict
****

Joined: 22 Apr, 2009
Posts: 868



Thanked: 148 times
My Contributions
Modularize to make it more comprehensible - I may be using things beyond what you current have been taught (e.g. arrays and ByRef parameters).
CODE

Public Class Form1

    ' returns true if empty
    Private Function checkName(ByRef tb As TextBox) As Boolean
        If (tb.Text = String.Empty) Then
            tb.Focus()
            MessageBox.Show("All Names and times must be filled in.")
            Return True
        End If
        Return False
    End Function

    ' returns true if empty
    Private Function checkTime(ByRef tb As TextBox, ByRef time As Integer) As Boolean
        Dim result As Boolean = checkName(tb)
        If Not result Then
            time = Integer.Parse(tb.Text)
            result = False
        End If
        Return result
    End Function

    Private Sub Swap(ByRef str() As String, ByRef num() As Integer, ByVal index As Integer)
        Dim iTemp As Integer
        Dim sTemp As String
        sTemp = str(index)
        str(index) = str(index + 1)
        str(index + 1) = sTemp
        iTemp = num(index)
        num(index) = num(index + 1)
        num(index + 1) = iTemp
    End Sub

    Private Sub btnCalculate_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCalculate.Click

        'Local variable declaration
        Dim times(3) As Integer
        Dim names(3) As String

        If checkName(runnerTextBox1) OrElse checkName(runnerTextBox2) OrElse checkName(runnerTextBox3) Then
            Exit Sub
        End If

        If checkTime(timeTextBox1, times(0)) OrElse checkTime(timeTextBox2, times(1)) OrElse checkTime(timeTextBox3, times(2)) Then
            Exit Sub
        End If

        names(0) = runnerTextBox1.Text
        names(1) = runnerTextBox2.Text
        names(2) = runnerTextBox3.Text

        ' Bubble sort based on the times - swap the names as well
        For i As Integer = 0 To 1
            For j As Integer = 0 To 1 - i
                If times(j) > times(j + 1) Then
                    Swap(names, times, j)
                End If
            Next
        Next

        lbl_output_1st.Text = names(0)
        lbl_output_2nd.Text = names(1)
        lbl_output_3rd.Text = names(2)
    End Sub

End Class

Any time you are typing the same thing (or almost the same thing againa, there is likely a chance for a subroutine or function to improve your code modularity and clarity.

p.s. the OrElse logic is what is known as short-circuit logic e.g. an Or is true whenever any of the componenets is true, so you don't need to keep checking condition once you find a true one.

This post has been edited by mark.bottomley: 4 Nov, 2009 - 09:17 AM
User is offlineProfile CardPM
+Quote Post

Boyet728

RE: Help On How To Determine 1st, 2nd, 3rd Place By Time

4 Nov, 2009 - 09:24 AM
Post #5

New D.I.C Head
*

Joined: 22 Mar, 2008
Posts: 6


My Contributions
QUOTE(Metitron @ 4 Nov, 2009 - 08:48 AM) *

the reason for this problem is that you only send into that if statment on the click event so if tb1 is empty when the button is clicked then it stops and is never sent back into the statement. looks like you may be able to do this with some kind of loop but it would be messy just easier to have it check each button click and if empty then that box gets focus.


But if the "if" statement is false it should jump to the next if statement. If the condition is true then a message box appears and then it will exit out of the sub, making the user enter an input in the field and click on the button again, going through the validation again. But you are right so I changed it to look at the first two If statements then added elseif after the first if statement for the focus function. Changed code, now it works.
CODE



        If runnerTextBox1.Text = "" Or runnerTextBox2.Text = "" Or runnerTextBox3.Text = "" Then
            MessageBox.Show("Please do not leave a field blank, please enter a name.", Application.ProductName, _
                            MessageBoxButtons.OK, MessageBoxIcon.Information)
            If runnerTextBox1.Text = "" Then
                runnerTextBox1.Focus()
            ElseIf runnerTextBox2.Text = "" Then
                runnerTextBox2.Focus()
            ElseIf runnerTextBox3.Text = "" Then
                runnerTextBox3.Focus()
            End If
        Exit Sub
        End If

        If timeTextBox1.Text = "" Or timeTextBox2.Text = "" Or timeTextBox3.Text = "" Then
            MessageBox.Show("Please do not leave a field blank, please enter finishing time.", Application.ProductName, _
                MessageBoxButtons.OK, MessageBoxIcon.Information)
            If timeTextBox1.Text = String.Empty Then
                timeTextBox1.Focus()
            ElseIf timeTextBox2.Text = String.Empty Then
                timeTextBox2.Focus()
            ElseIf timeTextBox3.Text = String.Empty Then
                timeTextBox3.Focus()
            End If
            Exit Sub
        End If



And Mark, I did think about modularization, but this is still a little vague to me. I can do it in JAVA but VB.Net is still a learning process for me. Thanks all for helping me out.

This post has been edited by Boyet728: 4 Nov, 2009 - 09:56 AM
User is offlineProfile CardPM
+Quote Post

Fast ReplyReply to this topicStart new topic

Time is now: 11/21/09 10:01PM

Live VB.NET Help!

Be Social

Dream.In.Code RSS Feed Dream.In.Code LinkedIn Group Follow Us On Twitter Fan Us On Facebook

VB.NET Tutorials

Reference Sheets

VB.NET Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month