为什么我得到错误,变量需要是静态的?

时间:2017-01-18 00:01:19

标签: c# .net winforms

在类文件的顶部,我添加了一个类:

public class World
{
    public List<Continent> continents;

    public World()
    {
        continents = new List<Continent>();
    }
}

public class Continent
{
    public string name;
    public List<Country> countries { get; set; }

    public Continent()
    {
        name = string.Empty;
        countries = new List<Country>();
    }
}

public class Country
{
    public string name { get; set; }
    public List<string> imageUrls { get; set; }
    public Country()
    {
        name = string.Empty;
        imageUrls = new List<string>();
    }
}

在form1中使用它时:

foreach (ExtractImages.Continent continent in ExtractImages.World.continents)
{
    Console.WriteLine(continent.name);
    foreach (ExtractImages.Country country in continent.countries)
    {
        Console.WriteLine(country.name);
        foreach (string imageUri in country.imageUrls)
        {
            Console.WriteLine(imageUri);
        }
    }
}

我在form1上遇到错误:

ExtractImages.World.continents

在线:

foreach (ExtractImages.Continent continent in ExtractImages.World.continents)
  

严重级代码描述项目文件行抑制状态   错误CS0120非静态字段,方法或属性'ExtractImages.World.continents'

需要对象引用

更新

首先在类文件中我在使用form1之前使用World: 在Form1中,我为World创建新实例时,它将全部为空。

public void ImagesLinks()
        {
            try
            {
                int counter = 0;

                foreach (string countryCode in countriescodes)
                {
                    Country country = new Country();
                    country.name = countriesnames[counter];
                    List<string> imageUrl = new List<string>();
                    counter++;
                    for (int cnt = 0; cnt < DatesAndTimes.Count(); cnt++)
                    {
                        string imageUrlIrTrue = firstUrlPart + countryCode + secondUrlPart + DatesAndTimes[cnt] + thirdUrlPart + "true";
                        string imageUrlIrFalse = firstUrlPart + countryCode + secondUrlPart + DatesAndTimes[cnt] + thirdUrlPart + "false";
                        imageUrl.Add(imageUrlIrTrue);
                        imageUrl.Add(imageUrlIrFalse);

                        if (cnt == 9)
                        {
                            cnt = 0;
                            break;
                        }
                    }
                    country.imageUrls = imageUrl;
                    Europe.countries.Add(country);

                }
                world.continents.Add(Europe);
            }

现在在Form1中:

ExtractImages.World world = new ExtractImages.World();
            foreach (ExtractImages.Continent continent in world.continents)
            {
                Console.WriteLine(continent.name);
                foreach (ExtractImages.Country country in continent.countries)
                {
                    Console.WriteLine(country.name);
                    foreach (string imageUri in country.imageUrls)
                    {
                        Console.WriteLine(imageUri);
                    }
                }
            }

1 个答案:

答案 0 :(得分:1)

您应首先创建一个类的实例,因为continents实例字段static field,然后访问{{ 1}}。但是,请记住,在构造函数中,您只需创建一个空列表。您没有提供continents的任何列表。因此,每次创建新的World对象时,continenets都将是continents个对象的空列表。你应该提供与空列表不同的东西,因为你帖子中的Continent将毫无意义。

foreach