Results 1 to 11 of 11

Thread: [RESOLVED] [2005] Checking if something should happen today (code tidying required!)

  1. #1

    Thread Starter
    Hyperactive Member
    Join Date
    Feb 2000
    Location
    the UK
    Posts
    265

    Resolved [RESOLVED] [2005] Checking if something should happen today (code tidying required!)

    OK - I have a form with seven tickboxes - one for each day of the week - each one saved in my.settings.monday, my.settings.tuesday, etc

    Now I need to check if something should happen today and the code I'm using (below) seems very longwinded. Can anyone help 'tidy it up'?!

    Thanks

    Code:
    If Now.DayOfWeek = DayOfWeek.Monday And My.Settings.monday = True Then
                    'do it
                End If
    
                If Now.DayOfWeek = DayOfWeek.Tuesday And My.Settings.tuesday = True Then
                    'do it
                End If
    
                If Now.DayOfWeek = DayOfWeek.Wednesday And My.Settings.wednesday = True Then
                    'do it
                End If
    
                If Now.DayOfWeek = DayOfWeek.Thursday And My.Settings.thursday = True Then
                    'do it
                End If
    
                If Now.DayOfWeek = DayOfWeek.Friday And My.Settings.friday = True Then
                    'do it
                End If
    
                If Now.DayOfWeek = DayOfWeek.Saturday And My.Settings.saturday = True Then
                    'do it
                End If
    
                If Now.DayOfWeek = DayOfWeek.Sunday And My.Settings.sunday = True Then
                    'do it
                End If

  2. #2
    Frenzied Member Campion's Avatar
    Join Date
    Jul 2007
    Location
    UT
    Posts
    1,098

    Re: [2005] Checking if something should happen today (code tidying required!)

    Use Select Case instead.

    Code:
    Select Case Now.DayofWeek
        Case DayofWeek.Monday
            If My.Settings.Monday Then
         . . .
            End If
        Case Dayofweek.Tuesday
        . . .
    End Select
    It'll trim it down and keep it more efficient.

  3. #3
    Powered By Medtronic dbasnett's Avatar
    Join Date
    Dec 2007
    Location
    Jefferson City, MO
    Posts
    9,897

    Re: [2005] Checking if something should happen today (code tidying required!)

    in form load
    Code:
            CheckBox1.Text = DayOfWeek.Sunday.ToString.Substring(0, 3)
            CheckBox2.Text = DayOfWeek.Monday.ToString.Substring(0, 3)
            CheckBox3.Text = DayOfWeek.Tuesday.ToString.Substring(0, 3)
            CheckBox4.Text = DayOfWeek.Wednesday.ToString.Substring(0, 3)
            CheckBox5.Text = DayOfWeek.Thursday.ToString.Substring(0, 3)
            CheckBox6.Text = DayOfWeek.Friday.ToString.Substring(0, 3)
            CheckBox7.Text = DayOfWeek.Saturday.ToString.Substring(0, 3)
    Code:
                Select Case DateTime.Now.DayOfWeek.ToString.Substring(0, 3)
                    Case CheckBox1.Text
                        If Not CheckBox1.Checked Then doitQ = False
                    Case CheckBox2.Text
                        If Not CheckBox2.Checked Then doitQ = False
                    Case CheckBox3.Text
                        If Not CheckBox3.Checked Then doitQ = False
                    Case CheckBox4.Text
                        If Not CheckBox4.Checked Then doitQ = False
                    Case CheckBox5.Text
                        If Not CheckBox5.Checked Then doitQ = False
                    Case CheckBox6.Text
                        If Not CheckBox6.Checked Then doitQ = False
                    Case CheckBox7.Text
                        If Not CheckBox7.Checked Then doitQ = False
                End Select
    My First Computer -- Documentation Link (RT?M) -- Using the Debugger -- Prime Number Sieve
    Counting Bits -- Subnet Calculator -- UI Guidelines -- >> SerialPort Answer <<

    "Those who use Application.DoEvents have no idea what it does and those who know what it does never use it." John Wein

  4. #4
    PowerPoster gep13's Avatar
    Join Date
    Nov 2004
    Location
    The Granite City
    Posts
    21,963

    Re: [2005] Checking if something should happen today (code tidying required!)

    Hey,

    Could you not just have one If statement?

    Rather than have My.Settings.Monday through to My.Setting.Sunday, which not just have My.Settings.DayOfWeek and set that equal to "Monday", and then your check becomes:

    Code:
    If DateTime.Now.DayOfWeek.ToString() = My.Settings.DayOfWeek Then
        'Do stuff here
    End If
    Hope that helps!!

    Gary

  5. #5

    Thread Starter
    Hyperactive Member
    Join Date
    Feb 2000
    Location
    the UK
    Posts
    265

    Re: [2005] Checking if something should happen today (code tidying required!)

    Hi Gary,

    That was my original idea but the user needs to be able to choose multiples of days not just one.

    I was faffing around arrays to see if I could get all seven days in one setting but wasn't getting stuck.

    Select Case seems to look best

    Thanks guys.

    Simon

  6. #6
    Frenzied Member Campion's Avatar
    Join Date
    Jul 2007
    Location
    UT
    Posts
    1,098

    Re: [2005] Checking if something should happen today (code tidying required!)

    Quote Originally Posted by gep13
    Rather than have My.Settings.Monday through to My.Setting.Sunday, which not just have My.Settings.DayOfWeek and set that equal to "Monday"
    I can see that if the OP is doing the same thing over and over. Just to be safe, I assume that he's doing something different in each block of code.

  7. #7
    Frenzied Member MaximilianMayrhofer's Avatar
    Join Date
    Aug 2007
    Location
    IM IN YR LOOP
    Posts
    2,001

    Re: [2005] Checking if something should happen today (code tidying required!)

    Code:
    If DirectCast( _
        My.Settings(DateTime.Now.DayOfWeek.ToString()), _
        Boolean _
    ) Then
        'Yes there is crap to do today
    End If

  8. #8

    Thread Starter
    Hyperactive Member
    Join Date
    Feb 2000
    Location
    the UK
    Posts
    265

    Re: [2005] Checking if something should happen today (code tidying required!)

    Quote Originally Posted by MaximilianMayrhofer
    Code:
    If DirectCast( _
        My.Settings(DateTime.Now.DayOfWeek.ToString()), _
        Boolean _
    ) Then
        'Yes there is crap to do today
    End If
    Oooohh - this looks best yet!

    I've just tried it out and got:

    The settings property 'Saturday' was not found.
    Is there something I can tweak to make it work?

    Cheers

  9. #9
    PowerPoster gep13's Avatar
    Join Date
    Nov 2004
    Location
    The Granite City
    Posts
    21,963

    Re: [2005] Checking if something should happen today (code tidying required!)

    That is because you are looking for a setting called My.Settings.Saturday, and the setting you have is called My.Settings.saturday.

    You will need to change the name of your Settings.

    Gary

  10. #10

    Thread Starter
    Hyperactive Member
    Join Date
    Feb 2000
    Location
    the UK
    Posts
    265

    Re: [2005] Checking if something should happen today (code tidying required!)

    ah - my settings names were slightly different;

    I've used:

    Code:
    My.Settings("BackupDaysoftheWeek" + DateTime.Now.DayOfWeek.ToString()), _
    . . . and it's working great.

    Fantastic bit of code - thanks everyone.

    Simon

  11. #11
    Frenzied Member MaximilianMayrhofer's Avatar
    Join Date
    Aug 2007
    Location
    IM IN YR LOOP
    Posts
    2,001

    Re: [RESOLVED] [2005] Checking if something should happen today (code tidying required!)

    Glad to help.

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