Error adding While value in List<>

Asked

Viewed 75 times

1

I’m trying to add the value of a result of mine while, and then carry it on my GridView. But I’m having a problem adding value to mine List<>. Does not accept adding prob1.vTotal in 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; }
    }
}
  • Prefer to post code as text, it is easier for people to work with it. And what’s the problem? It does not accept add prob1.vTotal in lstTotal?

  • Yeah, that’s the problem.

  • And what is the composition of _Problema1? prob1.vTotalis a numerical type, a int, Decimal or something like that, right?

  • _Problema1 is my class, where to define my 3 variables as int type.

  • I know, but I’d need to know what her composition is to help you.

  • Okay, I posted my class code.

Show 2 more comments

1 answer

1


lstTotal is the type List<_Problema1> so you can only add elements in this list that are of this type. prob1.Vtotal is not the type _Problema1. Or you should add some data that is of the type _Problema1 or must do 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 put in the Github for future reference.

In this case the GridView1 is receiving a list of _Probblema1 and he must be prepared for this. I do not know if it is his desire to receive this, if it is not he will have to adapt to what he wants to receive. Maybe you just want to show the total value on GridView1, OK then just assemble it to show 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 the type int and not _Problema1.

But the code probably has other problems. But there might be a good hidden motive that you can’t tell by just looking at this stretch.


As an additional note, you do not follow any standard nomenclature, mainly you do not follow anything commonly recommended. It may sound silly but it makes it difficult to understand and maintain in code. Take a read in that reply. It’s big but it helps a lot to organize code.

  • I did it that way, but it didn’t load anything into my Gridview.

  • I solved the problem you described. I didn’t even compile before. Now you have another problem and with the past information you can’t solve. I suggest posting another question passing the relevant data so that we can understand what your GridView1 ago.

Browser other questions tagged

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