【问题标题】:The name '...' does not exist in the current context当前上下文中不存在名称“...”
【发布时间】:2017-06-17 20:49:58
【问题描述】:

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

在我的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; }
}

}

【问题讨论】:

标签: c# list methods public


【解决方案1】:

dogList 仅存在于Main 方法的范围内。如果你在一个方法中声明一个变量,它会变成local并且不能在另一个方法中访问。

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

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 的实例才能访问该变量

【讨论】:

    【解决方案2】:

    dogList 是方法 Main 的本地。您想要做的是将dogList 放在该范围之外。

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

    您也可以将列表发送到您的 add 方法中。

    【讨论】:

    • 太棒了!我之前尝试过,但没有添加“静态”,看来我需要真正专注于掌握这些。
    【解决方案3】:

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

    【讨论】:

      【解决方案4】:

      主要问题是您在 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);
      
      }
      

      【讨论】:

        【解决方案5】:

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

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

        【讨论】:

          猜你喜欢
          • 2013-10-02
          • 2014-04-22
          • 2015-09-11
          • 1970-01-01
          • 1970-01-01
          • 2022-12-18
          • 2020-11-02
          • 2011-10-19
          相关资源
          最近更新 更多