The name '…' does not exist in the current context

匿名 (未验证) 提交于 2019-12-03 02:03:01

问题:

I have a list within my Main() and I'm trying to add an item to that list from within a variable. But it's throwing the error "The name 'dogList' does not exist in the current context"

Inside my addDog() method, dogList.Add() is not working due to above.

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

}

回答1:

dogList is local to the method Main. What you want to do instead is to place dogList outside of that scope.

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

Alternately you can send the list into your add method.



回答2:

The main problem is that you have declared dogList locally from within main. You have also declared addDog as static. Static methods are outside of the current object.

Think of Main as your living room you are standing in your living room. Now think of addDog as your bathroom I am standing in there. We have know knowledge that each other is there so there is no way of us to communicate.

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


回答3:

@Ari....Here is how you can do it

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

The list was inaccessible due to its protection level.When you have to use a list in another method then you have to declare it first.Happy coding



回答4:

dogList exists only in the scope of the Main method. If you declare a variable in one method it becomes local and cannot be accessed in another method.

You could solve it by passing the necessary variable as a parameter:

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

now you pass the variable in the call like this:

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

or you can declare the variable at the scope of the class:

public class Program {     // create the list using the Dog class     static List dogList = new List(); 

In the latter version you need to declare it as static, otherwise the compiler will demand an Instance of the class Program to be able to access the variable



回答5:

The dogList variable is scoped local to the Main method, so it is not accessible to other method in your class, you have few ways to make it correct, one solution can be to pass the dogList as well as parameter to that method like:

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

and change the signature of addDog method as well to be :

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

If you don't want to do that way, another solution can be to make your dogList variable at class level i.e. make it field like:

public class Program {    List dogList = new List();   } 


标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!