繁体   English   中英

尝试从类实例化新对象时,ASP.NET和C#编译错误

[英]ASP.NET and C# compilation error while attempting to instantiate a new object from a class

我在这里有这个简单的ASP.NET页面:

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Tree.aspx.cs" Inherits="CoconutTree.One" %>    
<html>    
<head>    
<title>Planting Trees</title>    
<script runat="server">    
 protected void Page_Load(Object Source, EventArgs E)    
{    
string msg = "Let's plant some trees!<br/>";    

// Create a new Tree    
Tree tree1 = new Tree();    

msg += "I've created a tree with a height of " +    
tree1.height + " metre(s).<br/>";    

tree1.Grow(3);    

msg += "After a bit of growth, it's now up to " +    
tree1.height + " metre(s) tall.<br/>";    

msg += "Maybe eventually it will grow to 10 meters tall!</br>…<br/>";

tree1.Grow(7);

msg += "*15 years later*<br/>Let's check out our tree's height now!  It's now up to " + tree1.height + " meter(s) tall!  Awesome!<br/>";

Output.Text = msg;   

string msg2 = "Let's plant some coconut trees!<br/>";

// Create a new Tree
CoconutTree coconutTree1 = new CoconutTree();

msg2 += "I've created a tree with " + coconutTree1.numNuts + " coconuts.<br/>";

coconutTree1.GrowNut(10);

ms2 += "I've now grown " + coconutTree1.numNuts + " coconuts on our tree.<br/>";

Output2.Text = msg2;

   }
</script> 

</script>    
</head>    
<body>    
<p><asp:label runat="server" id="Output" /></p>
<p><asp:label runat="server" id="Output2" /></p> 
</body>    
</html>

通过这个简单的类:

 namespace One
{
  public class Tree {

    public int height = 0;

    public void Grow(int heightToGrow) {
      height += heightToGrow;
    }
  }
    public class CoconutTree : Tree {

    public int numNuts = 0; //Number of coconuts

    public void GrowNut(int numberToGrow) {
      numNuts += numberToGrow;
    }

    public void PickNut(int numberToPick) {
      numNuts -= numberToPick;
    }
  }
}

更新更新:

解析器错误

说明:解析服务于此请求所需的资源时出错。 查看您的源文件并对其进行修改以解决此错误。

解析器错误消息:找不到类型CoconutTree.One

我只是在后面的代码中执行Page_Load并完成它。 我不知道使用aspx文件中的页面导入的Mono编译器正在发生什么愚蠢的事情。

这是在后面使用代码的示例:

<%@ Page Title="" Language="C#" AutoEventWireup="true" 
    CodeBehind="User.aspx.cs" Inherits="Example.User" %>

然后,在User.aspx.cs文件中,您将需要在Namespace Example中创建一个User类。

1)您应该发布实际的错误消息。
2)不要要求别人为您做功课。
3)在一个.cs文件中定义两个类是可以的。

抱歉,我当时很轻率。 问题是Page指令中的“ src”标记是没有意义的。 ASP.NET引擎不知道在何处定义CoconutTree。 将CodeFile用于动态编译的页面。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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