简体   繁体   中英

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

I have this simple ASP.NET page here:

<%@ 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>

With this simple class:

 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;
    }
  }
}

UPDATE UPDATE:

Parser Error

Description: Error parsing a resource required to service this request. Review your source file and modify it to fix this error.

Parser Error Message: Cannot find type CoconutTree.One

I would just do the Page_Load in code behind and be done with it. I don't know what silliness is happening with the mono compiler using page imports in the aspx file.

Here is an example of using code behind:

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

Then in the User.aspx.cs file you would need a class User in namespace Example.

1) You should post the actual error message.
2) Don't ask people to do your homework for you.
3) Defining two classes in one .cs file is fine.

Sorry, I was being flippant. The problem is that the "src" tag in the Page directive is meaningless. The ASP.NET engine has no idea where CoconutTree is defined. Use CodeFile for dynamically compiled pages.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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