Get an item from a referenced list

Asked

Viewed 75 times

0

I am trying to get the items from a generic list that I pass for reference to a class, but I only have as return the name of my project concatenated with the class. Below is a code fragment demonstrating how I am creating and passing the list by reference.

readonly Class.Orders order = new Class.Orders();
readonly Class.Items items = new Class.Items();

  Class ItemsList
  {
    public int ID {get; set;}
    public int Quantidade{get; set;}
  }

  List<Items> list = new List<Items>();

  .
  .
  .

   public void AdicionaItens() //Se produto já existe na lista, apenas somo a quantidade
   {
      int index = list.FindIndex(x=> x.ID == items.prod.ID);
      if(index == -1)
      {
        list.Add (Items {ID = items.prod.ID, Quantidade= items.prod.Quantidade});
      }
      else
      {
        list[index].Quantidade += qtd;
      }
   }

   public void EnviaLista()
   {
     orders.Imprimir(ref list);
   }

Below is the Class I wish to pass the list by reference

StringBuilder sbString = new StringBuilder();

public void Imprimir(ref List<T>ListaSeparado)
{
  foreach(var i in ListaSeparado)
  {
    sbString.AppendLine(i)
  }
}

However when viewing the data added to StringBuilder i have as a result my project name + class name (ProjectTest.ClassTest.ItemList).

What should I do to properly access the list items?

  • Exactly what you wanted to appear?

  • I was trying to get the ID and Quantity of the items in the Print List method...

  • I understood. Another question: this ItemsList would be the same as the Items?

  • No, actually the class items has some queries and validations of the Items... Already the Class ItemsList would be a local class, just so I can create the list with the results of Items... I don’t know if it’s clear...

2 answers

0

sbString.Appendline(i);

When you add the instance in StringBuilder implicitly you are making a i.ToString() to add the object to StringBuilder, so the class and the namespace appears.

An alternative is to change this line of code to this, for example:

sbString.AppendLine($"ID: {i.ID}. Quantidade: {i.Quantidade}");
  • Ronaldo, I tried to implement it this way, but I can’t "see" the properties (ID and Quantity) of the List.

  • Actually, it is a list of generic items. In this case, this method Imprimir, who uses a List<T> parameter...what types of items can come from parameter? Depending, you can use an Interface as parameter, and this interface would have a method string GetDescricao() (or something like), which you would use when giving the AppendLine()

  • @Leonardo succeeded?

  • not yet, but now I’m getting to "see" the list properties using the .ForEach linq, ListaSeparado.ToList().Foreach( p=>{...})

-1

Do the Tostring() method Overload to get a correct string.

class ItemsList
{
   public int ID { get; set; }
   public int Quantidade{ get; set; }

   public override string ToString()
   {
       return $"ID: { ID }. Quantidade: { Quantidade }";
   }
}

In addition, the method Imprimir(ref List<T> ListaSeparado) does not need to be passed using keyword ref

Classes in C# are passed by default as reference, different from structures.

It would also make an addendum as to the class name. It has a name of ItemList but refers to only one item. The List<ItemList> which is actually the list. The class name should be just Item, for example, which would make sense since it is a single item.

  • 1

    Anonymous downvote is always very interesting and useful, thanks to those who did it.

  • 1

    It wasn’t me so I don’t know why the downvote, but what I think which may have been that some people are against changing the ToString() for data formatting. Follow a useful link.

  • I don’t see how to do having a list of generic items. Unless it was a list of an interface that has a function for formatting. Or change the print function to be an Ilist<Itemlist extension>

Browser other questions tagged

You are not signed in. Login or sign up in order to post.