junep
junep

Reputation: 2164

Getting value from IEnumerable collection

There are some problems here:

1) Firstly, I've a property in Class1

class Class1
{
    IEnumerable itemSource;

    public IEnumerable ItemSource
    {
        set { itemSource = value; }
        get { return itemSource; }
    }
}

2) a class named TempClass

public class TempClass
{
    int a;
    public int A
    {
        set { a = value; }
        get { return a; }
    }

    public TempClass() { }
    public TempClass(int t) { A = t; }
}

3) In static void Main(string[] args), I tried to this:

ObservableCollection<TempClass> test = new ObservableCollection<TempClass>
{
    new TempClass(1),
    new TempClass(2),
    new TempClass(3)
}

Class1 class1 = new Class1();
class1.ItemSource = test;

foreach (var item in class1.ItemSource)
{
    Console.WriteLine(item);
}

4) Then, the console shows this:

testSolution.TempClass
testSolution.TempClass
testSolution.TempClass

where testSolution is my solution name, so is a name of the namespace.

I want to get values: 1, 2, 3.

Here, what I want to make is a property and method that show values of any collection or list of any class type.

In this case, ItemSource property and method that has:

foreach (var item in class1.ItemSource)
{
    Console.WriteLine(item);
}

How to do it?

Upvotes: 0

Views: 3206

Answers (3)

Bob Dust
Bob Dust

Reputation: 2460

You would need this in TempClass:

        public override string ToString()
        {
            return A.ToString();
        }

For a generic solution, you would need constraint:

    public class Class1<T> where T : Temp
    {
        public IEnumerable<T> ItemSource
        {
            get;
            set;
        }

        public void WriteLine()
        {
            foreach (var item in ItemSource)
            {
                Console.WriteLine(item.A);
            }
        }
    }

    public abstract class Temp
    {
        public abstract int A { get; set; }
    }

    public class TempClass : Temp
    {
        public override int A
        {
            get;
            set;
        }

        public TempClass(int t) { A = t; }
    }

to write the property A:

        ObservableCollection<TempClass> test = new ObservableCollection<TempClass>
        {
            new TempClass(1),
            new TempClass(2),
            new TempClass(3)
        };

        Class1<TempClass> class1 = new Class1<TempClass>();
        class1.ItemSource = test;
        class1.WriteLine();

Upvotes: 1

sujith karivelil
sujith karivelil

Reputation: 29006

Try this :

foreach (var item in class1.ItemSource)
{
    Console.WriteLine(((TempClass)item).A);
}

Second option is, Overriding as the other answer suggests, change the class definition as like this :

public class TempClass
{
   int a;
   public int A
   {
       set { a = value; }
       get { return a; }
   }
   public TempClass() { }
   public TempClass(int t) { A = t; }
   public override string ToString()
   {
       return A.ToString();
   }
}

And print the value like this:

foreach (var item in class1.ItemSource)
{
    Console.WriteLine(item.ToString());
}

Note : You need not to know about the class or properties when iterate over the class1.ItemSource use overrided .ToString() method in each class and print Return what you wished to print on the screen, Lets have another example here:

public class student
 {
     public int id { get; set; }
     public String fName { get; set; }
     public String lName { get; set; }

     public override string ToString()
     {
         return String.Format("Student ID : {0} \n Full Name : {1} {2}", id, fName, lName);
     }
 }

Here in this case the item.ToString() will prints the Student ID : 1 Full Name : suji vasuki

Upvotes: 1

Jaya
Jaya

Reputation: 3911

This may need modification, am just laying out an idea on how to use reflection when the type and properties are unknown. I did not test this code,so feel free to modify

foreach(var objUnknown in ItemSource) {
    foreach(var prop in objUnknown.GetType().GetProperties()) {
        Console.WriteLine("{0}={1}", prop.Name, prop.GetValue(objUnknown, null));
   }
}

Upvotes: 2

Related Questions