日期:2014-05-18  浏览次数:20709 次

请数组初始化的问题
出现下面的错误,请问怎么解决,谢谢
Error 2 Use of unassigned local variable 'BNID'


  string[] BNID;
  int i = 0;
  string[] TITLE_BN;
  using (SQLiteConnection cn = new SQLiteConnection("Data Source=customer.sqlite"))
  {
  cn.Open();
  using (SQLiteCommand cmd = new SQLiteCommand())
  {
  cmd.Connection = cn;
  cmd.CommandText = string.Format("select ID,TITLE_BNID from BNIDS");
  SQLiteDataReader reader = cmd.ExecuteReader();
  while (reader.Read())
  {
  Console.WriteLine(String.Format("{0}", reader["ID"]));
BNID[i] = String.Format("{0}", reader["ID"]);
  i++;
  }
  }
  }

------解决方案--------------------
使用了为赋值的局部变量
第一,要么 string[] BNID = new string[10];10可以自定义长度
第二,在while前,声明 BNID并初始化,因为那时已经知道要分配多少长度