简体   繁体   English

C#公共对象列表

[英]C# public list of objects

I'm converting from VB to C# and struggling to workout how to access a public list of objects... 我正在从VB转换为C#并努力研究如何访问公共对象列表......

class Program
{
    public List<players> myListOfPlayers = new List<players>();

    static void Main(string[] args)
    {

        foreach(var player in myListOfPlayers)
        {

        }
    } 

    class players
    {
        public string playerName { get; set; }
        public string playerCountry { get; set; }

    }
}

In my Main module I can't access "myListOfPlayers". 在我的主模块中,我无法访问“myListOfPlayers”。

By design you cannot access a non static member from a static member 按照设计,您无法从静态成员访问非静态成员

From MSDN Static methods and properties cannot access non-static fields and events in their containing type, and they cannot access an instance variable of any object unless it is explicitly passed in a method parameter. 从MSDN Static methods and properties cannot access non-static fields and events in their containing type, and they cannot access an instance variable of any object unless it is explicitly passed in a method parameter.

You need static modifier here 你需要静态修改器

public static List<players> myListOfPlayers = new List<players>();

You need an instance of your Program class: 您需要一个Program类的实例:

  static void Main(string[] args)
    {
        Program p = new Program(); // p is the instance.

        foreach(var player in p.myListOfPlayers)
        {

        }
    } 

This is the equivalent of: 这相当于:

Dim p As New Program 

Alternatively you could make myListOfPlayers static. 或者,您可以将myListOfPlayers静态。

As an additional comment, you should try and follow proper naming conventions, for example: C# classes should have their first letter capitalized. 作为附加注释,您应该尝试遵循正确的命名约定,例如: C#类应该首字母大写。 players should be Players . players应该是Players

You cannot access a non-static context from a static context. 您无法从静态上下文访问非静态上下文。 So try to access the list within your constructor. 因此,尝试访问构造函数中的列表。

class Program
{
    public List<players> myListOfPlayers = new List<players>();

    public Program(){
      foreach(var player in myListOfPlayers)
      {

      }
    } 

static void Main(string[] args)
{
    new Program();

} 

    class players
    {
        public string playerName { get; set; }
        public string playerCountry { get; set; }

    }
}

The variable myListOfPlayer is not static so it only exists within the context of an instance of the class. 变量myListOfPlayer不是静态的,因此它只存在于类的实例的上下文中。 Since the main method is static, it does not exist in the context of an instance, and so it cannot "see" instance members 由于main方法是静态的,因此它不存在于实例的上下文中,因此它不能“看到”实例成员

You need to make you myListOfPlayer static so that you can access it from a instance method. 您需要使myListOfPlayer静态,以便您可以从实例方法访问它。

public static List<players> myListOfPlayers = new List<players>();
class Program
{
    static void Main(string[] args)
    {
        List<players> myListOfPlayers = new List<players>();

        foreach (var player in myListOfPlayers)
        {

        }
    }


}
class players
{
    public string playerName { get; set; }
    public string playerCountry { get; set; }

}

try the above code, it should work. 尝试上面的代码,它应该工作。 let me know if you any question 如果您有任何问题,请告诉我

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM