【问题标题】:ASP.NET and C# compilation error while attempting to instantiate a new object from a class尝试从类实例化新对象时出现 ASP.NET 和 C# 编译错误
【发布时间】:2010-10-01 05:58:10
【问题描述】:

我在这里有这个简单的 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

【问题讨论】:

  • 顺便说一句,您可以在一个文件中包含多个类,所以我认为这不是问题。
  • 哇!我的错,对此感到抱歉。

标签: c# asp.net compiler-construction mono compilation


【解决方案1】:

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

下面是使用代码的示例:

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

然后在 User.aspx.cs 文件中,您需要命名空间 Example 中的类 User。

【讨论】:

  • 对不起,我对这个很陌生,代码后面,你的意思是把 Page_Load 代码放在一个单独的文件中,然后用代码后面调用它吗?
【解决方案2】:

1) 您应该发布实际的错误消息。
2) 不要让别人为你做作业。
3) 在一个 .cs 文件中定义两个类就可以了。

对不起,我太轻率了。问题是 Page 指令中的“src”标签是没有意义的。 ASP.NET 引擎不知道 CoconutTree 的定义位置。将 CodeFile 用于动态编译的页面。

【讨论】:

  • 这些都不是真正的答案;他们都应该是cmets。最后一个是最接近的答案,但仅此而已。
  • 对不起,我太轻率了。问题是 Page 指令中的“src”标签是没有意义的。 ASP.NET 引擎不知道 CoconutTree 的定义位置。将 CodeFile 用于动态编译的页面。
  • 你为什么不一开始就把它放在答案中?
  • @Bryan:这就是为什么有评论要求发表评论......这适时引发了错误消息被编辑到问题中。那是处理事情的适当方式。我还要补充一点,没有迹象表明这是家庭作业,而不是简单的学习练习。此外,即使它家庭作业,OP 也不会要求我们为他们做。
  • @BOSS,这个问题已经演变成太多的讨论,您现在正在更改您发布的代码。请接受答案并还原问题,以便它反映问题的实际内容。如果您对背后的代码或其他任何问题有疑问,请提出一个新问题。这些 cmets 不是用来提问和回答其他问题的。
猜你喜欢
  • 1970-01-01
  • 2019-11-01
  • 1970-01-01
  • 2017-03-13
  • 2011-07-23
  • 2015-12-20
  • 2017-10-05
  • 1970-01-01
  • 2019-12-31
相关资源
最近更新 更多