|
-
Mar 7th, 2009, 09:38 AM
#1
Thread Starter
Hyperactive Member
[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
-
Mar 7th, 2009, 09:42 AM
#2
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.
-
Mar 7th, 2009, 09:44 AM
#3
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
-
Mar 7th, 2009, 09:48 AM
#4
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
-
Mar 7th, 2009, 09:51 AM
#5
Thread Starter
Hyperactive Member
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
-
Mar 7th, 2009, 09:53 AM
#6
Re: [2005] Checking if something should happen today (code tidying required!)
 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.
-
Mar 7th, 2009, 09:59 AM
#7
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
-
Mar 7th, 2009, 10:03 AM
#8
Thread Starter
Hyperactive Member
Re: [2005] Checking if something should happen today (code tidying required!)
 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
-
Mar 7th, 2009, 10:04 AM
#9
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
-
Mar 7th, 2009, 10:13 AM
#10
Thread Starter
Hyperactive Member
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
-
Mar 7th, 2009, 12:25 PM
#11
Re: [RESOLVED] [2005] Checking if something should happen today (code tidying required!)
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
|