在创建它的函数之外打印列表

时间:2018-02-10 00:20:00

标签: c#

我创建了一个名为Dog的类,我已将其添加到列表中。我想要一个打印功能来输出我创建的列表中的对象。我有它在创建列表的函数内工作,但我想要一个单独的函数,将列表打印到控制台。

static void Main(string[] args)
    {
        //calling working print feature from within the list function
        DogDatabase.dogList();
        Console.ReadLine();
    }
    public class Dog
    {
        //class that is being added to the list
        public string owner;
        public string name;
        public string colour;
        public Dog()
        {
            owner = "unkown";
            name = "unknown";
            colour = "unknown";
        }

        public Dog(string nm, string cl, string ow)
        {
            name = nm;
            colour = cl;
            owner = ow;
        }

        public string talk()
        {
            return "woof woof";
        }

        public string eat(string food)
        {
            return name + "eats" + food;
        }

        public void SetName(string newName)
        {
            name = newName;
        }

        public void SetColour(string newColour)
        {
            colour = newColour;
        }
    }
    public class DogDatabase
    {

        public static void dogList()
        {
            //lists the Dog class from above
            List<Dog> dogs = new List<Dog>();
            dogs.Add(new Dog("Baxter", "blue", "me"));
            dogs.Add(new Dog("Fido", "Black", "peter"));
            //the foreach loops for printing the list
            foreach (Dog printDog in dogs)
            {
                Console.WriteLine(printDog.name + " " + printDog.colour);
            }
        }
        public void print()
        {                
         //I am wanting to be able to call this function instead and print
         // the list.
        }

2 个答案:

答案 0 :(得分:0)

您正在使用DogDatabase类中的静态元数据并在方法中使用本地列表变量。您无法在print方法中访问该变量。

您需要更改DogDatabase类,如下所示。

public class DogDatabase
{
    private static List<Dog> dogs = new List<Dog>();
    public static void dogList()
    {
        //lists the Dog class from above
        dogs.Add(new Dog("Baxter", "blue", "me"));
        dogs.Add(new Dog("Fido", "Black", "peter"));  
    }
    public static void print()
    {                
        //the foreach loops for printing the list
        foreach (Dog printDog in dogs)
        {
            Console.WriteLine(printDog.name + " " + printDog.colour);
        }
    }
}

然后你从main方法调用它如下。

DogDatabase.dogList();
DogDatabase.print();
Console.ReadLine();

答案 1 :(得分:-1)

您应该在更大的范围内定义dogs列表({em>超出dogList方法)。

例如,我在Program课程中定义了狗。现在,您可以使用dogList方法填写它,然后按print方法打印。

代码: Live Demo

using System;
using System.Collections.Generic;

public class Program
{
    private static List<Dog> dogs;

    public static void Main(string[] args)
    {
        //calling working print feature from within the list function
        DogDatabase.dogList();
        DogDatabase.print();
        Console.ReadLine();
    }

    public class DogDatabase
    {
        public static void dogList()
        {
            //lists the Dog class from above
            dogs = new List<Dog>();
            dogs.Add(new Dog("Baxter", "blue", "me"));
            dogs.Add(new Dog("Fido", "Black", "peter"));
        }
        public static void print()
        {                
            //the foreach loops for printing the list
            foreach (Dog printDog in dogs)
            {
                Console.WriteLine(printDog.name + " " + printDog.colour);
            }
        }
     }

    public class Dog
    {
        //class that is being added to the list
        public string owner;
        public string name;
        public string colour;
        public Dog()
        {
            owner = "unkown";
            name = "unknown";
            colour = "unknown";
        }

        public Dog(string nm, string cl, string ow)
        {
            name = nm;
            colour = cl;
            owner = ow;
        }

        public string talk()
        {
            return "woof woof";
        }

        public string eat(string food)
        {
            return name + "eats" + food;
        }

        public void SetName(string newName)
        {
            name = newName;
        }

        public void SetColour(string newColour)
        {
            colour = newColour;
        }
    }   
}