首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > .NET > C# >

多个赋值的简写形式

2013-07-01 
多个赋值的简写方式自己做了一个简易月日选择器,其中4、6、9、11月是30天,我进行分别赋值。s 4 || s

多个赋值的简写方式
自己做了一个简易月日选择器,其中4、6、9、11月是30天,我进行分别赋值。
s == "4" || s == "6" || s == "9" || s == "11"
如果有更多的赋值,有没有简写的方式?

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace 日月选择器
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
        {    
            comboBox2.Items.Clear();
            string s = comboBox1.Text;
            if (s == "2")
            {
                for (int i = 1; i <= 28; i++)
                {
                    comboBox2.Items.Add(i);                                                                           }
            }
            else if (s == "4" || s == "6" || s == "9" || s == "11")
            {
                for (int i = 1; i <= 30; i++)
                {
                    comboBox2.Items.Add(i);
                }
            }

            else
            {
                for (int i = 1; i <= 31; i++)
                {
                    comboBox2.Items.Add(i);
                }


            }
        }
    }
}

多赋值、简写、C#
[解决办法]
string[] arr = { "4","6","9","11"};
            if(arr.Contains(s)){...};

[解决办法]
2月全是28天??

其实.net  DateTime 有俩个属性非常好!



DateTime.IsLeapYear; 
DateTime.DaysInMonth;



http://msdn.microsoft.com/zh-cn/library/system.datetime.isleapyear(v=VS.80).aspx

http://msdn.microsoft.com/zh-cn/library/system.datetime.daysinmonth(v=VS.80).aspx
[解决办法]

            int month= 2;
            int days = DateTime.DaysInMonth(2013, month);
            //输出某年某月多少天
            Console.WriteLine(days);

热点排行