Question Array sorting

dhall1

Member
Joined
Jul 25, 2013
Messages
6
Programming Experience
Beginner
I have an application that looks at various dates and compares those dates to yet another date for a time lapse. The problem is that I cannot figure out how to codefor the following scenario:
There is a receive date and an application date. There may be multiple application dates, but only one received date. I need to compare the MOST RECENT application date against the received date to determine a time lapse of greater than 10 days. When there are multiple application dates, I get multiple 10 day warnings, and rightly so according to the code as it is now:

DIM datagriditem6 As DataGridItem


ForEach datagriditem6 InMe.grdEquipment.Items


Dim receviedDate AsDate = CDate(Me.txtDateRecieved.Text)


Dim AppliedDate AsDate = CDate(datagriditem6.Cells(3).Text)


If receviedDate < (AppliedDate.AddDays(11)) Then


Else


Me.lblErrors.Text = Me.lblErrors.Text + "<br/>* Date Recieved is past 10 days of applied date"


EndIf


Next

So, I thought the way to do this in order to have only the most recent application date as the date being compared against the received date is to sort the array (datagriditem6), then compare against the largest date value. I can't see how to do this, however. I know array.sort(datagriditem6), but then how would I do the comparison with the most recent date?
 
So, just to confirm, what you're saying is that, rather than looping through the grid and comparing every row, you want to get the largest (most recent) date from the grid and then compare just that. Where does the data in the grid come from, because that's most likely what you should be working with.
 
Correct, I need the most recent. Here is where the dates are coming from:

Protected Sub btnAddEquip_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnAddEquip.Click


'/check for grower applied check box or chemigation for required eqip tag


If Me.txtTimeAppl_1.Text = "" Or Me.txtTimeAppl_1.Text.Length < 4 Then


Me.lblEquipmentTagErrors.Text = "Time Applied is Required, check for correct time"

SetFocus(
Me.txtTimeAppl_1)


Exit Sub


Else


Try

DateTime.ParseExact(
Me.txtTimeAppl_1.Text, "HHmm", System.Globalization.DateTimeFormatInfo.CurrentInfo)


Me.lblEquipmentTagErrors.Text = ""


Catch ex As Exception


Me.lblEquipmentTagErrors.Text = "Check the time inTime Applied start...must be military time 0000-2400 "

SetFocus(
Me.txtTimeAppl_1)


Exit Sub


End Try


Me.lblEquipmentTagErrors.Text = ""


End If



If Me.txtTimeAppl_2.Text <> "" And Me.txtTimeAppl_2.Text.Length < 4 Then


Me.lblEquipmentTagErrors.Text = "Check for correct time in Time Applied END"

SetFocus(
Me.txtTimeAppl_2)


Exit Sub



If Me.txtTimeAppl_2.Text = "" Then


Try

DateTime.ParseExact(
Me.txtTimeAppl_2.Text, "HHmm", _

System.Globalization.DateTimeFormatInfo.CurrentInfo)


Me.lblEquipmentTagErrors.Text = ""


Catch ex As Exception


Me.lblEquipmentTagErrors.Text = _


"Check the time inTime Applied End...must be military time 0000-2400 "

SetFocus(
Me.txtTimeAppl_2)


Exit Sub


End Try


Me.lblEquipmentTagErrors.Text = ""


Else


End If


End If



If Me.txtAppliedDate1.Text = "" Then


Me.lblEquipmentTagErrors.Text = "Date Applied is Required"

SetFocus(
Me.txtAppliedDate1)


Exit Sub



Else


If Me.txtDateRecieved.Text = "" Then


Me.lblEquipmentTagErrors.Text = "Date we stamped it, Required - fill in the top section"


Exit Sub


End If



'compare date applied to recieved date must be less than 10 days


Dim receviedDate As Date = CDate(Me.txtDateRecieved.Text)


Dim AppliedDate As Date = CDate(Me.txtAppliedDate1.Text)


If receviedDate < (AppliedDate.AddDays(11)) Then


Else


Me.lblEquipmentTagErrors.Text = ""


Me.lblErrors.Text = Me.lblErrors.Text + "<br/>* Date Recieved is past 10 days of applied date"


'keep going but can only be saved for compliance


End If
 
Sorry but I'm not even going to try to read that code. Please code snippets copied directly from the IDE as plain text using xcode tags, i.e. [xcode=vb]your code here[/xcode]That makes them nice and readable without excessive line spacing and with indenting.
 
Sorry..try this:

