简体   繁体   English

更好地了解范围

[英]Better understanding scope

I'm still very new to C#, but I thought I understood the concept of scope. 我对C#还是很陌生,但是我认为我了解范围的概念。 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 以下代码的问题是第35行失败

"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. 您可以看到对象Mail被实例化为Program类的一部分,并且似乎应该可以全局访问它。 But when I try to use Mail.Add in the InitMail() method, it doesn't recognize the Mail object. 但是,当我尝试在InitMail()方法中使用Mail.Add时,它无法识别Mail对象。

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). 如果将实例化和InitMail代码移到Main()中,它就可以正常工作(尽管我也必须在实例化上删除public修饰符)。 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] { "addr1@contoso.com", "addr2@contoso.com" },
            FailAddr = new string[2] { "addr3@contoso.com", "addr4@contoso.com" }
            });
        }
    }
}
static void InitMail() {
        Mail.Add( new MailList() {
        // properties
        });
    }

This will try to add a new MailList object to Mail . 这将尝试向Mail添加一个新的MailList对象。 However when we look at Mail , we see this declaration: 但是,当我们查看Mail ,我们看到以下声明:

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

Notice the absence of static which is present in InitMail() . 注意InitMail()不存在static InitMail() This means that when the method InitMail() would be executed statically ( Program.InitMail() ), it would try to access the non-static variable Mail . 这意味着当方法InitMail()将被静态执行( Program.InitMail() )时,它将尝试访问非静态变量Mail

Thus the compiler complains. 因此,编译器抱怨。

Mail is an instance field - not a static one. Mail是一个实例字段,而不是静态字段。

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. 实例化Program并在变量上调用InitMail

You are trying to access the instance variable Mail from a static method. 您正在尝试从静态方法访问实例变量Mail

This can not work as you need an object instance of your class Program to access the instance variable 由于您需要类Program的对象实例来访问实例变量,因此无法使用

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

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