C# 如何使变量可以从类的每个方法访问?
我有一个名为“mode”的int。我想让每个函数都能访问它 这是我的密码C# 如何使变量可以从类的每个方法访问?,c#,visual-studio,C#,Visual Studio,我有一个名为“mode”的int。我想让每个函数都能访问它 这是我的密码 namespace WindowsFormsApplication1 { public partial class Form5 : Form { public Form5() { InitializeComponent(); } private void button1_Click(object sender, Eve
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;
}
}
}
然而,如果没有一页关于这一点的文章,我会感到惊讶。此外,我还建议您查看MSDN和其他c#.net编程资源 只需将其作为类的属性即可
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;
}
}
}
这真的是编程101。我建议您查看如何创建对象。在它前面写“public”,我们不会对此问题提供任何解释-没有帮助。您应该在Form5_Load中从赋值中删除
int
,否则它将隐藏属性。
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;
}
}
}