[英]Index out of bound of the array
i have this two methods to look for an object in a list of products, but when i try to ask to select one object to see i get the index out of bounds array(sorry that some of the code is in spanish)我有这两种方法可以在产品列表中查找对象,但是当我尝试要求选择一个对象来查看时,我的索引超出了数组范围(抱歉,某些代码是西班牙语)
Public Producto BuscarProducto(int id,List<Producto> prod)
{
var productos = ObtenerProducto();
var p = (from producto in productos
where producto.Id == id
select producto).First();
return p;
}
public List<Producto> ObtenerProducto()
{
var datos = ObtenerLineas();
List<Producto> productos = new List<Producto>();
foreach (var item in datos)
{
string[] info = item.Split(',');
Producto producto = new Producto
{
Id = int.Parse(info[0]),
Nombre = info[1],
Precio = double.Parse(info[2]),
Categoria = info[3],
Detalle = info[4]
};
productos.Add(producto);
}
return productos;
}
Make sure that there are four "," in the string that you are trying to split.确保您尝试拆分的字符串中有四个“,”。
Also it would be better to check if there are atleast 5 elements in the array.此外,最好检查数组中是否至少有 5 个元素。 You could try replacing your Add code with this checking.
您可以尝试使用此检查替换您的添加代码。
if (info != null && info.Length >= 5)
{
Producto producto = new Producto
{
Id = int.Parse(info[0]),
Nombre = info[1],
Precio = double.Parse(info[2]),
Categoria = info[3],
Detalle = info[4]
};
productos.Add(producto);
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.