简体   繁体   中英

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?)

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());
        }
 }

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM