C# 正在尝试根据DateTime.now在messagebox中设置名称

C# 正在尝试根据DateTime.now在messagebox中设置名称,c#,visual-studio-express,C#,Visual Studio Express,为了简短起见,我想根据日期填写姓名。 我没有使用任何数据库,它是纯c 下面是我当前的代码 DateTime dt = DateTime.Now; string vandaag = dt.ToString("dd-MM-yyyy"); string bday = " has his/her birthday today"; Boolean name1 = DateTime.Now.Month == 3 && DateTime.Now.Day == 12; Boolean name

为了简短起见,我想根据日期填写姓名。 我没有使用任何数据库,它是纯c

下面是我当前的代码

DateTime dt = DateTime.Now;
string vandaag = dt.ToString("dd-MM-yyyy");
string bday = " has his/her birthday today";

Boolean name1 = DateTime.Now.Month == 3 && DateTime.Now.Day == 12;
Boolean name2 = DateTime.Now.Month == 6 && DateTime.Now.Day == 9;

if (name1) {MessageBox.Show(string.Format("Hello, today is {0}. name of person 1 {1}", today, bday), "Hello", MessageBoxButtons.OK);}

else if (name2) {MessageBox.Show(string.Format("Hello, today is {0}. name of person 2 {1}", today, bday), "Hello", MessageBoxButtons.OK);}
等等

这个代码实际上是有效的,但像这样,我会为任何人重复这个代码。有没有一种方法可以在不重复相同代码的情况下填写“人名”呢?

我通常会创建一个列表对象,以便通过表单上的控件进行枚举

        public List<TextBox> boxes = null;
        public Form1()
        {
            InitializeComponent();

            boxes = new List<TextBox>() { textBox1, textBox2, textBox3, textBox4};

            foreach (TextBox box in boxes)
            {
            }
公共列表框=null;
公共表格1()
{
初始化组件();
box=新列表(){textBox1,textBox2,textBox3,textBox4};
foreach(文本框中的文本框)
{
}
我通常创建一个列表对象,以便可以通过表单上的控件进行枚举

        public List<TextBox> boxes = null;
        public Form1()
        {
            InitializeComponent();

            boxes = new List<TextBox>() { textBox1, textBox2, textBox3, textBox4};

            foreach (TextBox box in boxes)
            {
            }
公共列表框=null;
公共表格1()
{
初始化组件();
box=新列表(){textBox1,textBox2,textBox3,textBox4};
foreach(文本框中的文本框)
{
}
在本例中,使用非常有用,请看以下示例:

public void sayHappyBirthDay(Dictionary<string, DateTime> nameBirthDay)
{
    DateTime today = DateTime.Now;
    foreach (var n in nameBirthDay)
    {
        if (today.Month == n.Value.Month && today.Day == n.Value.Day)
        {
            string bday = n.Key;
            MessageBox.Show(string.Format("Hello, today is {0}. name of person: {1}", today, bday), "Hello", MessageBoxButtons.OK);
        }
    }
}
public void sayHappyBirthDay(字典名生日)
{
DateTime today=DateTime.Now;
foreach(名称中的变量n)
{
if(今日.Month==n.Value.Month&&Day.Day==n.Value.Day)
{
字符串bday=n.键;
Show(string.Format(“你好,今天是{0}。人名:{1}”,今天是星期四),“你好”,MessageBoxButtons.OK);
}
}
}
在本例中,使用非常有用,请看以下示例:

public void sayHappyBirthDay(Dictionary<string, DateTime> nameBirthDay)
{
    DateTime today = DateTime.Now;
    foreach (var n in nameBirthDay)
    {
        if (today.Month == n.Value.Month && today.Day == n.Value.Day)
        {
            string bday = n.Key;
            MessageBox.Show(string.Format("Hello, today is {0}. name of person: {1}", today, bday), "Hello", MessageBoxButtons.OK);
        }
    }
}
public void sayHappyBirthDay(字典名生日)
{
DateTime today=DateTime.Now;
foreach(名称中的变量n)
{
if(今日.Month==n.Value.Month&&Day.Day==n.Value.Day)
{
字符串bday=n.键;
Show(string.Format(“你好,今天是{0}。人名:{1}”,今天是星期四),“你好”,MessageBoxButtons.OK);
}
}
}

您可以这样做(未经测试,直接在堆栈溢出上编写,小心处理,我花时间测试了它,但令人惊讶的是,只出现了一些小错误!):

static void AddBirthdayToDictionary(字典目录、整数月、整数日、字符串名)
{
var bdate=新的日期时间(DateTime.MinValue.Year、month、day);
列表val;
如果(!dict.TryGetValue(bdate,out val))
{
val=新列表();
dict.Add(b日期,val);
}
val.Add(名称);
}
静态列表GetBirthdays(字典dict dict,int month,int day)
{
var bdate=新的日期时间(DateTime.MinValue.Year、month、day);
列表val;
if(dict.TryGetValue(bdate,out val))
{
返回val;
}
返回新列表();
}
静态无效显示生日()
{
var birthdayDictionary=新字典();
//添加可能的生日
将BirthdayToDictionary(birthdayDictionary,3,12,“Person 1”);
将BirthdayToDictionary(birthdayDictionary,6,9,“Person 2”);
var today=DateTime.today;
//今天过生日
var bdayList=GetBirthdays(birthdayDictionary,today.Month,today.Day);
if(bdayList.Any())
{
var count=bdayList.count();
MessageBox.Show(string.Format(“你好,今天是{0}和{1}{2}庆祝{3}生日!”,今天,string.Join(“,”,bdayList),(count>1)?“are”:“is”,(count>1)?“他们的”:“its”);
}       
}

您可以这样做(未经测试,直接在堆栈溢出上编写,小心处理,我花时间测试了它,但令人惊讶的是,只出现了一些小错误!):

static void AddBirthdayToDictionary(字典目录、整数月、整数日、字符串名)
{
var bdate=新的日期时间(DateTime.MinValue.Year、month、day);
列表val;
如果(!dict.TryGetValue(bdate,out val))
{
val=新列表();
dict.Add(b日期,val);
}
val.Add(名称);
}
静态列表GetBirthdays(字典dict dict,int month,int day)
{
var bdate=新的日期时间(DateTime.MinValue.Year、month、day);
列表val;
if(dict.TryGetValue(bdate,out val))
{
返回val;
}
返回新列表();
}
静态无效显示生日()
{
var birthdayDictionary=新字典();
//添加可能的生日
将BirthdayToDictionary(birthdayDictionary,3,12,“Person 1”);
将BirthdayToDictionary(birthdayDictionary,6,9,“Person 2”);
var today=DateTime.today;
//今天过生日
var bdayList=GetBirthdays(birthdayDictionary,today.Month,today.Day);
if(bdayList.Any())
{
var count=bdayList.count();
MessageBox.Show(string.Format(“你好,今天是{0}和{1}{2}庆祝{3}生日!”,今天,string.Join(“,”,bdayList),(count>1)?“are”:“is”,(count>1)?“他们的”:“its”);
}       
}

要做的第一件事是定义这样一个Person类(以后可以扩展)

然后,你需要建立一个名单,你要检查他们的生日

List<Person> people = new List<Person>()
{
    new Person() {Name="Steve", dob = new DateTime(1960, 3, 26)},
    new Person() {Name="John", dob = new DateTime(1961, 7, 1)},
};
使用此方法,您只需使用上述格式将其他人添加到列表中,其他内容不会发生任何更改。但是,存在一个问题,因为您可能有多个具有相同出生日期的人。在这种情况下,搜索列表的代码将变为

List<Person> ppl = people.Where(x => x.dob.Month == DateTime.Today.Month && 
                                x.dob.Day == DateTime.Today.Day).ToList();
MessageBox.Show(string.Format("There are {0} people with this dob", ppl.Count));
foreach(Person p in ppl)
    MessageBox.Show(string.Format("Hello, today is {0}. Birthday of person {1}", DateTime.Today, p.Name), "Hello", MessageBoxButtons.OK); 
List ppl=people.Where(x=>x.dob.Month==DateTime.Today.Month&&
x、 dob.Day==DateTime.Today.Day).ToList();
Show(string.Format(“有{0}个人拥有这个dob”,ppl.Count));
foreach(ppl中的p人)
Show(string.Format(“你好,今天是{0}。人{1}的生日”,DateTime.today,p.Name),“你好”,MessageBoxButtons.OK);

要做的第一件事是定义这样一个Person类(以后可以扩展)

然后,你需要建立一个名单,你要检查他们的生日

List<Person> people = new List<Person>()
{
    new Person() {Name="Steve", dob = new DateTime(1960, 3, 26)},
    new Person() {Name="John", dob = new DateTime(1961, 7, 1)},
};
使用这种方法,您只需使用上面的格式将其他人添加到列表中,其他内容不做任何更改
// out message
string _msg = String.Empty;

// sample array of Name/Birthday
string[,] _bd = { 
                    { "Tom", "03/26/2016" }, 
                    { "Gary", "06/27/2016" }, 
                    { "Anna", "04/30/2016" } 
                };
for (int i = 0; i < _bd.GetLength(0); i++)
{
    if (DateTime.Today == DateTime.Parse(_bd[i, 1]))
    {
        // output message containing list of names
        _msg += _bd[i, 0] + ",";
    }
}
if (!String.IsNullOrEmpty(_msg)) MessageBox.Show("Hello" +_msg);