|
-
Apr 9th, 2013, 12:30 PM
#41
Thread Starter
Lively Member
Re: Reading from XML
How can I then solve this problem? I need a method to detect whether I should get the time of Morning, from the currentDay or tomorrow.
-
Apr 9th, 2013, 05:33 PM
#42
Thread Starter
Lively Member
-
Apr 10th, 2013, 09:08 AM
#43
Re: Reading from XML
Hey, Crystalii,
Consider this line form your CheckStatus method:
Code:
Dim CurrentPrayer As String = allTimes.LastOrDefault(Function(kvp) kvp.Value < CurrentTime).Key
Firstly, should the prayer start AT the time stored in your XML data, as opposed to AFTER that time, you might want to change it to:
Code:
Dim CurrentPrayer As String = allTimes.LastOrDefault(Function(kvp) kvp.Value <= CurrentTime).Key
Secondly, the value returned for CurrentPrayer by that code will be NOTHING when the current time is less than the time given by Morning.
So if CurrentPrayer = Nothing, then the current Prayer has to be the Prayer started LAST NIGHT.
Conversely, if CurrentPrayer = "Night" then the next Prayer has to be TOMORROW MORNING.
These facts lead to the following code:
vb.net Code:
Public Sub CheckStatus()
Dim MorningTime As DateTime = Convert.ToDateTime(Morning)
Dim NoonTime As DateTime = Convert.ToDateTime(Noon)
Dim AfterNoonTime As DateTime = Convert.ToDateTime(Afternoon)
Dim SunSetTime As DateTime = Convert.ToDateTime(Sunset)
Dim NightTime As DateTime = Convert.ToDateTime(Night)
Dim allTimes As New Dictionary(Of String, DateTime) From {{"Morning", MorningTime}, _
{"Noon", NoonTime}, _
{"Afternoon", AfterNoonTime}, _
{"Sunset", SunSetTime}, _
{"Night", NightTime}}
Dim CurrentTime As DateTime = DateTime.Now
Dim CurrentPrayer As String = allTimes.LastOrDefault(Function(kvp) kvp.Value <= CurrentTime).Key
' CurrentPrayer will be NOTHING for times < Morning
' so the actual Prayer will be Yesterday's NightTime Prayer
' and the next Prayer has to be Today's Morning Prayer
If CurrentPrayer = Nothing Then
CurrentPrayer = "Yesterday Night"
End If
lblCurrent.Text = "Current prayer: " & CurrentPrayer
If CurrentPrayer = "Yesterday Night" Then
lblNext.Text = "Next prayer: Morning " & Morning
ElseIf CurrentPrayer = "Morning" Then
lblNext.Text = "Next prayer: Noon " & Noon
ElseIf CurrentPrayer = "Noon" Then
lblNext.Text = "Next prayer: Afternoon " & Afternoon
ElseIf CurrentPrayer = "Afternoon" Then
lblNext.Text = "Next prayer: Sunset " & Sunset
ElseIf CurrentPrayer = "Sunset" Then
lblNext.Text = "Next prayer: Night " & Night
ElseIf CurrentPrayer = "Night" Then
' next Prayer has to be Tomorrow's Morning Prayer
Dim Tomorrow As DateTime = DateTime.Today.AddDays(1)
lblNext.Text = "Next prayer: Tomorrow Morning " & GetTime("_" & Tomorrow.ToString("MM"), "_" & Tomorrow.ToString("dd"), "Morning")
End If
End Sub
I would also change your GetTime function to allow for the possibility that your XML data file does not contain data for the following day. Something along the lines of:
vb.net Code:
Private Function GetTime(ByVal monthString As String, ByVal dayString As String, ByVal time As String) As String
Dim xml As XDocument
xml = XDocument.Load(Application.StartupPath & "\data.xml")
Dim tempTime As String = ""
Dim month As XElement = xml.Root.Element(monthString)
If month IsNot Nothing Then
Dim day As XElement = month.Element(dayString)
If day IsNot Nothing AndAlso day.Element(time) IsNot Nothing Then
tempTime = day.Element(time).Value
End If
End If
Return tempTime
End Function
-
Apr 10th, 2013, 01:24 PM
#44
Thread Starter
Lively Member
Re: Reading from XML
Thank you, you fixed one of my biggest problems! It's actually fascinating that the solution was in front of my eyes. But I had been trying so many ways to fix this, and the whole code was such a mess, that I did not know what to do.
Thank you
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|