C# 如何增加整数数组的索引值
我想将数据基表中的值存储到数组中。但当我这样做时,出现异常“object eferance not set instance of object”,请查看我的代码 .cod BehindeC# 如何增加整数数组的索引值,c#,arrays,C#,Arrays,我想将数据基表中的值存储到数组中。但当我这样做时,出现异常“object eferance not set instance of object”,请查看我的代码 .cod Behinde public DataSet showoption1() { SqlCommand cmd = new SqlCommand("select * from assessmenttest",con); SqlDataAdapter adptr = new SqlD
public DataSet showoption1()
{
SqlCommand cmd = new SqlCommand("select * from assessmenttest",con);
SqlDataAdapter adptr = new SqlDataAdapter(cmd);
DataSet ds = new DataSet();
adptr.Fill(ds,"test");
int [] arr=new int[10];
DataTable table=ds.Tables[0];
for(int i=0;i<table.Rows.Count;i++ )
{
test.Agree[i] =Convert.ToInt32(ds.Tables[0].Rows[i]["option1"]);
}
公共数据集showoption1()
{
SqlCommand cmd=新的SqlCommand(“从assessmenttest中选择*”,con);
SqlDataAdapter adptr=新的SqlDataAdapter(cmd);
数据集ds=新数据集();
自动填充(ds,“测试”);
int[]arr=新的int[10];
DataTable=ds.Tables[0];
对于(int i=0;itest.agree
为null
)
您需要在字段中放置一个新数组。test.agree
为null
您需要在字段中放置一个新数组。您究竟在哪里得到错误,在哪个数组上?从外观上看,它必须在agree[]数组上,您从未实例化过
试一试
但我想你可能想列一个清单,因为你不知道你需要多少
public static List<int> agree = new List<int>;
您可以像访问数组一样访问它
MessageBox.Show(agree[1].ToString());
你到底在哪里得到错误,在哪个数组上?从外观上看,它必须在agree[]上,你从来没有实例化过
试一试
但我想你可能想列一个清单,因为你不知道你需要多少
public static List<int> agree = new List<int>;
您可以像访问数组一样访问它
MessageBox.Show(agree[1].ToString());
在添加值之前实例化:
SqlCommand cmd = new SqlCommand("select * from assessmenttest", con);
SqlDataAdapter adptr = new SqlDataAdapter(cmd);
DataSet ds = new DataSet();
adptr.Fill(ds, "test");
int[] arr = new int[10];
DataTable table = ds.Tables[0];
test.agree = new int[table.Rows.Count];
for (int i = 0; i < table.Rows.Count; i++)
{
test.agree[i] = Convert.ToInt32(ds.Tables[0].Rows[i]["option1"]);
}
SqlCommand cmd=newsqlcommand(“从assessmenttest中选择*”,con);
SqlDataAdapter adptr=新的SqlDataAdapter(cmd);
数据集ds=新数据集();
自动填充(ds,“测试”);
int[]arr=新的int[10];
DataTable=ds.Tables[0];
test.agree=newint[table.Rows.Count];
for(int i=0;i
在添加值之前实例化:
SqlCommand cmd = new SqlCommand("select * from assessmenttest", con);
SqlDataAdapter adptr = new SqlDataAdapter(cmd);
DataSet ds = new DataSet();
adptr.Fill(ds, "test");
int[] arr = new int[10];
DataTable table = ds.Tables[0];
test.agree = new int[table.Rows.Count];
for (int i = 0; i < table.Rows.Count; i++)
{
test.agree[i] = Convert.ToInt32(ds.Tables[0].Rows[i]["option1"]);
}
SqlCommand cmd=newsqlcommand(“从assessmenttest中选择*”,con);
SqlDataAdapter adptr=新的SqlDataAdapter(cmd);
数据集ds=新数据集();
自动填充(ds,“测试”);
int[]arr=新的int[10];
DataTable=ds.Tables[0];
test.agree=newint[table.Rows.Count];
for(int i=0;i
可能是test或test.agree未初始化…但为什么要使用数组?是否确定行[i]
包含'option1'
?您可能希望在一行中的项目上尝试foreach
语句,以验证您得到的是预期的结果。可能是test或test.agree未初始化…但为什么要使用数组?您确定行[i]
包含'option1'
?您可能希望在一行中的项目上尝试foreach
语句,以验证您是否得到了预期的结果。