当前位置: 代码迷 >> C# >> 枚举变量,代码无语法异常,为什么显示不出来
  详细解决方案

枚举变量,代码无语法异常,为什么显示不出来

热度:66   发布时间:2016-05-05 02:44:17.0
枚举变量,代码无语法错误,为什么显示不出来?


代码如下:

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 WindowsFormsApplication5
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }
        enum Colors { Red = 0, Green = 1, Blue };
        private void Form1_Load(object sender, EventArgs e)
        {
            Colors currentColor, nextColor, lastColor;
            currentColor = Colors.Red;
            nextColor = (Colors)1;
            lastColor = Colors.Blue;
            label1.Text = "现在的颜色是" + currentColor;
            label1.Text = "\n下一个颜色是" + nextColor;
            label1.Text = "\n最后的颜色的枚举变量的值为" +(int) lastColor;
        }
    }
}
------解决思路----------------------
字符串拼接和反复赋值的区别
------解决思路----------------------
label1.Text = "现在的颜色是" + currentColor;
 label1.Text = "\n下一个颜色是" + nextColor;
label1.Text = "\n最后的颜色的枚举变量的值为" +(int) lastColor;

等于是赋值,就把前面的都冲掉了。integer更容易懂些。
int i = 9;
i = 10;  这时候i 就是10了。字符串也是一样。

你需要的是把所哟字符串加起来
label1.Text = "现在的颜色是" + currentColor;
 label1.Text += "\n下一个颜色是" + nextColor;
label1.Text += "\n最后的颜色的枚举变量的值为" +(int) lastColor;

+= 就是在本身的基础上再相加上面的语句等于
label1.Text = "现在的颜色是" + currentColor;
 label1.Text =  label1.Text + "\n下一个颜色是" + nextColor;
label1.Text =  label1.Text + "\n最后的颜色的枚举变量的值为" +(int) lastColor;

不过要是我修改的话会只使用一个赋值
label1.Text = "现在的颜色是" + currentColor +  "\n下一个颜色是" + nextColor + "\n最后的颜色的枚举变量的值为" +(int) lastColor;

------解决思路----------------------
            label1.Text = "现在的颜色是" + currentColor;
            label1.Text += "\n下一个颜色是" + nextColor;
            label1.Text += "\n最后的颜色的枚举变量的值为" +(int) lastColor;
  相关解决方案