-2

I get error when I populate list from class FamilyList BUT: if I populate from Main method, program works fine.

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"});
}
}
Soner Gönül
  • 97,193
  • 102
  • 206
  • 364
Anil
  • 193
  • 3
  • 14
  • u got the error becoz u cannot directly initialize object in ur class. you can initialize either inside a constructor or inside a method – DineshKumar Sep 24 '14 at 11:11

3 Answers3

1

First of all, you need to put your code into a constructor:

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

Second, your FamilyList could implement List<T>, which makes life easier. Your internal list isn't available to the Main method, since it is hidden:

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

Or make the list accessible:

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

Third, you should create an instance of the list in your Main method and pass that into the method:

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

    Family.DisplaySameSex(list);

    // Or this when using the property `Families`:
    //Family.DisplaySameSex(list.Families);
}
Patrick Hofman
  • 153,850
  • 22
  • 249
  • 325
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;
    }
}
mrtsl7
  • 81
  • 4
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;
    }
}

}

Anil
  • 193
  • 3
  • 14