当前上下文中不存在名称为'...'的内容。

3

我在我的Main()函数内有一个列表,我想从变量中添加一项到这个列表中。但是它抛出了错误:"The name 'dogList' does not exist in the current context"

在我的addDog()方法内,由于上述问题,dogList.Add()无法工作。

namespace DoggyDatabase
{
    public class Program
    {
          public static void Main(string[] args)
        {
        // create the list using the Dog class
        List<Dog> dogList = new List<Dog>();

        // Get user input
        Console.WriteLine("Dogs Name:");
        string inputName = Console.ReadLine();
        Console.WriteLine("Dogs Age:");
        int inputAge = Convert.ToInt32(Console.ReadLine());
        Console.WriteLine("Dogs Sex:");
        string inputSex = Console.ReadLine();
        Console.WriteLine("Dogs Breed:");
        string inputBreed = Console.ReadLine();
        Console.WriteLine("Dogs Colour:");
        string inputColour = Console.ReadLine();
        Console.WriteLine("Dogs Weight:");
        int inputWeight = Convert.ToInt32(Console.ReadLine());

        // add input to the list.
        addDog(inputName, inputAge, inputSex, inputBreed, inputColour, inputWeight);          
    }

    public static void addDog(string name, int age, string sex, string breed, string colour, int weight)
    {
        // The name 'dogList' does not exist in the current context
       dogList.Add(new Dog()
        {
            name = name,
            age = age,
            sex = sex,
            breed = breed,
            colour = colour,
            weight = weight
        });           
    }
}

public class Dog
{
    public string name { get; set; }
    public int age { get; set; }
    public string sex { get; set; }
    public string breed { get; set; }
    public string colour { get; set; }
    public int weight { get; set; }
}

}


如果你想在两个或更多的方法中使用同一个 DogList 变量,则需将变量声明为全局变量。 - kgzdev
可能是 http://stackoverflow.com/questions/10270479/the-name-temp-does-not-exist-in-the-current-context-c-desktop-application 的重复问题。 - Rizki Pratama
5个回答

5

dogList 是方法 Main 中的局部变量。你需要做的是将 dogList 放在该作用域之外。

public class Program
{
    static List<Dog> dogList = new List<Dog>();

...

或者您可以将列表发送到您的添加方法中。


太好了!我之前尝试过这个,但没有加上“Static”,看来我需要真正专注于掌握它们。 - Ari

1

dogList 只存在于 Main 方法的范围内。如果您在一个方法中声明变量,它将成为本地变量,无法在另一个方法中访问。

您可以通过将必要的变量作为参数传递来解决这个问题:

public static void addDog(string name, int age, string sex, string breed, string colour, int weight, List<Dog> dogList) 

现在你可以像这样在调用中传递变量:

// add input to the list.
addDog(inputName, inputAge, inputSex, inputBreed, inputColour, inputWeight, dogList);          

或者您可以在类的范围内声明变量:
public class Program
{
    // create the list using the Dog class
    static List<Dog> dogList = new List<Dog>();

在后续版本中,您需要将其声明为静态变量,否则编译器会要求创建该类的实例Program才能访问变量。

1

dogList 变量的作用域仅限于 Main 方法,因此其他类中的方法无法访问它,您有几种方法可以解决这个问题,其中一种解决方案是像这样将 dogList 作为参数传递给该方法:

 // add input to the list.
 addDog(inputName, inputAge, inputSex, inputBreed, inputColour, inputWeight,dogList);

同时,还需要修改addDog方法的签名,使其变为:

public static void addDog(string name, int age, string sex, string breed, string colour, int weight, List < Dog > dogList) 
{
}

如果您不想这样做,另一种解决方案是将您的dogList变量设置为类级别,即像字段一样设置它:
public class Program
{
   List<Dog> dogList = new List<Dog>();  
}

1
主要问题是你在main函数中本地声明了dogList。你还将addDog声明为静态方法。静态方法在当前对象之外。
把Main想象成你所在的客厅。现在把addDog想象成你所在的浴室,我在那里。我们不知道彼此的存在,所以没有办法相互通信。
public class DogDb
{
    // DogDb contains a list of dogs
    public List<Dog> dogs { get; set; }

    public DogDb() {
        dogs = new List<Dog>();
    }
    // DogDb can control adding new dogs to its list of dogs.
    public void addDog(string name, int age, string sex, string breed, string colour, int weight)
    {               

        dogs.Add(new Dog()
        {
            name = name,
            age = age,
            sex = sex,
            breed = breed,
            colour = colour,
            weight = weight
        });
    }

    public class Dog
    {
        public string name { get; set; }
        public int age { get; set; }
        public string sex { get; set; }
        public string breed { get; set; }
        public string colour { get; set; }
        public int weight { get; set; }
    }
}

public class Program
{
      public static void Main(string[] args)
    {

    // Create a new instance of our DogDB class.
    var DogDb = new DogDb();

    // Get user input
    Console.WriteLine("Dogs Name:");
    string inputName = Console.ReadLine();
    Console.WriteLine("Dogs Age:");
    int inputAge = Convert.ToInt32(Console.ReadLine());
    Console.WriteLine("Dogs Sex:");
    string inputSex = Console.ReadLine();
    Console.WriteLine("Dogs Breed:");
    string inputBreed = Console.ReadLine();
    Console.WriteLine("Dogs Colour:");
    string inputColour = Console.ReadLine();
    Console.WriteLine("Dogs Weight:");
    int inputWeight = Convert.ToInt32(Console.ReadLine());

    // add input to the object.
    DogDb.addDog(inputName, inputAge, inputSex, inputBreed, inputColour, inputWeight);

}

1
"

@Ari....以下是您可以操作的方式

"

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

namespace ConsoleApplication4
{
    namespace DoggyDatabase
    {
        public class Program
        {
            private static List<Dog> dogList = new List<Dog>();

            public static void Main(string[] args)
            {
                // create the list using the Dog class                

                // Get user input
                Console.WriteLine("Dogs Name:");
                string inputName = Console.ReadLine();
                Console.WriteLine("Dogs Age:");
                int inputAge = Convert.ToInt32(Console.ReadLine());
                Console.WriteLine("Dogs Sex:");
                string inputSex = Console.ReadLine();
                Console.WriteLine("Dogs Breed:");

                string inputBreed = Console.ReadLine();
                Console.WriteLine("Dogs Colour:");
                string inputColour = Console.ReadLine();
                Console.WriteLine("Dogs Weight:");
                int inputWeight = Convert.ToInt32(Console.ReadLine());

                // add input to the list.
                addDog(inputName, inputAge, inputSex, inputBreed, inputColour, inputWeight);
            }

            public static void addDog(string name, int age, string sex, string breed, string colour, int weight)
            {
                // The name 'dogList' does not exist in the current context
                dogList.Add(new Dog()
                {
                    name = name,
                    age = age,
                    sex = sex,
                    breed = breed,
                    colour = colour,
                    weight = weight
                });
            }
        }

        public class Dog
        {
            public string name { get; set; }
            public int age { get; set; }
            public string sex { get; set; }
            public string breed { get; set; }
            public string colour { get; set; }
            public int weight { get; set; }
        }
    }
}

由于其保护级别,该列表无法访问。当您需要在另一个方法中使用列表时,您必须先声明它。编码愉快。

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接