C# 如何创建公共数组?
我无法将阵列能量[]设置为公共。我需要在Timer_Tick事件中使用数组。我已经将数组设置为int,并尝试将它们公开,但没有成功 代码如下:C# 如何创建公共数组?,c#,arrays,sqlite,C#,Arrays,Sqlite,我无法将阵列能量[]设置为公共。我需要在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 partia
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;
}
}
}
}
使用系统;
使用System.Windows.Forms;
使用System.Collections.Generic;
使用System.Linq;
使用系统数据;
使用System.Data.SQLite;
命名空间项目
{
公共部分类FrmGame:表单
{
私人弦乐队;
私有字符串p;
选择私人名单;
公共FrmPartida(字符串组,选定列表)
{
this.selected=selected;
初始化组件();
这个团队=团队;
this.ProgressBar.Value=0;
this.timer1.Interval=100;
this.timer1.Enabled=true;
尝试
{
SQLiteConnection-ocon=新的SQLiteConnection(Config.stringConnect);
dataSet1.Clear();
SQLiteDataAdapter a=新SQLiteDataAdapter(“从球员中选择*,其中Teamplayers='“+team+'”,ocon);
a、 填充(数据集1,“玩家”);
}
捕获(SQLiteException)
{
Show(“出现错误”);
}
统计(数据集1,时间);
}
公共无效统计信息(数据集、字符串组)
{
SQLiteConnection-ocon=新的SQLiteConnection(Config.stringConnect);
DataTable table1=dataset.Tables[0];
string[]PlayerName=新字符串[6];
int[]强度=新int[6];
int[]能量=新int[6];
尝试
{
if(tabela1.Rows.Count!=0)
{
players=selected.Select(r=>r.Cells[1].Value.ToString()).Take(6.ToArray();
}
如果(表1.Rows.Count!=0)
{
Strength=selected.Select(r=>int.Parse(r.Cells[3].Value.ToString()).Take(6.ToArray();
}
如果(表1.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());
内强度H1=强度[0],强度H2=强度[1],强度H3=强度[2],强度H4=强度[3],强度H5=强度[4],强度H6=强度[5];
int Energy1=能量[0],Energy2=能量[1],Energy3=能量[2],Energy4=能量[3],Energy5=能量[4],Energy6=能量[5];
国际球员统计1,球员统计2,球员统计3,球员统计4,球员统计5,球员统计6;
如果((计数/4)=时间)
{
对于(能量1=100;能量1>1;能量1--)
{
PlayerStatistics 1=力量1*能量1*时间;
}
}
PlayerStatistics 1=力量1*能量1*时间;
PlayerStatistics 2=力量1*能量1*时间;
PlayerStatistics 3=力量1*能量1*时间;
PlayerStatistics 4=力量1*能量1*时间;
PlayerStatistics 5=力量1*能量1*时间;
PlayerStatistics 6=力量1*能量1*时间;
if(this.ProgressBar.Value==180)
{
MessageBox.Show(“+Energy1+”);
}
}
捕获(SQLiteException)
{
Show(“出现错误”);
}
}
public void timer1_Tick(对象发送方,事件参数e)
{
如果(此.BarraTempo.Value<180)
{
这个.barratemo.Value++;
如果(BarraTempo.Value==180)
{
MessageBox.Show(“+Energy1+”);/“Energy1”需要出现在此MessageBox上。
FrmBreak f1=新FrmBreak(选择此时间);
f1.ShowDialog();
this.Hide();
}
}
其他的
{
ProgressBar.Enabled=false;
}
}
}
}
如果有人知道如何解决这个问题,请告诉我
谢谢,
Gianlucca.看起来能量是一个局部变量。局部变量没有访问修饰符,因为只有方法执行才能访问它们。考虑把它变成一个字段,比如<代码>团队> />代码>代码>选择< <代码> >
一些旁注:按照命名约定,变量以小写字母开头,例如
energy
。此外,一旦您将其设置为字段,如果您只需要类中的另一个方法来访问它,那么它就不需要是除private
之外的任何内容 因为您的能量
数组是在函数内部声明的,所以只能从该函数内部访问,即它不存在于公共无效统计信息
之外
要使数组公开,需要在类的开头声明它,如下所示:
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
}
}
}
使用系统;
使用System.Windows.Forms;
使用System.Collections.Generic;
使用System.Linq;
使用系统数据;
使用System.Data.SQLite;
命名空间项目
{
公共部分类FrmGame:表单
{
私人弦乐队;
私有字符串p;
选择私人名单;
//使用公共访问修饰符在此处声明数组***
公共国际[]能源=新国际[6];
公共FrmPartida(字符串组,选定列表)
{
//为清晰起见,代码已删除
}
公共无效统计信息(数据集、字符串组)
{
//为清晰起见,代码已删除
}
公共无效计时器1_刻度(对象