2010-10-04 75 views
0

好的,所以我在Mac OS X上使用Mono來使用ASP.NET和C#工作簡單的「應用程序」,而且我正在我的代碼有兩個問題。ASP.NET C#「不能隱式地將類型'字符串'轉換爲'int'」錯誤

這裏是我的Default.aspx呈現標記:

<%@ Page Language="C#" Inherits="project1.Tree" %> 
<!DOCTYPE html> 
<html> 
<head runat="server"> 
    <title>project1_test</title> 
</head> 
<body> 
    <form runat="server"> 
     <asp:HiddenField id="hd_Height" runat="server" /> 
     <p><asp:label runat="server" id="lblOutput" /></p> 
     <asp:TextBox ID="txtGrowBy" runat="server" /> 
     <asp:Button id="btnGrow" runat="server" Text="Grow!!!" /> 
    </form> 
</body> 
</html> 

這裏是我隱藏的C#文件:

using System; 
using System.Web; 
using System.Web.UI; 

namespace project1 
{ 
    public partial class Tree : System.Web.UI.Page 
    { 

     private void PersistHeight(int height) 
     { 
      this.Session["height"] = height; 
     } 

     private int RestoreHeight() 
     { 
      return (int)this.Session[0]; 
     } 

     public int Height 
     { 
      get 
      { 
       return int.Parse(hd_Height.Value).ToString(); 
      } 
      set 
      { 
       hd_Height.Value = value.ToString(); 
      } 
     } 
     public int height = 0; 

     public void Grow(int heightToGrow) { 
      height += heightToGrow; 
     } 

     protected void Page_Load(Object Source, EventArgs E)  
     {  
      string msg = "Let's plant a tree!<br/>";   

      msg += "I've created a tree with a height of " +  
      this.height + " metres.<br/>";  

      lblOutput.Text = msg; 
     } 

     public virtual void btnGrowClicked (object sender, EventArgs args) 
     { 
      txtGrowBy.Text = this.heightToGrow; 
     } 

    } 
} 

單給我下面的兩個錯誤:

1)cannot implicitly convert type 'string' to 'int'
line return int.Parse(hd_Height.Value).ToString();

2)Type 'project1.Tree' does not contain a definition for 'heightToGrow' and no extension method 'heightToGrow' of type 'project1.Tree could be found
line txtGrowBy.Text = this.heightToGrow;

+0

那麼究竟是什麼原因造成你的困惑?錯誤信息非常清晰。我問這個問題的原因是因爲你似乎缺乏某些編程基礎知識,但同時你正試圖編寫適度複雜的代碼。 – ChaosPandion 2010-10-04 22:36:37

+0

@ChaosPandion我們都從某個地方開始......記住你來自哪裏,即使你還是小時候必須回去。我看過比這個更糟糕的問題。 – 2010-10-05 01:15:05

+0

@理查德 - 是的,我知道......當我疲憊的時候,我的精神防禦阻止了我成爲一個漏洞。 – ChaosPandion 2010-10-05 01:36:10

回答

4

對於1:Height是int,你不能在它的get中返回一個字符串。
對於2:HeightToGrow不是你的類的字段或屬性,所以你不能像那樣使用它。
我不確定你想要在2中實現什麼,但是你可能想打電話Grow,然後得到樹的高度。

+0

這裏的問題是,你基本上重複的OP發佈的詳細的錯誤信息。 – ChaosPandion 2010-10-04 22:39:32

+0

是的,我想要做的是設置'txtGrowBy.Text'等於'this'或'tree1'的'Grow()'方法的參數。我被告知我不需要創建一個新對象,因爲IIS已經創建了一個對象。那我該怎麼做? – Qcom 2010-10-04 22:44:51

+2

@ChaosPndion:我以爲我在解釋人類可讀語言中的錯誤,以便OP能夠理解他做錯了什麼。我其實仍然這麼認爲。 – 2010-10-04 22:44:53

1

對於第一個錯誤,線

return int.Parse(hd_Height.Value).ToString() 

試圖返回一個字符串,整數類型屬性。

1

對於2:它只是說您沒有名爲「heightToGrow」的字段或屬性。它在Grow方法中定義,但在該方法之外無法訪問。

2
public int Height 
{ 
    get 
    { 
    return int.Parse(hd_Height.Value).ToString(); 
    } 

屬性getter應該返回一個int。 ToString返回一個字符串。編譯器不會將該字符串隱式轉換爲int。刪除對ToString的調用。


public void Grow(int heightToGrow) 
{ 
    height += heightToGrow; 
} 

public virtual void btnGrowClicked (object sender, EventArgs args) 
{ 
    txtGrowBy.Text = this.heightToGrow; 
} 

heightToGrow被聲明作爲另一種方法的參數。 this沒有heightToGrow

1

1)屬性的類型爲int,並且執行的ToString方法返回一個字符串。

或者你的屬性更改爲一個字符串屬性:

public string Height 
    { 
     get 
     { 
      return int.Parse(hd_Height.Value).ToString(); 
     } 
     set 
     { 
      hd_Height.Value = value; 
     } 
    } 

或雖然這樣做{'this.heightToGrow你不要執行的ToString

public int Height 
    { 
     get 
     { 
      return int.Parse(hd_Height.Value); 
     } 
     set 
     { 
      hd_Height.Value = value.ToString(); 
     } 
    } 

2); '你的意思是你有一個名字爲heightToGrow的類的字段......並且我沒有看到它聲明。

聲明該字段:

private string heghtToGrow; 
相關問題