0

What is the best way to print out a Dictionary, I created Currency class, it is as below

class Currency{
  public string currency_code { get; set; }
  public float unit { get; set; }
  public string currency_name { get; set; }
  public string currency_isim { get; set; }
  public float forex_buying { get; set; }
  public float forex_selling { get; set; }
  public float banknote_buying { get; set; }
  public float banknote_selling { get; set; }
}

Is there a way to automaticaly print the object ?

I want to print it out like if possible

Dictionary<string, Currency> dict = new Dictionary<string, Currency>();
.....
.....
add data to dict properly
..... 
.....
dict.print();

I want it return a proper string which includes all the data of the object. how can I do that?

Arif YILMAZ
  • 5,754
  • 26
  • 104
  • 189

3 Answers3

7

Code example:

public class Currency
{
  //... Add rest of class here
  public override string ToString()
  {
    return String.Format("Currency Code: {0}\nUnit:{1}...",...);
  }
}

foreach (string key in dict.Keys)
{
  Console.WriteLine("Key: {0}\nValue: {1}", key, dict[key].ToString());
}

Override ToString, loop through all the keys/values, and print.

ntrch
  • 76
  • 1
  • 22
Tawnos
  • 1,877
  • 1
  • 17
  • 26
3

How about a ToString() override in Currency that returns what you need from that class? You would still need to pair that up with the String key in the dictionary.

class Currency {
    public string currency_code { get; set; }
    public float unit { get; set; }
    public string currency_name { get; set; }
    public string currency_isim { get; set; }
    public float forex_buying { get; set; }
    public float forex_selling { get; set; }
    public float banknote_buying { get; set; }
    public float banknote_selling { get; set; }

    public override String ToString()
    {
        StringBuilder sb = new StringBuilder();
        sb.Append(currency_code + ", ");
        sb.Append(unit.ToString() + ", ");
        sb.Append(currency_name + ", ");
        sb.Append(currency_isim + ", ");
        sb.Append(forex_buying.ToString() + ", ");
        sb.Append(forex_selling.ToString() + ", ");
        sb.Append(banknote_buying.ToString() + ", ");
        sb.Append(banknote_selling.ToString());
        return sb.ToString();
    }
}

Formatting of the returned string, of course, can be any way you'd like. Here, it's a comma-separated list, but you could easily replace ", " with "\n" to get a carriage return.

DonBoitnott
  • 10,787
  • 6
  • 49
  • 68
  • This answer includes the whole ToString(), I suggest accepting it. Technically, for this, straight string concatenation would be fastest (http://stackoverflow.com/questions/6785/is-string-format-as-efficient-as-stringbuilder), but this is a nice answer (include the loop code I demoed, and you're golden) – Tawnos Jun 05 '13 at 17:34
0

Make a printing logic somewhere. Then:

foreach (Currency c in dict.Values) {
    // c.Print(), or MyPrinterHandler.Print(c), or whatever...
}

If you're going for serialization to make things easier, make your class serializable. As has said in the comments for the question, the Dictionary type isn't serializable. I don't remember whether it's sealed - if it's not, you could make a serializable child class, though I'd rather go with serializing the class holding your data proper.

Geeky Guy
  • 9,229
  • 4
  • 42
  • 62
  • 2
    As @I4V pointed out, you *can* serialize a dictionary with the `JavaScriptSerializer`, just not the `XmlSerializer`. – Bobson Jun 05 '13 at 17:22