2

I'm converting from VB to C# and struggling to workout how to access a public list of objects...

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".

1
  • 1
    You have to make an instance of the Program class, Main() is static. Or make the list static too. Commented Mar 11, 2014 at 8:57

5 Answers 5

4

You need an instance of your Program class:

  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.

As an additional comment, you should try and follow proper naming conventions, for example: C# classes should have their first letter capitalized. players should be Players.

Sign up to request clarification or add additional context in comments.

4 Comments

Thank you! I'm sure you don't need to do this within a class in VB.
the question is tagged c# not vb.net
@K.B - Yes, hence my C# answer. I included the VB.NET snippet to show the OP how to achieve a new instance of an object in C#.
Cheers Darren! Very helpful.
4

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.

You need static modifier here

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

Comments

0

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; }

    }
}

Comments

0

The variable myListOfPlayer is not static so it only exists within the context of an instance of the class. Since the main method is static, it does not exist in the context of an instance, and so it cannot "see" instance members

You need to make you myListOfPlayer static so that you can access it from a instance method.

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

Comments

0
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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.