简体   繁体   中英

C# switch case forcing

Hi there I am making a C# windows forms app, I made a switch with all my teammates birthdays in order to calculate their birthday age, however I want to add the functionality that if today is their birthday a message appears. Therefore I need something that goes into EACH CASE of the switch and there I will add a validation like if today's date is equal to bday, then messageshow "its name 1 birthday, today!"

switch (name)
{
    case "name 1":
        bday = DateTime.Parse("11.11.1988");
        break;
    case "name 2":
        bday = DateTime.Parse("11.12.1988");
        break;
    case "name 3":
        bday = DateTime.Parse("03.12.1987");  
        break;
}   

Why dont you use a Dictionary. Dictionaries use other objects as Keys to retrieve their values.

In your case you could map all the birthdays to their names like:

Dictionary<string, DateTime> birthdays = new Dictionary<string, DateTime>;

//Add values like this 
birthdays.Add("name 1", DateTime.Parse("11.11.1988"));
birthdays.Add("name 2", DateTime.Parse("11.12.1988"));
...

//Then you could loop through all the entries
foreach(KeyValuePair<string, DateTime> entry in birthdays)
{
    if(entry.Value.Day == DateTime.Now.Day && entry.Value.Month == DateTime.Now.Month)
    {
        Console.WriteLine(entry.Key + " has birthday!");
    }
}

Based on what you provided in your snippet, you could do this sort of check outside of the case statement. Example:

public void WhateverYourMethodIs()
{
    switch (name)
    {
        case "name 1":
            bday = DateTime.Parse("11.11.1988");
            break;
        case "name 2":
            bday = DateTime.Parse("11.12.1988");
            break;
        case "name 3":
            bday = DateTime.Parse("03.12.1987");  
            break;
    }   

    if (this.IsBirthday(bday))
    {
        // do whatever you want for when it's the name's birthday.
    }
}

public bool IsBirthday(DateTime bday)
{
    if (bday.Day == DateTime.Now.Day && bday.Month == DateTime.Now.Month)
        return true;

    return false;
}

note the above would not take into account leap day birthdays.

Based on your comments, it sounds like you're hoping to evaluate the birthdays of all names regardless of the switch. This won't work in your current approach. bday is a single value that can only be attributed to the current "name".

One way to accomplish what you're hoping for would be to use a class to represent a name as so:

public class User
{
    public string Name { get; set; }
    public DateTime Birthday { get; set; }

    public bool IsBirthday
    {
        get
        {
            if (Birthday.Day == DateTime.Now.Day && Birthday.Month == DateTime.Now.Month)
                return true;

            return false;
        }        
    }
}

public class SomeClass
{    

    private List<User> Users = new List<User>();
    public void PopulateNames()
    {
        Users.Add(new User()
        {
            Name = "name 1",
            Birthday = DateTime.Parse("11.11.1988")
        };

        Users.Add(new User()
        {
            Name = "name 2",
            Birthday = DateTime.Parse("11.12.1988")
        };
        // etc
    }

    public void DoSomethingOnUsersWithABirthday()
    {
        foreach (User user in Users)
        {
            if (user.IsBirthday)
            {
                // do something for their birthday.
                Console.WriteLine(string.format("It's {0}'s birthday!", user.Name));
            }
        }
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM