Categories: MSDN / DotNet / Java / Scripts / Linux / PHP Ask - La ask - La Answer

Check Function Not Working

I have a simple application that takes user input by text and time
selected by date time picker. It displays the appt description in one
list box and the time in another list box. I have a simple function
that checks the times for duplicates. I want to check for a duplicate
before it writes to the listbox by returning a boolean (true/false)
value. I only need to check hours and minutes not date or seconds.

Right now I have this but it's not finding duplicates, I think there's
a problem with the function? Any Suggestions?

Dim Time As Boolean

Time = TimeTaken(Me.dtmTime.Value.ToShort* DateString)

If Time Then
MessageBox.Show("You already have an appointment at
this time")
Else

'display appointment in Listbox
lstApptResults.Items.Add(txtAppoin* tment.Text)
txtAppointment.Clear() 'clear appointment from TextBox
txtAppointment.Focus() 'transfer focus to TextBox

'display appointment time in Listbox

lstTimeResults.Items.Add(Me.dtmTim* e.Value.ToShortTimeString)

End If
End If
End Sub 'btnAddAppt_Click
'function to check if an appointment time already exists in listbox
Public Function TimeTaken(ByVal ApptTime As DateTime) As Boolean

Dim DuplicateTime As Boolean = False

'loop that checks listed times for duplicates
For Each strItem As String In lstTimeResults.Items
If strItem = ApptTime Then
DuplicateTime = True

Exit For
End If
Next

Return DuplicateTime
End Function 'TimeTaken
[1669 byte] By [geo039] at [2007-11-11 9:51:12]
# 1 Re: Check Function Not Working
Set a breakpoint on the line, "For Each strItem As String In lstTimeResults.Items" then run your app. As the code iterates through the loop, look in the Locals window (or hover your mouse pointer over the variable names) and compare the values of strItem and ApptTime.
Phil Weber at 2007-11-11 21:45:16 >
# 2 Re: Check Function Not Working
I was able to determine that the function works on the other listbox so I narrowed it down to the date as the problem. Now I just need to convert something somewhere so I can compare apples with apples. Thanks for pointing out the locals window, never looked there.
geo039 at 2007-11-11 21:46:16 >
# 3 Re: Check Function Not Working
You might try something like this:

Public Function TimeTaken(ByVal ApptTime As DateTime, _
ByVal TimesList As ICollection) As Boolean

Dim DuplicateTime As Boolean = False

'loop that checks listed times for duplicates
For Each strItem As String In TimesList
Dim TimeItem As DateTime = DateTime.Parse(strItem)
If TimeItem.Hour = ApptTime.Hour AndAlso _
TimeItem.Minute = ApptTime.Minute Then
DuplicateTime = True
Exit For
End If
Next

Return DuplicateTime

End Function 'TimeTaken
Phil Weber at 2007-11-11 21:47:15 >
# 4 Re: Check Function Not Working
I was able to determine that the function works on the other listbox so I narrowed it down to the date as the problem. Now I just need to convert something somewhere so I can compare apples with apples. Thanks for pointing out the locals window, never looked there.

If you turn on Option Strict in the IDE, it will prevent these errors by not allowing you to compare a string value to a date value (without converting one).
joewmaki at 2007-11-11 21:48:15 >
# 5 Re: Check Function Not Working
Yep I saw more of the problem when I turned Option Strict on, I got this working now.

Thanks Again
geo039 at 2007-11-11 21:49:17 >