2

I have the next object/list "ListaDatos" and I like to get it as clear string (to visualize/send this via mail, etc)

public List<Datos> ListaDatos = new List<Datos>();

public class Datos
{
    public string Numero;
    public string Alias;
    public string URLConsumo;

    //-- Consumos ----------------------------- 
    public List<Consumo> Consumos = new List<Consumo>();
    public string ConsumoTotal;
}

public class Consumo 
{
    public string Tipo;
    public string Subtipo;
    public string Concepto;
    public string Cantidad;
    public string Importe;
    public string Total;
}

What is the easiest way to "render" this object into text to obtain a string variable with something like this:

DATOS
 Numero     : 10
 Alias      : "aaaaa"
 urlConsumo : "www.aaaaaaaaaaaaa"
 Consumos
  Tipo      : "abc"
  SubTtipo  : "aaa"
  ...
DATOS
 Numero     : 10
 Alias      : "aaaaa"
 urlConsumo : "www.aaaaaaaaaaaaa"
 Consumos
  Tipo      : "abc"
  SubTtipo  : "aaa"
  ...
2
  • What have you tried? It this question a programming one, or simply about formatting? Commented Jan 1, 2012 at 14:14
  • I suppose that have to use reflection to see each property on listaDatos and make a string of it, but not sure how is the best approach or if there exist something on the net framework 4.0 that allows me to make this in the best way Commented Jan 1, 2012 at 14:18

3 Answers 3

7

Implement a ToString() method for the Consumo class and then implement a ToString() method for the Datos class using the ToString() method for the Consumo class.

Sign up to request clarification or add additional context in comments.

1 Comment

sounds simple and efective, I was sure that if I was used reflection was overkill :) I will wait some other answer but this seems the easiest way for now. thanks
2

You must override a "ToString" dafault function like:

public class Consumo 
{
    public string Tipo;
    public string Subtipo;
    public string Concepto;
    public string Cantidad;
    public string Importe;
    public string Total;

   public override string ToString()
   {
   return Tipo + " \n " + Subtipo + " \n "... etc;
   }
}

refer to the escape sequences section in C# development guide... http://msdn.microsoft.com/en-us/library/h21280bw.aspx or http://msdn.microsoft.com/en-us/netframework/aa569608

Comments

1

When string representations are used for visualization and similar purposes, the best approach is to override ToString method. Start with the most nested type, and go up the hierarchy. Use ToString of nested objects to implement ToString of outer objects. One very useful method in formatting collections of objects is string.Join(): it lets you format a collection without an explicit loop.

1 Comment

thanks for point me on using string.Join for the lists, is easiest to use "string.Join("\r\n", ListaDatos);" than "ListaDatos.Aggregate("", (current, listaDato) => current + (listaDato.ToString() + "\r\n"));"

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.