日期:2014-05-17  浏览次数:20922 次

C# switch()语句分支过多时,有没有别的方式实现呢?
C# code
int flag = 1;
        private void timer1_Tick(object sender, EventArgs e)
        {
            string str = "Properties.Resources._01";
            str.Replace(@"""","");
            switch (flag)
            {
                case 1:
                    this.pictureBox1.Image = Properties.Resources._01;
                    flag = 2;
                    break;
                case 2:
                    this.pictureBox1.Image = Properties.Resources._02;
                    flag = 3;
                    break;
                case 3:
                    this.pictureBox1.Image = Properties.Resources._03;
                    flag = 4;
                    break;
                case 4:
                    this.pictureBox1.Image = Properties.Resources._04;
                    flag = 5;
                    break;
                case 5:
                    this.pictureBox1.Image = Properties.Resources._05;
                    flag = 6;
                    break;
                ......................................................
                default :
                    break;
            }
        }

我要实现的功能是读取保存在Resource文件夹先的图片,然后轮流显示,从而实现动态效果。存在的问题是图片很多的话,switch()语句的分支过多,会不会影响程序效率呢,有没有其他方式来实现呢?

------解决方案--------------------
好吧,又错了
我进了vs,应该是这样:
this.pictureBox1.Image = (Image)Properties.Resources.ResourceManager.GetObject("_0" + flag);
flag++;