tags:

views:

212

answers:

4

using System;
using System.Collections.Generic;
using System.Text;

namespace ConsoleApplication3Generics
{
    class Program
    {
        static void Main(string[] args)
        {
            ScheduleSelectedItems sitems = new ScheduleSelectedItems("Yusuf");
            ScheduleSelectedItemsList slist = new ScheduleSelectedItemsList();
            slist.Items.Add(sitems);
            Console.Write(slist.Items[0].ToString());
            Console.ReadKey();
        }
    }
    public class ScheduleSelectedItems
    {
        private string Ad;

        public ScheduleSelectedItems(string ad)
        {
            Ad = ad;
        }
    }

    public class ScheduleSelectedItemsList
    {
        public List Items;

        public ScheduleSelectedItemsList()
        {
            Items = new List();
        }
    }
}

how can i add "yusuf" on my Console?

+15  A: 
public class ScheduleSelectedItems
    {
        private string Ad;

        public ScheduleSelectedItems(string ad)
        {
            Ad = ad;
        }

        public override string ToString()
        {
            return this.Ad;
        }
    }
BFree
Thanks Alot BFree!!! Your Assist really help me. Can i ask some Question? How can i do it witout override method my Sir? İ need your great help or tricks :) Thans again...
Phsika
If you don't want to override ToString(), you could always add a GetConsoleString() method and call it instead.
Joel Coehoorn
Also, this is forgetting the missing "Add" method in the other class.
Joel Coehoorn
Actually, in HIS code, his "Items" is public so if that were a List<T> (he has it as just a List, not sure what that is), then you can just access Items directly and call add.
BFree
+2  A: 

You need to override the toString() method of ScheduleSelectedItems to return 'Ad'.

Outlaw Programmer
+3  A: 

Add this to your ScheduleSelectedItems class:

    public override string ToString() {
        return Ad;
    }

That tells the system how such an object should be formatted.

David Schmitt
+5  A: 

What BFree said, with a slight modification to make it singular instead of plural:

public class ScheduleSelectedItem
{
    private string Ad;

    public ScheduleSelectedItem(string ad)
    {
        Ad = ad;
    }
    public override string ToString()
    {
        return this.Ad;
    }
}

Additionally, you want an "Add" method for your list. While you're at it, why not just inherit from the list class:

public class ScheduleSelectedItemsList : List<ScheduleSelectedItem>
{

}

Or you could just create a type alias:

using ScheduleSelectedItemsList = List<ScheduleSelectedItem>;

Either way, you can use the new code like this:

class Program
{
    static void Main(string[] args)
    {
        var slist = new ScheduleSelectedItemsList() 
        { 
            new ScheduleSelectedItem("Yusuf") 
        };

        //write every item to the console, not just the first
        slist.All(item => Console.Write(item.ToString()) );
        Console.ReadKey();
    }
}
Joel Coehoorn