2015-06-21 87 views
-3

存在我不断收到错误XXXX不在这个当前上下文

“XXXX不会在这方面目前存在的”。

我知道有很多类似的问题,但他们不帮我,因为我不是很擅长C#& asp.net。 我一直在努力处理这段代码。基本上,我需要有一个计算成本(这是意味着TOT在饮料类完成,并在关于页面上输出。 我的代码是非常凌乱老实说,我旁边不知道我在做什么。

饮料类:

public class Beverage 
    { 
     string fruit; 
     int kg; 
     int cost; 
     int cal; 

     public string getOutputString() 
     { 
      return " Your selected Beverage is made of " + kg + " of " + fruit + ". " + 
      "The cost is £ " + cost + " and has " + cal + "."; 
     } 

     public static int CalculatePrice() 
     { 
      int cost = (TextBox1.text + TextBox2.text); 
      int cal = TextBox1.Text + TextBox3.Text; 
     } 

    } 

关于后面的代码:

public partial class About : Page 
    { 
     protected void Page_Load(object sender, EventArgs e) 
     { 
      MyFruit = Session["Fruitname"] as List<string>; //Create new, if null 
      if (MyFruit == null) 
      MyFruit = new List<string>(); 
      DropDownList1.DataSource = MyFruit; 
      DropDownList1.DataBind(); 
     } 

     protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e) 
     { 

     } 

     protected void Button1_Click(object sender, EventArgs e) 
     { 
      { 
       decimal total = calculatePrice(DropDownList1.SelectedItem.Text, 
               TextBox1.Text.Trim()); 

       lbl1.Text = "You would like " + TextBox1.Text.Trim() + 
        DropDownList1.SelectedItem.Text + "(s) for a total of $" + 
        total.ToString(); 
      } 

     } 
      public List<string> MyFruit { get; set; } 

    } 

的错误是总是TextBox1TextBox2TextBox3 & calculatePrice

+0

不能访问静态方法实例字段。要么使字段静态,要么删除方法中的'static'修饰符。 – CodesInChaos

+0

我没有看到CalculatePrice'的'方法签名在两个类都匹配。也没有超载。我想你应该叫'Beverage.CalculatePrice()''中的事件Button1_Click'。 –

回答

1

你,因为你是在试图访问TextBox1.TextAbout类得到的错误。只有这个类可以访问这个属性,因为它继承System.Web.UI.PageTextBox1.TextBeverage类中没有它的范围。如果您想在该类中使用这些值,请将它们作为输入参数传递给该方法。

相关问题