Protected Sub btnAddEquip_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnAddEquip.Click
        '/check for grower applied check box or chemigation for required eqip tag
        If Me.txtTimeAppl_1.Text = "" Or Me.txtTimeAppl_1.Text.Length < 4 Then
            Me.lblEquipmentTagErrors.Text = "Time Applied is Required, check for correct time"
            SetFocus(Me.txtTimeAppl_1)
            Exit Sub
        Else
            Try
                DateTime.ParseExact(Me.txtTimeAppl_1.Text, "HHmm", System.Globalization.DateTimeFormatInfo.CurrentInfo)
                Me.lblEquipmentTagErrors.Text = ""
            Catch ex As Exception
                Me.lblEquipmentTagErrors.Text = "Check the time inTime Applied start...must be military time 0000-2400 "
                SetFocus(Me.txtTimeAppl_1)
                Exit Sub
            End Try
            Me.lblEquipmentTagErrors.Text = ""
        End If
        If Me.txtTimeAppl_2.Text <> "" And Me.txtTimeAppl_2.Text.Length < 4 Then
            Me.lblEquipmentTagErrors.Text = "Check for correct time in Time Applied END"
            SetFocus(Me.txtTimeAppl_2)
            Exit Sub
            If Me.txtTimeAppl_2.Text = "" Then
                Try
                    DateTime.ParseExact(Me.txtTimeAppl_2.Text, "HHmm", _
                                         System.Globalization.DateTimeFormatInfo.CurrentInfo)
                    Me.lblEquipmentTagErrors.Text = ""
                Catch ex As Exception
                    Me.lblEquipmentTagErrors.Text = _
                        "Check the time inTime Applied End...must be military time 0000-2400 "
                    SetFocus(Me.txtTimeAppl_2)
                    Exit Sub
                End Try
                Me.lblEquipmentTagErrors.Text = ""
            Else
            End If
        End If
        If Me.txtAppliedDate1.Text = "" Then
            Me.lblEquipmentTagErrors.Text = "Date Applied is Required"
            SetFocus(Me.txtAppliedDate1)
            Exit Sub
        Else
            If Me.txtDateRecieved.Text = "" Then
                Me.lblEquipmentTagErrors.Text = "Date we stamped it, Required - fill in the top section"
                Exit Sub
            End If
            'compare date applied to recieved date must be less than 10 days
            Dim receviedDate As Date = CDate(Me.txtDateRecieved.Text)
            Dim AppliedDate As Date = CDate(Me.txtAppliedDate1.Text)
            If receviedDate < (AppliedDate.AddDays(11)) Then
            Else
                Me.lblEquipmentTagErrors.Text = ""
                Me.lblErrors.Text = Me.lblErrors.Text + "<br/>* Date Recieved is past 10 days of applied date"
                'keep going but can only be saved for compliance
            End If
            If receviedDate.Year < AppliedDate.Year Then
                Me.lblEquipmentTagErrors.Text = "Date Applied Year is wrong"
                SetFocus(Me.txtAppliedDate1)
                Exit Sub
            End If
        End If
        Dim EquipTag As String = "Error"
        If Me.txtEquipTag1.Text = "" And ddlApplicationType.SelectedValue = "C" Then
            EquipTag = "Chemi"
        End If
        If Me.txtEquipTag1.Text = "" And Me.chkGrowerApplied.Checked = True Then
            EquipTag = "growe"
        End If
        If EquipTag <> "" Then
            EquipTag = Me.txtEquipTag1.Text
        End If
        'If EquipTag = "" And txtPGP_PCA_Num.Text <> "" Then
        '    Me.lblErrors.Text = "Equipment tag required with PCA, if you entered in PGP select grower applied checkbox, then Remove & re-enter."
        'End If
        Try
            'add equipmentTag to grid
            Dim myTable As DataTable = CType(Session("myEquipmentDataTable"), DataTable)
            Dim row As DataRow
            Dim count As Integer
            count = myTable.Rows.Count() + 1
            row = myTable.NewRow()
            row("Count") = count
            row("EquipmentTag1") = EquipTag
            row("TimeApplied") = Me.txtTimeAppl_1.Text
            row("TimeApplied2") = Me.txtTimeAppl_2.Text
            row("DateApplied") = Me.txtAppliedDate1.Text
            myTable.Rows.Add(row)
            BindgrdEquipment()
        Catch ex As Exception
            errorHandling.WriteToEventLog(ex.Message, "NewEntryAlt_addEquip", EventLogEntryType.Error, "1080_beta")
            Me.lblEquipmentTagErrors.Text = ex.Message
            Exit Sub
        End Try
        'clear fields for next entry
        Me.txtEquipTag1.Text = ""
        Me.txtTimeAppl_1.Text = ""
        Me.txtTimeAppl_2.Text = ""
        Me.txtAppliedDate1.Text = ""
        SetFocus(Me.txtEquipTag1)
    End Sub
 
Back
Top