2014-11-06 60 views
-5

我有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 

这是相同的文件可能是一个简单的解决方法,也许我一直在使用谷歌搜索错误的东西,但我无法解决它。

+1

谷歌“变量范围” – 2014-11-06 17:03:47

+0

我用尽我的列表设置为公开,但只给了我更多的错误.. – Hocho 2014-11-06 17:05:07

+1

你从来没有宣布'vluchtList'。它应该是什么? – Andrew 2014-11-06 17:05:32

回答

0

错误是告诉你,vluchtList尚未定义。

在你的代码行“foreach(vluchtten in vluchtList)”中,你试图访问一个编译器不知道的名为vluchtList的东西。

在您使用vluchtList之前,您需要定义它。它看起来像它应该是一个列表,所以上面的行之前尝试这个办法:

List<Vluchten> vluchtList = new List<Vluchten>(); 

这将定义变量的类型列表,这意味着Vluchten对象的列表。

虽然这是很好的,你的foreach循环将不会出现用它做任何事情,因为它是空的,所以你可能会想一些Vluchten数据填充它:

vluchtList.Add(new Vluchten 
{ 
    FlightNr = "My Flight Number"; 
    FlightCarrier = "My Flight Carrier"; 
    Destination = "My Destination"; 
    maxPassagers = 200; 
    bookedPassagers = 130; 
}); 

这只是增加一个Vluchten对象列表,但你可以添加很多。

+0

谢谢。如果我想在另一个课程中访问,我是否也需要公开此列表? – Hocho 2014-11-06 17:15:33

+0

不,公共修饰符的使用仅确定一个类的属性对其他类的可见性级别。因此,例如,因为您的Vluchten类属性是公共的,您在Vluchten_Boeken类中创建的任何类实例都将被允许访问它们。 – Kevin 2014-11-06 17:18:00

0

3个问题:

  1. 你的列表vluchtList不被任何定义。你必须定义它并用一些Vluchten对象填充它以输出任何内容。
  2. 在你的循环,你应该做你的控制台输出的时候,而不是每次都
  3. 应覆盖ToString()财产上的类,以便它输出一些有意义的事情,输出整个List<Vluchten>从列表中使用当前vluchten对象。如果没有覆盖,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); 
     } 
    } 
} 
相关问题