2015-06-21 84 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類中沒有它的範圍。如果您想在該類中使用這些值,請將它們作爲輸入參數傳遞給該方法。

相關問題