2012-11-18 27 views
-3

我有一個問題,將數組Energy []設置爲public。我需要在Timer_Tick事件中使用數組。我已經將數組設置爲int,並試圖讓它們公開而沒有成功。如何製作公共陣列?

下面的代碼:

using System; 
using System.Windows.Forms; 
using System.Collections.Generic; 
using System.Linq; 
using System.Data; 
using System.Data.SQLite; 

namespace Project 
{ 
    public partial class FrmGame : Form 
    { 
     private string team; 
     private string p; 
     private List<DataGridViewRow> selected; 
     public FrmPartida(string team, List<DataGridViewRow> selected) 
     { 
      this.selected = selected; 
      InitializeComponent(); 
      this.team = team; 

      this.ProgressBar.Value = 0; 
      this.timer1.Interval = 100; 
      this.timer1.Enabled = true; 


      try 
      { 
       SQLiteConnection ocon = new SQLiteConnection(Config.stringConnect); 
       dataSet1.Clear(); 
       SQLiteDataAdapter a = new SQLiteDataAdapter("Select * From players WHERE Teamplayers = '" + team + "'", ocon); 
       a.Fill(dataSet1, "players"); 
      } 
      catch (SQLiteException) 
      { 
       MessageBox.Show("There was an error"); 
      } 

      Statistics(dataSet1, time); 
     } 

     public void Statistics(DataSet dataset, string team) 
     { 
      SQLiteConnection ocon = new SQLiteConnection(Config.stringConnect); 
      DataTable table1 = dataset.Tables[0]; 

      string[] PlayerName = new string[6]; 
      int[] Strength = new int[6]; 
      int[] Energy = new int[6]; 
      try 
      { 
       if (tabela1.Rows.Count != 0) 
       { 
        players = selected.Select(r => r.Cells[1].Value.ToString()).Take(6).ToArray(); 
       } 
       if (table1.Rows.Count != 0) 
       { 
        Strength = selected.Select(r => int.Parse(r.Cells[3].Value.ToString())).Take(6).ToArray(); 
       } 
       if (table1.Rows.Count != 0) 
       { 
        Energy = selected.Select(r => int.Parse(r.Cells[3].Value.ToString())).Take(6).ToArray(); 
       } 

       int time = int.Parse(timer1.Interval.ToString()); 
       int count = int.Parse(ProgressBar.Value.ToString()); 
       int Strength1 = Strength[0], Strength2 = Strength[1], Strength3 = Strength[2], Strength4 = Strength[3], Strength5 = Strength[4], Strength6 = Strength[5]; 
       int Energy1 = Energy[0], Energy2 = Energy[1], Energy3 = Energy[2], Energy4 = Energy[3], Energy5 = Energy[4], Energy6 = Energy[5]; 
       int PlayerStatistics1, PlayerStatistics2, PlayerStatistics3, PlayerStatistics4, PlayerStatistics5, PlayerStatistics6; 

       if ((count/4) == time) 
       { 
        for (Energy1 = 100; Energy1 > 1; Energy1--) 
        { 
         PlayerStatistics1 = Strength1 * Energy1 * time; 
        } 
       } 
       PlayerStatistics1 = Strength1 * Energy1 * time; 
       PlayerStatistics2 = Strength1 * Energy1 * time; 
       PlayerStatistics3 = Strength1 * Energy1 * time; 
       PlayerStatistics4 = Strength1 * Energy1 * time; 
       PlayerStatistics5 = Strength1 * Energy1 * time; 
       PlayerStatistics6 = Strength1 * Energy1 * time; 

       if (this.ProgressBar.Value == 180) 
       { 
        MessageBox.Show(""+Energy1+""); 
       } 
      } 
      catch (SQLiteException) 
      { 
       MessageBox.Show("There was an error"); 
      } 
     } 

     public void timer1_Tick(object sender, EventArgs e) 
     { 
      if (this.BarraTempo.Value < 180) 
      { 
       this.BarraTempo.Value++; 
       if (BarraTempo.Value == 180) 
       { 
        MessageBox.Show(""+Energy1+""); //"Energy1" needs to appear on this messagebox. 
        FrmBreak f1 = new FrmBreak(this.time, selected); 
        f1.ShowDialog(); 
        this.Hide(); 
       } 
      } 
      else 
      { 
       ProgressBar.Enabled = false; 
      } 
     }  
    } 
} 

如果有人知道如何解決這個問題,請讓我知道。

謝謝,

Gianlucca。

+3

建議您將代碼簡化爲一個簡短的自包含示例。沒有人想閱讀你的其他代碼。 – spender

+0

該數組是在一個函數內部聲明的,爲了公開,它需要是一個類成員 –

回答

1

看起來像Energy是一個局部變量。局部變量沒有訪問修飾符,因爲只有方法執行才能訪問它們。考慮將其設爲一個字段,例如teamselected

一些附註:按照命名約定,變量以小寫字母開頭,例如energy。另外,一旦你把它變成了一個領域,除了private之外,如果你只需要另一種方法來訪問它,就不需要它。

0

因爲您的Energy數組是在函數內部聲明的,所以只能從該函數內部訪問它,即它不存在於public void Statistics之外。

爲了讓你需要在課程開始時,像這樣聲明它的陣列市民:

using System; 
using System.Windows.Forms; 
using System.Collections.Generic; 
using System.Linq; 
using System.Data; 
using System.Data.SQLite; 

namespace Project 
{ 
    public partial class FrmGame : Form 
    { 
     private string team; 
     private string p; 
     private List<DataGridViewRow> selected; 

     //Declare the array here with the public access modifier*** 
     public int[] Energy = new int[6]; 

     public FrmPartida(string team, List<DataGridViewRow> selected) 
     { 
      //Code removed for clarity 
     } 

     public void Statistics(DataSet dataset, string team) 
     { 
      //Code removed for clarity 
     } 

     public void timer1_Tick(object sender, EventArgs e) 
     { 
      //Code removed 
     } 
    } 
} 

這就是說,而不是宣佈它作爲一個公共領域,你應該考慮使用一個accessor(A 「get」方法)。