user3021598
user3021598

Reputation: 35

does not contain a definition defination

After i declare this, this is the error i get

protected void Page_Load(object sender, EventArgs e)
{
        List<String> LabelTextList = new List<String>();
         dr = cmd.ExecuteReader();
        while (dr.Read())
        {
        LabelTextList.add(dr[0].ToString());
        }
 }

Error 1 'MasterPage_Profile' does not contain a definition for 'LabelTextList' and no extension method 'LabelTextList' accepting a first argument of type 'MasterPage_Profile' could be found (are you missing a using directive or an assembly reference?)

[UPDATE] Now it says :

'System.Collections.Generic.List' does not contain a definition for 'add' and no extension method 'add' accepting a first argument of type 'System.Collections.Generic.List' could be found (are you missing a using directive or an assembly reference?)

Upvotes: 1

Views: 2502

Answers (2)

JaredPar
JaredPar

Reputation: 754715

To fix this change it to the following

LabelTextList.Add(dr[0].ToString());

The LabelTextList value is a local variable definition. When you prefix an expression with this. it tells the compiler to look for members of the value, not locals.

Here is a counter example with a field named LabelTextList that works with this.

List<String> LabelTextList = new List<String>();
protected void Page_Load(object sender, EventArgs e)
{
   dr = cmd.ExecuteReader();
   while (dr.Read())
   {
      this.LabelTextList.Add(dr[0].ToString());
   }
}

Also if you keep the value as a local the standard naming pattern would be labelTextList instead of LabelTextList. This isn't required by the language but is the preferred style.

Upvotes: 2

Jakub Konecki
Jakub Konecki

Reputation: 46008

Remove this - LabelTextList is a local variable.

protected void Page_Load(object sender, EventArgs e)
{
        List<String> LabelTextList = new List<String>();
         dr = cmd.ExecuteReader();
        while (dr.Read())
        {
            LabelTextList.add(dr[0].ToString());
        }
 }

Upvotes: 4

Related Questions