Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/292.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 名称'';在当前上下文中不存在_C#_List_Methods_Public - Fatal编程技术网

C# 名称'';在当前上下文中不存在

C# 名称'';在当前上下文中不存在,c#,list,methods,public,C#,List,Methods,Public,我在我的Main()中有一个列表,我正试图从变量中向该列表添加一项。但是它抛出了一个错误“名字‘dogList’在当前上下文中不存在” 在我的addDog()方法中,dogList.Add()由于上述原因无法工作 namespace DoggyDatabase { public class Program { public static void Main(string[] args) { // create the list

我在我的
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; }
}
namespace数据库
{
公共课程
{
公共静态void Main(字符串[]args)
{
//使用Dog类创建列表
List dogList=新列表();
//获取用户输入
控制台。WriteLine(“狗名:”);
字符串inputName=Console.ReadLine();
控制台。WriteLine(“狗的年龄:”);
int-inputAge=Convert.ToInt32(Console.ReadLine());
控制台。WriteLine(“狗的性:”);
字符串inputSex=Console.ReadLine();
控制台。WriteLine(“狗繁殖:”);
字符串inputbride=Console.ReadLine();
控制台。WriteLine(“狗的颜色:”);
字符串inputColour=Console.ReadLine();
控制台。WriteLine(“狗体重:”);
int inputWeight=Convert.ToInt32(Console.ReadLine());
//将输入添加到列表中。
addDog(输入名称、输入日期、输入性别、输入品种、输入颜色、输入重量);
}
公共静态void addDog(字符串名称、整数年龄、字符串性别、字符串品种、字符串颜色、整数重量)
{
//当前上下文中不存在名称“dogList”
添加(新狗()
{
name=name,
年龄=年龄,
性=性,
繁殖,
颜色,
重量=重量
});           
}
}
公家犬
{
公共字符串名称{get;set;}
公共整数{get;set;}
公共字符串sex{get;set;}
公共字符串{get;set;}
公共字符串颜色{get;set;}
公共整数权重{get;set;}
}

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

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

public static void addDog(string name, int age, string sex, string breed, string colour, int weight, List<Dog> dogList) 
也可以在类的作用域中声明变量:

public class Program
{
    // create the list using the Dog class
    static List<Dog> dogList = new List<Dog>();
公共类程序
{
//使用Dog类创建列表
静态列表dogList=新列表();

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

dogList
是方法
Main
的本地变量。相反,您要做的是将
dogList
放在该范围之外

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

...
公共类程序
{
静态列表dogList=新列表();
...

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

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

主要问题是您已经从main内部本地声明了dogList。您还将addDog声明为static。静态方法不在当前对象的范围内

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

}
公共类DogDb
{
//DogDb包含一个狗的列表
公共列表狗{get;set;}
公共数据库(){
狗=新列表();
}
//DogDb可以控制将新狗添加到其狗列表中。
public void addDog(字符串名称、整数年龄、字符串性别、字符串品种、字符串颜色、整数重量)
{               
dogs.Add(新的Dog()
{
name=name,
年龄=年龄,
性=性,
繁殖,
颜色,
重量=重量
});
}
公家犬
{
公共字符串名称{get;set;}
公共整数{get;set;}
公共字符串sex{get;set;}
公共字符串{get;set;}
公共字符串颜色{get;set;}
公共整数权重{get;set;}
}
}
公共课程
{
公共静态void Main(字符串[]args)
{
//创建DogDB类的新实例。
var DogDb=新的DogDb();
//获取用户输入
控制台。WriteLine(“狗名:”);
字符串inputName=Console.ReadLine();
控制台。WriteLine(“狗的年龄:”);
int-inputAge=Convert.ToInt32(Console.ReadLine());
控制台。WriteLine(“狗的性:”);
字符串inputSex=Console.ReadLine();
控制台。WriteLine(“狗繁殖:”);
字符串inputbride=Console.ReadLine();
控制台。WriteLine(“狗的颜色:”);
字符串inputColour=Console.ReadLine();
控制台。WriteLine(“狗体重:”);
int inputWeight=Convert.ToInt32(Console.ReadLine());
//向对象添加输入。
DogDb.addDog(inputName、inputAge、inputsecx、inputbred、inputColour、inputweet);
}

@Ari…以下是您的操作方法

使用系统;
使用System.Collections.Generic;
使用System.Linq;
使用系统文本;
使用System.Threading.Tasks;
命名空间控制台应用程序4
{
名称空间数据库
{
公共课程
{
私有静态列表dogList=新列表();
公共静态void Main(字符串[]args)
{
//使用Dog类创建列表
//获取用户输入
控制台。WriteLine(“狗名:”);
字符串inputName=Console.ReadLine();
控制台。WriteLine(“狗的年龄:”);
int inputAge=Convert.ToInt32(Console.ReadLi
public class Program
{
   List<Dog> dogList = new List<Dog>();  
}
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);

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