C# Foreach IndexOutOfRangeException
我已经在这里尝试了一些解决方案。。没有成功 这是我的代码和下面的错误消息C# Foreach IndexOutOfRangeException,c#,.net,sqlite,C#,.net,Sqlite,我已经在这里尝试了一些解决方案。。没有成功 这是我的代码和下面的错误消息 SQLite.SQLiteConnection connection = new SQLite.SQLiteConnection(dbPath); using (var db = new SQLite.SQLiteConnection(dbPath)) { int i = 0; va
SQLite.SQLiteConnection connection = new SQLite.SQLiteConnection(dbPath);
using (var db = new SQLite.SQLiteConnection(dbPath))
{
int i = 0;
var d = from x in db.Table<stations>() select x;
foreach (var sd in d)
{
pushpin[] Tanke = new pushpin[i];
Tanke[i].Titel = sd.name.ToString(); //IndexOutOfRangeException (see below)
Tanke[i].Text = sd.brand.ToString();
Tanke[i].longitude = sd.longitude;
Tanke[i].latitude = sd.latitude;
MapLayer.SetPosition(Tanke[i], new Location(Tanke[i].latitude, Tanke[i].longitude));
pinLayer.Children.Add(Tanke[i]);
ToolTipService.SetToolTip(Tanke[i], Tanke[i].Titel);
i++;
}
db.Dispose();
db.Close();
}
SQLite.SQLiteConnection connection=new SQLite.SQLiteConnection(dbPath);
使用(var db=new SQLite.SQLiteConnection(dbPath))
{
int i=0;
var d=从db.Table()中的x选择x;
foreach(d中的var sd)
{
图钉[]槽=新图钉[i];
Tanke[i].Titel=sd.name.ToString();//IndexOutOfRangeException(见下文)
Tanke[i].Text=sd.brand.ToString();
Tanke[i].经度=sd.经度;
Tanke[i].纬度=sd.纬度;
设置位置(Tanke[i],新位置(Tanke[i]。纬度,Tanke[i]。经度);
pinLayer.Children.Add(Tanke[i]);
SetToolTip(Tanke[i],Tanke[i].Titel);
i++;
}
db.Dispose();
db.Close();
}
当i为零时,您正在创建一个零元素数组
pushpin[] Tanke = new pushpin[i];
Tanke[i].Titel = sd.name.ToString();
然后使用[0]访问第一个元素。那不行。零元素数组中没有元素。当i为零时,您正在创建零元素数组
pushpin[] Tanke = new pushpin[i];
Tanke[i].Titel = sd.name.ToString();
然后使用[0]访问第一个元素。那不行。零元素数组中没有元素。当i为零时,您正在创建零元素数组
pushpin[] Tanke = new pushpin[i];
Tanke[i].Titel = sd.name.ToString();
然后使用[0]访问第一个元素。那不行。零元素数组中没有元素。当i为零时,您正在创建零元素数组
pushpin[] Tanke = new pushpin[i];
Tanke[i].Titel = sd.name.ToString();
然后使用[0]访问第一个元素。那不行。零元素数组中没有元素。当您在foreach中跳过条目并每次创建零元素数组(i=0)时,无法保存某些内容,因为[0]元素不存在。当您在foreach中跳过条目并每次创建零元素数组(i=0)时无法保存某些内容,因为[0]元素不存在当您在foreach中跳过条目并每次创建零元素数组(i=0)时,无法保存某些内容,因为[0]元素不存在当您在foreach中跳过条目并每次创建零元素数组时(i=0)无法保存某些内容,因为[0]元素不存在问题在于如何创建图钉数组。看起来您不需要该数组,因此请改为: SQLite.SQLiteConnection连接=新的SQLite.SQLiteConnection(dbPath)
使用(var db=new SQLite.SQLiteConnection(dbPath))
{
var d=从db.Table()中的x选择x;
foreach(d中的var sd)
{
var tmp=新的图钉();
tmp.Titel=sd.name.ToString();//IndexOutOfRangeException(见下文)
tmp.Text=sd.brand.ToString();
tmp.longitude=sd.longitude;
tmp.纬度=标准纬度;
设置位置(tmp,新位置(tmp纬度,tmp经度));
pinLayer.Children.Add(tmp);
设置工具提示(tmp,tmp.Titel);
}
db.Dispose();
db.Close();
}
问题在于如何创建图钉阵列。图钉阵列看起来并不需要,因此请执行以下操作:
SQLite.SQLiteConnection连接=新的SQLite.SQLiteConnection(dbPath)
使用(var db=new SQLite.SQLiteConnection(dbPath))
{
var d=从db.Table()中的x选择x;
foreach(d中的var sd)
{
var tmp=新的图钉();
tmp.Titel=sd.name.ToString();//IndexOutOfRangeException(见下文)
tmp.Text=sd.brand.ToString();
tmp.longitude=sd.longitude;
tmp.纬度=标准纬度;
设置位置(tmp,新位置(tmp纬度,tmp经度));
pinLayer.Children.Add(tmp);
设置工具提示(tmp,tmp.Titel);
}
db.Dispose();
db.Close();
}
问题在于如何创建图钉阵列。图钉阵列看起来并不需要,因此请执行以下操作:
SQLite.SQLiteConnection连接=新的SQLite.SQLiteConnection(dbPath)
使用(var db=new SQLite.SQLiteConnection(dbPath))
{
var d=从db.Table()中的x选择x;
foreach(d中的var sd)
{
var tmp=新的图钉();
tmp.Titel=sd.name.ToString();//IndexOutOfRangeException(见下文)
tmp.Text=sd.brand.ToString();
tmp.longitude=sd.longitude;
tmp.纬度=标准纬度;
设置位置(tmp,新位置(tmp纬度,tmp经度));
pinLayer.Children.Add(tmp);
设置工具提示(tmp,tmp.Titel);
}
db.Dispose();
db.Close();
}
问题在于如何创建图钉阵列。图钉阵列看起来并不需要,因此请执行以下操作:
SQLite.SQLiteConnection连接=新的SQLite.SQLiteConnection(dbPath)
使用(var db=new SQLite.SQLiteConnection(dbPath))
{
var d=从db.Table()中的x选择x;
foreach(d中的var sd)
{
var tmp=新的图钉();
tmp.Titel=sd.name.ToString();//IndexOutOfRangeException(见下文)
tmp.Text=sd.brand.ToString();
tmp.longitude=sd.longitude;
tmp.纬度=标准纬度;
设置位置(tmp,新位置(tmp纬度,tmp经度));
pinLayer.Children.Add(tmp)