Collection of objects c#

Asked

Viewed 99 times

0

Galera need to fill a list that is inside another list, but is giving error.

Follows the code:

Promotora = new LojaPromotoraInfo 
            { 
                CodGerente = DBUtil.GetValor<string>(oRow, "CodGerente"), 
                CodLojaPromotora = DBUtil.GetValor<string>(oRow["CodLojaPromotora"]),
                Agente = new List<PropostaAgente>()
                {

                }

            },

The properties within Agente do not appear because Agente is a IList.

How can I fill it ?

Thank you

  • your objects Codmanager and Codlojapromotora are being filled correctly? , Which programming language are you using?

  • Yes they are being filled out correctly. I am using C#

  • Okay, you have a class called Propostaagente, does this class already have her data? if not in place of you do the new call a method that returns that class with your data.

1 answer

0


See the example below;

This is an application webforms, but you only need to understand the class part and what you have within the method Page_Load.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace WebApplication1
{
    public partial class _Default : Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            var addlista = new List<PropostaAgente>();

            var lista1 = new PropostaAgente()
            {
                Campo1 = "Campo1",
                Campo2 = "Campo2",
            };

            addlista.Add(lista1);

            var Promotora = new LojaPromotoraInfo
            {
                CodGerente = "CodGerente",
                CodLojaPromotora = "CodLojaPromotora",
                Agente = addlista,
            };

            var teste = Promotora;
        }
    }

    public class LojaPromotoraInfo
    {
        public List<PropostaAgente> Agente { get; internal set; }
        public string CodGerente { get; internal set; }
        public string CodLojaPromotora { get; internal set; }
    }

    public class PropostaAgente
    {
        public string Campo1 { get; internal set; }
        public string Campo2 { get; internal set; }
    }
}

You can change the addlista by a method that returns that list or only tells the list if you already have it.

  • Thanks for the help

Browser other questions tagged

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