Error while adding While value in List

1

I'm trying to add the value of a result from my while , then load it into my GridView . But I'm having a problem adding the value to my List<> . You can not add prob1.vTotal to lstTotal .

namespace AvalicaoPratica.View
{
public partial class Home : System.Web.UI.Page
{
    List<_Problema1> lstTotal = new List<_Problema1>();
    protected void Page_Load(object sender, EventArgs e)
    {
       _Problema1 prob1 = new _Problema1();
       while (prob1.Cont <= 64)
       {
           prob1.Vtotal = prob1.Vtotal + prob1.Graos;
           prob1.Graos = prob1.Graos * 2;
           prob1.Cont += 1;
       }

       lstTotal.Add(prob1.Vtotal);

       GridView1.DataSource = lstTotal;
       GridView1.DataBind();
    }
   }
 }

My class

 public class _Problema1
{
    private int vtotal = 0;

    public int Vtotal
    {
        get { return vtotal; }
        set { vtotal = value; }
    }
    private int cont;

    public int Cont
    {
        get { return cont; }
        set { cont = value; }
    }
    private int graos = 1;

    public int Graos
    {
        get { return graos; }
        set { graos = value; }
    }
}
    
asked by anonymous 04.11.2014 / 01:24

1 answer

1

lstTotal is of type List<_Problema1> therefore you can only add elements in this list that are of this type. prob1.Vtotal is not of type _Problema1 . Or you should add some data that is of type _Problema1 or should lstTotal be a list that receives the same type of prob1.Vtotal . Probably what you want is:

namespace AvalicaoPratica.View {
    public partial class Home : System.Web.UI.Page {
        List<_Problema1> lstTotal = new List<_Problema1>();
        protected void Page_Load(object sender, EventArgs e) {
            _Problema1 prob1 = new _Problema1();
            while (prob1.Cont <= 64) {
                prob1.Vtotal += prob1.Graos;
                prob1.Graos *= 2;
                prob1.Cont++;
            }
            lstTotal.Add(prob1); //<== a mudança mais importante aqui
            GridView1.DataSource = lstTotal;
            GridView1.DataBind();
        }
    }
}

I placed GitHub for future reference .

In this case, the GridView1 is getting a list of _Probblema1 and it must be prepared for it. I do not know if it is your desire to receive this, otherwise it will have to adapt to what you want to receive. Maybe you want to show only the total value in GridView1 , okay then just mount it to show only this and ignore the other data. If you want to insist on having only the total value, no problem, but then your list will have to be specified to receive type int and not _Problema1 .

But probably the code must have other problems, it's weird to do this. But there may be a good hidden reason that you can not tell by looking at this passage alone.

As an additional note, you do not follow any naming pattern, especially not following anything commonly recommended. It may seem silly but this makes it difficult to understand and maintain code. Please read in this answer . It's great but it helps a lot to organize code.

    
04.11.2014 / 01:52