Sibel Yilmaz
Sibel Yilmaz

Reputation: 25

C# Printing a List

So, Im working on some code for class involving lists.

I currently have this

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;


namespace excercise_1
{
class Program
{
    static void Main(string[] args)
    {
        Card c1 = new Card("1112211", "Jhon", "Brown", "Wollongong", 2500, 1500);
        Card c2 = new Card("1111457", "Sibel", "Yilmaz", "Figtree", 3251, 3000);
        Card c3 = new Card("3333333", "Suzan", "Yilmaz", "Gywnville", 3000, 5000);
        Card c4 = new Card("4444444", "Bob", "Brown", "Balgownie", 1457, 2000);

        c1.Print();
        c2.Print();

        List<Card> Cards = new List<Card>();

        Cards.Add(c3);
        Cards.Add(c4);

     }


}

class Card

{
    public string id;
    public string first_name;
    public string family_name;
    public string suburb;
    public int postcode;
    public int balance;

    public Card (string id, string first_name, string family_name, string suburb, int postcode, int balance)
    {
        this.id = id;
        this.first_name = first_name;
        this.family_name = family_name;
        this.suburb = suburb;
        this.postcode = postcode;
        this.balance = balance;
    }

    public void Print()
    {
        Console.WriteLine(this.id);
        Console.WriteLine(this.first_name);
        Console.WriteLine(this.family_name);
        Console.WriteLine(this.suburb);
        Console.WriteLine(this.postcode);
        Console.WriteLine(this.balance);

    }
}
}

I need to be able to print my list called Cards. I have tried a variety of methods, but nothing is working and I'm getting more and more frustrated. If someone is able to help it would be appreciated.

Upvotes: 0

Views: 10779

Answers (5)

LordTitiKaka
LordTitiKaka

Reputation: 2156

1) implement override for ToString() Like http://msdn.microsoft.com/en-us/library/ms173154(v=vs.80).aspx

2) there many ways to iterate over List as many already pointed out above

3) try asking Google it will greatly shorten your time between asking and getting an answer and maybe you'll find a guide or two... like

Upvotes: 1

IL4Miy
IL4Miy

Reputation: 128

I would not write the print method myself. You can use TypeDescriptor for that.

public static void PrintList(IEnumerable<object> values)
{
    foreach (var obj in values)
    {
        string objString = "";
        foreach (PropertyDescriptor descriptor in TypeDescriptor.GetProperties(obj))
        {
            string name = descriptor.Name;
            object value = descriptor.GetValue(obj);
            objString += string.Format(" {0} = {1} ", name, value);

        }
        Console.WriteLine(objString);
    }
}

Upvotes: 0

nsgocev
nsgocev

Reputation: 4470

You can do it with like this:

 Cards.ForEach(x=>x.Print());

Or like this:

foreach (Card card in Cards)
{
    card.Print();
}

Upvotes: 2

loop
loop

Reputation: 9242

Just use Foreach for printing.

 foreach(var card in Cards)
 {
    card.print();
 }

pls :- Make all these public variable private.

public string id; // private
public string first_name; // private
public string family_name; // private
public string suburb; // private
public int postcode; // private
public int balance; // private

Upvotes: 0

Selman Gen&#231;
Selman Gen&#231;

Reputation: 101742

Why don't you use a simple loop ?

foreach(var card in Cards)
   card.Print();

BTW, you might consider overriding ToString for your class instead of Print method.

Upvotes: 0

Related Questions