如何从每个类的方法中访问变量?

时间:2016-05-13 23:53:51

标签: c# visual-studio

我有一个名为" mode"的int。我想让每个功能都能够访问它。

这是我的代码。

namespace WindowsFormsApplication1
{
    public partial class Form5 : Form
    {
        public Form5()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            int wow = mode - 1;
        }

        private void Form5_Load(object sender, EventArgs e)
        {
            int mode = 4;
        }
    }
}

2 个答案:

答案 0 :(得分:2)

让它成为班级的财产。

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

namespace WindowsFormsApplication1
{
public partial class Form5 : Form
{
    public int mode {get; set;}
    public Form5()
    {
        InitializeComponent();
    }

    private void button1_Click(object sender, EventArgs e)
    {
        int wow = mode - 1;
    }

    private void Form5_Load(object sender, EventArgs e)
    {
        mode = 4;
    }
}
}

答案 1 :(得分:-2)

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

namespace WindowsFormsApplication1
{
public partial class Form5 : Form
{

    public int mode;
    public Form5()
    {
        InitializeComponent();
    }

    private void button1_Click(object sender, EventArgs e)
    {
        int wow = mode - 1;
    }

    private void Form5_Load(object sender, EventArgs e)
    {
        mode = 4;
    }
}
}

但是,如果没有关于此问题的SO页面,我会感到惊讶。另外,我建议查看MSDN和其他编程c#.net资源。

相关问题