当前上下文中不存在名称“…”

我在Main()有一个列表,我正在尝试从变量中添加一个项目到该列表。 但它正在抛出错误“名称’dogList’在当前上下文中不存在”

在我的addDog()方法中,由于上面的原因, dogList.Add()无法正常工作。

 namespace DoggyDatabase { public class Program { public static void Main(string[] args) { // create the list using the Dog class List dogList = new List(); // 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是方法Main本地。 你想要做的是将dogList放在该范围之外。

 public class Program { static List dogList = new List(); ... 

或者,您可以将列表发送到add方法。

主要问题是你已经在main中声明了dogList。 您还将addDog声明为static。 静态方法在当前对象之外。

将Main视为您居住在起居室的起居室。 现在想想addDog作为你的浴室,我站在那里。 我们知道彼此在那里的知识,所以我们无法沟通。

 public class DogDb { // DogDb contains a list of dogs public List dogs { get; set; } public DogDb() { dogs = new List(); } // 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); } 

@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 dogList = new List(); 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; } } } } 

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

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

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

 public static void addDog(string name, int age, string sex, string breed, string colour, int weight, List 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 dogList = new List(); 

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

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 dogList = new List(); }