2016-05-13 57 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」,否則它將隱藏該屬性。 –

相關問題