2016-05-13 65 views
-5

我有一个名为“模式”的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; 
     } 
    } 
} 
+4

这真的是编程101.我会建议查找如何制作对象。 –

+0

在其前面写上“公开” – Ian

回答

-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资源。

+0

[几乎总是有类似的问题。](http://stackoverflow.com/q/36578134/3740093) –

+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 {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; 
    } 
} 
} 
+0

您应该从Form5_Load中的赋值中移除“int”,否则它将隐藏该属性。 –

相关问题