如何在主方法之外的List中填充项目?

时间:2014-09-24 11:04:32

标签: c#

当我从类FamilyList填充列表时出现错误但是:如果我从Main方法填充,程序运行正常。

using System;
using System.Collections.Generic;

namespace ListClassInstance74
{
    class Program
    {
        static void Main(string[] args)
        {
            List<Family> fami = new List<Family>();

        //populate list
        fami.Add(new Family() { name = "Ganpati Prasad", sex = "M", age = 52, occupation = "Under Manager" });
        fami.Add(new Family() { name = "Manju Devi", sex = "F", age = 49, occupation = "Housewife" });
        fami.Add(new Family() { name = "Anil Kumar", sex = "M", age = 27, occupation = "Entrepreneur" });
        fami.Add(new Family() { name = "Sunil Kumar", sex = "M", age = 25, occupation = "Project Executive" });

        //retrieve from list
        Family.DisplaySameSex(fami);
    }
}

public class Family 
{
    public string name { set; get; }
    public string sex { set; get; }
    public int age { set; get; }
    public string occupation { set; get; }

    public static void DisplaySameSex(List<Family> sSex)
    {
        foreach(Family f in sSex)
        {
            if (f.sex == "F")
                Console.WriteLine("Female: " + f.name + "  " + f.sex + "  " + f.age + "  " + f.occupation);

            if (f.age < 30)
                Console.WriteLine("Child: " + f.name + "  " + f.sex + "  " + f.age + "  " + f.occupation);
        }
    }
}

class FamilyList 
{
    //Family slim = new Family();
    ////declare list
    //List<Family> fami = new List<Family>();

    ////populate list
    //fami.Add(new Family(){name = "Ganpati Prasad", snnnsex = "M", slim.age = 52, slim.occupation = "Under Manager"});
}
}

3 个答案:

答案 0 :(得分:1)

首先,您需要将代码放入构造函数中:

class FamilyList 
{
    List<Family> fami = new List<Family>();

    public FamilyList()
    {
        fami.Add(new Family(){name = "Ganpati Prasad", snnnsex = "M", slim.age = 52, slim.occupation = "Under Manager"});
    }
}

其次,您的FamilyList可以实施List<T>,这会让生活更轻松。您的内部列表不可用于Main方法,因为它已被隐藏:

class FamilyList : List<Family>
{
    public FamilyList()
    {
        this.Add(new Family(){name = "Ganpati Prasad", snnnsex = "M", slim.age = 52, slim.occupation = "Under Manager"});
    }
}

或使列表可访问:

class FamilyList 
{
    public List<Family> Families {get; private set;}

    public FamilyList()
    {
        this.Families = new List<Family>();
        this.Families.Add(new Family(){name = "Ganpati Prasad", snnnsex = "M", slim.age = 52, slim.occupation = "Under Manager"});
    }
}

第三,您应该在Main方法中创建列表实例,并将其传递给方法:

static void Main(string[] args)
{
    FamilyList list = new FamilyList();

    Family.DisplaySameSex(list);

    // Or this when using the property `Families`:
    //Family.DisplaySameSex(list.Families);
}

答案 1 :(得分:0)

public class Program
{
    static void Main(string[] args)
    {
        List<Family> fami = Family.FamilyList();
        //retrieve from list
        Family.DisplaySameSex(fami);
    }
}



public class Family
{
    public string name { set; get; }
    public string sex { set; get; }
    public int age { set; get; }
    public string occupation { set; get; }

    public static void DisplaySameSex(List<Family> sSex)
    {
        foreach (Family f in sSex)
        {
            if (f.sex == "F")
                Console.WriteLine("Female: " + f.name + "  " + f.sex + "  " + f.age + "  " + f.occupation);

            if (f.age < 30)
                Console.WriteLine("Child: " + f.name + "  " + f.sex + "  " + f.age + "  " + f.occupation);
        }
    }
    public static List<Family> FamilyList()
    {
        List<Family> fami = new List<Family>();

        ////populate list
        fami.Add(new Family(){name = "Ganpati Prasad", sex = "M",age = 52, occupation = "Under Manager"});

        return fami;
    }
}

答案 2 :(得分:0)

namespace ListClassInstance2_74
{
    class Program
    {
        static void Main(string[] args)
        {
            //call FamilyList to create List
             List<Family> fami = CreateList.FamilyList();
            Family.DisplaySameSex(fami);
        }
    }

public class Family
{
    public string name { set; get; }
    public string sex { set; get; }
    public int age { set; get; }
    public string occupation { set; get; }

    //retrieve info from list
    public static void DisplaySameSex(List<Family> sSex)
    {
        foreach (Family f in sSex)
        {
            if (f.sex == "F")
                Console.WriteLine("Female: " + f.name + "  " + f.sex + "  " + f.age + "  " + f.occupation);

            if (f.age < 30)
                Console.WriteLine("Child: " + f.name + "  " + f.sex + "  " + f.age + "  " + f.occupation);
        }
    }
}

public class CreateList
{
    public static List<Family> FamilyList()
    {
        List<Family> fami = new List<Family>();

        //populate list
        fami.Add(new Family() { name = "Ganpati Prasad", sex = "M", age = 52, occupation = "Under Manager" });
        fami.Add(new Family() { name = "Manju Devi", sex = "F", age = 49, occupation = "Housewife" });
        fami.Add(new Family() { name = "Anil Kumar", sex = "M", age = 27, occupation = "Entrepreneur" });
        fami.Add(new Family() { name = "Sunil Kumar", sex = "M", age = 25, occupation = "Project Executive" });

        return fami;
    }
}

}