# Remove hardcoded days from a IF statement

Hi there

Wonder if you can help me?

I have the following code:

int todaysDay = DateTime.Today.Day;

if (todaysDay == 1 || todaysDay == 17 || todaysDay == 28 || someV = false)
{
// do something
}

The problem with that is I've had to hardcode the days i.e. 1,17,28 in the IF statement. Can someone please help me in getting the values i.e. 1,17,28 (or it could be more days) into a variable.

Something like this

string daysToSearch = "1,17,28"; // Please note, I will be getting the string "1,17,28" from a SQL field. Having got the "1,17,28" from SQL, I can then use this in the IF statement

Hope that makes sense?
Thanks
Mousemat24

Having

###### Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Middle School Assistant TeacherCommented:

string daysToSearch = ",1,17,28,"

Then you can use string.IndexOf():

if (daysToSearch.IndexOf("," + DateTime.Today.Day.ToString() + ",") >=0 || someV == false)
{

}
0

Experts Exchange Solution brought to you by

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Author Commented:
Thanks Idle_Mind for replying to me so quickly, just one question, why do you say

"Put a leading and trailing comma on your date values in your string:"? The following code works without the trailing comma

string daysToSearch = "1,17,28,21";

if (daysToSearch.IndexOf(DateTime.Today.Day.ToString()) >= 0)
{

}

Thanks
Mousemat24
0
Middle School Assistant TeacherCommented:
Because you will get "false positives"...

What if you search for the value of "2"?

String.IndexOf() will return a value because it is present in both "28" and "21" even though the value of just "2" is not present.

This is why I am surrounding the Day.ToString() value in commas:

"," + DateTime.Today.Day.ToString() + ","

and why you need the leading and trailing commas in the days string so that you can get a match on the first and last entries in the string.
0
Author Commented:
Ohhh right, thank you so much for help me, and having the time to explain this to me!!!