jwcoleman87
Well-known member
- Joined
- Oct 4, 2014
- Messages
- 124
- Programming Experience
- Beginner
So, I finally managed to slap this bit of code together. I am using it to filter my Dataset. I simply want to show you the code and humbly ask if there are any suggestions to clean it up.
First off, the individual expressions:
So there are 4 individual filters, one for technicians, one for unit status, one for start times, and one for end times. So far these all work, (to the best of my knowledge, as I wrote the hour part at home, I'll get to test that tomorrow)
So these four functions are slapped together into a string sandwich in this function:
So ultimately I am wondering if there is any good way to clean all this code up, polish it and make it neat. I was wondering about the logicaloperator strings, and how I might be more efficient.
Ideas, criticisms, comments, questions welcome!
First off, the individual expressions:
Private Function TechnicianFilter() As String Dim LogicalOperator As String = " OR " Dim TechFilter As String = "" Dim FilterString As String = "" If clbTechnicians.CheckedItems.Count <= 1 Then LogicalOperator = "" End If Dim index As Integer = 0 For Each itemChecked In clbTechnicians.CheckedItems If index = clbTechnicians.CheckedItems.Count - 1 Then LogicalOperator = "" End If TechFilter = String.Format("TechnicianID = '{0}' {1}", itemChecked.ToString(), LogicalOperator) FilterString += TechFilter index += 1 Next If FilterString <> "" Then FilterString = "(" + FilterString + ")" End If Return FilterString End Function Private Function StatusFilter() As String Dim LogicalOperator As String = " OR " Dim StatFilter As String = "" Dim FilterString As String = "" If clbStatus.CheckedItems.Count <= 1 Then LogicalOperator = "" End If Dim index As Integer = 0 For Each itemChecked In clbStatus.CheckedItems If index = clbStatus.CheckedItems.Count - 1 Then LogicalOperator = "" End If StatFilter = String.Format("Status = '{0}' {1}", itemChecked.ToString(), LogicalOperator) FilterString += StatFilter index += 1 Next If FilterString <> "" Then FilterString = "(" + FilterString + ")" End If Return FilterString End Function Private Function StartTimeFilter() As String Dim FilterString As String = "" StartTimeFilter = String.Format("StartTime > '#{0}#' AND StartTime < '#{1}#' AND " & _ "StartTime > '#{2}#' AND StartTime < '#{3}#'", dtpStartTime1.Value.Date, dtpStartTime1.Value.Date().AddDays(nmMultipleStartDays.Value), dtpStartHour.Value.Hour, dtpStartHour.Value.AddHours(nmStartHour2.Value)) FilterString += StartTimeFilter Return FilterString End Function Private Function EndTimeFilter() As String Dim FilterString As String = "" EndTimeFilter = String.Format("EndTime > '#{0}#' AND EndTime < '#{1}#' AND " & _ "EndTime > '#{2}#' AND EndTime < '#{3}#'", dtpEndTime1.Value.Date(), dtpEndTime1.Value.Date().AddDays(nmMultipleStartDays.Value), dtpEndHour.Value.Hour, dtpEndHour.Value.AddHours(nmEndHour2.Value)) FilterString += EndTimeFilter Return FilterString End Function
So there are 4 individual filters, one for technicians, one for unit status, one for start times, and one for end times. So far these all work, (to the best of my knowledge, as I wrote the hour part at home, I'll get to test that tomorrow)
So these four functions are slapped together into a string sandwich in this function:
Private Sub btnApplyFilter_Click(sender As Object, e As EventArgs) Handles btnApplyFilter.Click Dim TechFilter As String = TechnicianFilter() Dim StatFilter As String = StatusFilter() Dim StartFilter As String = StartTimeFilter() Dim EndFilter As String = EndTimeFilter() Dim LogicalOperator0 As String = " AND " Dim LogicalOperator1 As String = " AND " Dim LogicalOperator2 As String = " AND " 'conditional statement for item {1} of filter expression If TechFilter = "" Or StatFilter = "" Then LogicalOperator0 = "" End If 'conditional statement for item {3} of filter expression If (TechFilter = "" And StatFilter = "") Or StartFilter = "" Then LogicalOperator1 = "" End If 'conditional statement for item {5} of filter expression If (TechFilter = "" And StatFilter = "" And StartFilter = "") Or EndFilter = "" Then LogicalOperator2 = "" End If Dim FilterExpression As String = String.Format("{0}{1}{2}{3}{4}{5}{6}", TechFilter, LogicalOperator0, StatFilter, LogicalOperator1, StartFilter, LogicalOperator2, EndFilter) txtFilterString.Text = FilterExpression RepairBindingSource.Filter = txtFilterString.Text End Sub
So ultimately I am wondering if there is any good way to clean all this code up, polish it and make it neat. I was wondering about the logicaloperator strings, and how I might be more efficient.
Ideas, criticisms, comments, questions welcome!