-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;
            }
        }    
    }
}

如果有人知道如何解决这个问题,请告诉我。

谢谢,

詹卢卡。

4

2 回答 2

1

它看起来像Energy一个局部变量。局部变量没有访问修饰符,因为只有方法执行才能访问它们。考虑将其改为一个字段,team例如selected

一些旁注:遵循命名约定,变量以小写字母开头,例如energy. 此外,一旦您将其设为字段,则无需将其设置为其他任何内容,private除非您只需要类中的另一个方法来访问它。

于 2012-11-18T23:37:42.993 回答
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
        } 
    }
}

也就是说,与其将其声明为公共字段,不如考虑使用访问器(“get”方法)。

于 2013-01-23T11:00:04.860 回答