Haytham
Haytham

Reputation: 3

Pass a different list types to method and loop through it like array

// i have this class

    public class SCHOOL
    {
        public int ID { get; set; }
        public string Name { get; set; }
        public string Country{ get; set; }
        public decimal Total{ get; set; }
    }

// and another class with different type

    public class CLASS_2
    {            
        public string Student { get; set; }            
        public DateTime Graduate { get; set; }            
    }

// and may bee i add class 3 and 4 with different type

// i fill the lists with my data

  static void Main()
  {
    List < SCHOOL> FirstClass = new List < SCHOOL>();

    FirstClass.Add( new SCHOOL{ID=1,Name="aaa",County="USA",Total=10});
    FirstClass.Add( new SCHOOL{ID=1,Name="bbb",County="JAP",Total=7});
    FirstClass.Add( new SCHOOL{ID=1,Name="ccc",County="GBR",Total=5});


    List < CLASS_2 > SecondClass = new List < CLASS_2 >();

    SecondClass.Add( new CLASS_2 {Student =1, Graduate ="2/6/2015"});
    SecondClass.Add( new CLASS_2 {Student =1, Graduate ="2/4/2015"});
    SecondClass.Add( new CLASS_2 {Student =1, Graduate ="2/8/2015"});
  }

// i want to pass the first List and loop through my data

    GetmyData ( firstClass);

// and also pass another List with different Class type to the same method and also loop through my data GetmyData ( SenecdClass );

// i want one method to get the list and loop throught the data like array

private void GetmyData <T> ( List<T> newlist ) 
{

for (int y=0; y < newList.Count; y++)
{
     for ( int x=0 ; x < newLsit[y].Colms; x++ )
     {
         Console.WriteLine ( newList[y][x].value );
     }
}             
}

Upvotes: 0

Views: 847

Answers (2)

Haytham
Haytham

Reputation: 3

static void GetMyData(List theList) {

        int count = typeof(T).GetProperties().Count();

        for ( int y = 0 ; y < theList.Count ; y++ )
        {  
            for ( int x = 0; x < count; x++ )
            {
                var propertyName = typeof(T).GetProperties()[x].Name;
                var propertyValue = typeof(T).GetProperties()[x].GetValue( theList[y] , null );
                var propertyType = typeof(T).GetProperties()[x].PropertyType;

                Console.WriteLine(propertyType + " " + propertyName +  " " + propertyValue );
            }                 
        }
    }

Upvotes: 0

MikeW
MikeW

Reputation: 1630

I would say that firstly you need to use a generic method:

private void GetMyData(List<T> the List)
{
    foreach (T entry in theList)
    {
        //deal with the entry on the list
    }
}

and for the printing every property of the class, the best way in my eyes would be to override ToString

but if you need to access each property for something other than just displaying it will require reflection:

private void GetMyData(List<T> the List)
{
    foreach (T entry in theList)
    {
        foreach (var property in typeof(T).GetProperties())
        {
            var propertyName = property.Name;
            var propertyValue = property.GetValue(entry);
            Console.WriteLine("Value of " + propertyName + " is " + propertyValue);
        }
    }
}

but bear in mind that not all properties can be read.

Upvotes: 2

Related Questions