Ken Morley
Ken Morley

Reputation: 35

Better understanding scope

I'm still very new to C#, but I thought I understood the concept of scope. I'm having a problem with a program and I would really appreciate some help.

The problem with the following code is that Line 35 fails with

"An object reference is required for the non-static field, method, or property".

You can see that object Mail is instantiated as part of the Program class and it seems like it should be globally accessible. But when I try to use Mail.Add in the InitMail() method, it doesn't recognize the Mail object.

If I move the instantiation and InitMail code into Main(), it works just fine (although I also have to remove public modifier on the instantiation). What am I not understanding here?

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;


namespace TestApp1103
{
    class Program
    {
        // Define an enum type named "Division" specifying all possible values:
        public enum Division {PFR, PSE, PVF, PVM, PVS}

        //Define a generic class named "MailList" and specify accessor methods:
        public class MailList
        {
            public Division Div { get; set;}
            public string[] SuccAddr { get; set; }
            public string[] FailAddr { get; set; }
        }

        // Instantiate a MailList object named "Mail":
        public List<MailList> Mail = new List<MailList>();

        static void Main(string[] args)
        {
            // Populate the object "Mail":
            InitMail();
        }

        static void InitMail()
        {
            Mail.Add( new MailList()
            {
            Div = Division.PFR,
            SuccAddr = new string[2] { "[email protected]", "[email protected]" },
            FailAddr = new string[2] { "[email protected]", "[email protected]" }
            });
        }
    }
}

Upvotes: 0

Views: 138

Answers (3)

thumbmunkeys
thumbmunkeys

Reputation: 20764

You are trying to access the instance variable Mail from a static method.

This can not work as you need an object instance of your class Program to access the instance variable

Upvotes: 0

Jeroen Vannevel
Jeroen Vannevel

Reputation: 44449

static void InitMail() {
        Mail.Add( new MailList() {
        // properties
        });
    }

This will try to add a new MailList object to Mail. However when we look at Mail, we see this declaration:

public List<MailList> Mail = new List<MailList>();

Notice the absence of static which is present in InitMail(). This means that when the method InitMail() would be executed statically (Program.InitMail()), it would try to access the non-static variable Mail.

Thus the compiler complains.

Upvotes: 1

Oded
Oded

Reputation: 499132

Mail is an instance field - not a static one.

That means it belongs to instances of the class it is declared on - which there are none.

There are a couple of ways to go about fixing the issue:

  1. Make the field static.

  2. Instantiate Program and call InitMail on the variable.

Upvotes: 0

Related Questions