我有2个文件,其中一个包含:
namespace Vluchten_Boeken
{
public class Vluchten
{
public string FlightNr;
public string FlightCarrier;
public string Destination;
public int maxPassagers;
public int bookedPassagers;
public Vluchten(string _FlightNr, string _FlightCarrier, string _Destination, int _maxPassagers, int _bookedPassagers)
{
this.FlightNr = _FlightNr;
this.FlightCarrier = _FlightCarrier;
this.Destination = _Destination;
this.maxPassagers = _maxPassagers;
this.bookedPassagers = _bookedPassagers;
}
}
}
另一个文件包含:
namespace Vluchten_Boeken
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void dataGridView1_CellContentClick(object sender, DataGridViewCellEventArgs e)
{
Console.Write("");
foreach (Vluchten Vluchten in vluchtList)
{
Console.WriteLine(vluchtList);
}
}
}
}
但是,我可以轻松地在列表所在的同一个文件中对列表执行foreach
循环,但是当我尝试在另一个文件中执行时,我得到了
Error 1 The name 'vluchtList' does not exist in the current context
这可能很容易解决,也许我一直在搜索错误的内容,但我无法解决这个问题。
答案 0 :(得分:0)
错误告诉您vluchtList尚未定义。
在您的行中#effach(Vluchten Vluchten in vluchtList)",您正在尝试访问编译器不知道的名为vluchtList的内容。
在使用vluchtList之前,您需要定义它。看起来它应该是一个列表,所以在上面的行之前尝试这个:
List<Vluchten> vluchtList = new List<Vluchten>();
这会将变量定义为List类型,即Vluchten对象列表。
虽然这很好,但你的foreach循环似乎不会对它做任何事情,因为它是空的,所以你可能想要用一些Vluchten数据填充它:
vluchtList.Add(new Vluchten
{
FlightNr = "My Flight Number";
FlightCarrier = "My Flight Carrier";
Destination = "My Destination";
maxPassagers = 200;
bookedPassagers = 130;
});
这只是向列表添加一个Vluchten对象,但您可以添加许多。
答案 1 :(得分:0)
3个问题:
vluchtList
未在任何地方定义。您必须定义它并使用一些Vluchten
对象填充它以输出任何内容。vluchten
对象,而不是每次都输出整个List<Vluchten>
ToString()
属性,以便输出有意义的内容。如果没有覆盖,ToString()
(将在Console.WriteLine()
内部调用的内容)只会泄露有关Vluchten
类型的一些信息。以下是一个应该有用的代码示例:
public class Vluchten
{
// Existing code here . . .
// Override the ToString() method
public override string ToString()
{
return string.Format(
"{0} flight #{1} is heading to {2} carrying {3} passengers out of {4} max.",
FlightCarrier, FlightNr, Destination, bookedPassagers, maxPassagers);
}
}
private void dataGridView1_CellContentClick(object sender, DataGridViewCellEventArgs e)
{
// Normally this would be created and populated elsewhere.
// I've put it here for now because it's simpler for this example
var vluchtList = new List<Vluchten>
{
new Vluchten("flight1", "carrier1", "destination1", 10, 1),
new Vluchten("flight2", "carrier2", "destination2", 20, 2),
new Vluchten("flight3", "carrier1", "destination3", 30, 3),
new Vluchten("flight4", "carrier2", "destination1", 40, 4),
new Vluchten("flight5", "carrier1", "destination2", 50, 5),
};
Console.WriteLine("");
// In the real app, where we aren't creating the list above, it's possible
// that there may be no flights. So if the list is empty (or null) we can
// tell the user that there is no info (rather than not outputting anything)
if (vulchtList == null || !vluchtList.Any())
{
Console.WriteLine("There is no flight information to display.");
}
else
{
foreach (Vluchten vluchten in vluchtList)
{
Console.WriteLine(vluchten);
}
}
}