Results 1 to 4 of 4

Thread: [2005] Any Help

  1. #1

    Thread Starter
    Hyperactive Member
    Join Date
    May 2005
    Location
    London United Kingdom
    Posts
    334

    Post [2005] Any Help

    Could any one help me with this error warning I am getting with my function.
    Code:
     Private Function GetShiftType() As String
            Dim StartTime, EndTime As Date
            Dim StartHour, EndHour As Double
            StartTime = dtpStartTime.Text
            EndTime = dtpEndTime.Text
    
            StartHour = StartTime.TimeOfDay.TotalHours
            EndHour = EndTime.TimeOfDay.TotalHours
    
            If StartHour >= 7.0 AndAlso StartHour <= 8.5 AndAlso EndHour >= 12.0 AndAlso EndHour <= 16.0 Then
                txtShiftType.Text = "Ealy"
                btnUpdate.Focus()
            ElseIf StartHour >= 12.0 AndAlso StartHour <= 15.0 AndAlso EndHour >= 19.0 AndAlso EndHour <= 21.0 Then
                txtShiftType.Text = "Late"
                btnUpdate.Focus()
            ElseIf StartHour >= 7.0 AndAlso StartHour <= 8.5 AndAlso EndHour >= 19.0 AndAlso EndHour <= 21.0 Then
                txtShiftType.Text = "Long Day"
                btnUpdate.Focus()
            ElseIf StartHour >= 19.0 AndAlso StartHour <= 21.0 AndAlso EndHour >= 7.0 AndAlso EndHour <= 9.0 Then
                txtShiftType.Text = "Night"
                btnUpdate.Focus()
            ElseIf StartHour >= 15.0 AndAlso StartHour <= 18.0 AndAlso EndHour >= 0.0 Then
                txtShiftType.Text = "Toalight"
                btnUpdate.Focus()
            Else
                MessageBox.Show("The specified start and end times do not match any known shift.", "Check shift time")
                dtpStartTime.Focus()
            End If
    
        End Function
    Then end function is underline and the error message is as follows (Function 'GetShiftType' doesn't return a value on all code paths. A null reference exception could occur at run time when the result is used.)
    Thanks

  2. #2
    Hyperactive Member gnaver's Avatar
    Join Date
    Jul 2005
    Location
    Denmark/Sweden
    Posts
    289

    Re: [2005] Any Help

    you dont return a value to the GetShiftType that is why you get the warning.

    use private sub instead of a private function.

    or return a value using the function..

  3. #3
    Frenzied Member stimbo's Avatar
    Join Date
    Jun 2006
    Location
    UK
    Posts
    1,739

    Re: [2005] Any Help

    To reduce errors I would also consider using something like a numeric updown control, or even combo boxes instead of textboxes. That way the format would also be correct and you would vastly reduce the chances of errors.

    I also dont see why you have the start and end time declared as Date when they would probably be best declared as Double.
    Stim

    Free VB.NET Book Chapter
    Visual Basic 2005 Cookbook Sample Chapter

  4. #4
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: [2005] Any Help

    You shouldn't be doing this:
    vb Code:
    1. Dim StartTime, EndTime As Date
    2. '...
    3. StartTime = dtpStartTime.Text
    4. EndTime = dtpEndTime.Text
    You should just be doing this:
    vb Code:
    1. Dim StartTime, EndTime As TimeSpan
    2. '...
    3. StartTime = dtpStartTime.Value.TimeOfDay
    4. EndTime = dtpEndTime.Value.TimeOfDay
    The DTP already contains Date values and you're converting them to Strings and back to Dates again. Never convert between a non-string type and the string type unless you need to.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width