Solved

Remove hardcoded days from a IF statement

Posted on 2007-03-22
4
328 Views
Last Modified: 2010-04-16
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

0
Comment
Question by:mousemat24
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
  • 2
4 Comments
 
LVL 86

Accepted Solution

by:
Mike Tomlinson earned 500 total points
ID: 18775363
Put a leading and trailing comma on your date values in your string:

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

Then you can use string.IndexOf():

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

   }
0
 

Author Comment

by:mousemat24
ID: 18775557
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
 
LVL 86

Expert Comment

by:Mike Tomlinson
ID: 18775567
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 Comment

by:mousemat24
ID: 18775571
Ohhh right, thank you so much for help me, and having the time to explain this to me!!!

Your a star

Mousemat24
0

Featured Post

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
This article aims to explain the working of CircularLogArchiver. This tool was designed to solve the buildup of log file in cases where systems do not support circular logging or where circular logging is not enabled
Monitoring a network: why having a policy is the best policy? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the enormous benefits of having a policy-based approach when monitoring medium and large networks. Software utilized in this v…
If you’ve ever visited a web page and noticed a cool font that you really liked the look of, but couldn’t figure out which font it was so that you could use it for your own work, then this video is for you! In this Micro Tutorial, you'll learn yo…

630 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question