好的,所以我在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;
那麼究竟是什麼原因造成你的困惑?錯誤信息非常清晰。我問這個問題的原因是因爲你似乎缺乏某些編程基礎知識,但同時你正試圖編寫適度複雜的代碼。 – ChaosPandion 2010-10-04 22:36:37
@ChaosPandion我們都從某個地方開始......記住你來自哪裏,即使你還是小時候必須回去。我看過比這個更糟糕的問題。 – 2010-10-05 01:15:05
@理查德 - 是的,我知道......當我疲憊的時候,我的精神防禦阻止了我成爲一個漏洞。 – ChaosPandion 2010-10-05 01:36:10