views:

87

answers:

2

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

A: 

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.

Bryan
None of these is really an answer; they should all be comments. The last one is *closest* to being an answer, but that's all.
Jon Skeet
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.
Bryan
@Bryan: Okay - I suggest you edit that into the answer then, as that really *would* be helpful.
Jon Skeet
why wouldn't you have just put that in the answer in the first place?
tster
Because the OP didn't include the error message.
Bryan
@Bryan: Which is why there was a comment requesting one... which duly provoked the error message being edited into the question. That was the appropriate way of handling things. I'd also add that there's no indication that this is homework rather than a simple learning exercise. Furthermore, even if it *was* homework the OP wasn't asking for us to just do it for them.
Jon Skeet
Thank you Jon! I really should have posted the error in the beginning but I just completely forgot, sorry. You said it exactly right, though. Just so you know, this wasn't homework at all, like Jon said, I'm just trying to learn.
BOSS
Chastisement acknowledged. BOSS, did you get it working? tster also makes a good point. You can put ALL the code in Tree.cs... and this is good practice. The key is letting ASP.NET know what code/class actually implements your page logic.
Bryan
Well, not yet.. I just use one namespace, right? What's a good name to choose? Also, dose the src have to end in .aspx.cs or does just .cs work fine?
BOSS
Is the name of the .aspx.cs class file dependent on the name of the class? Also, if I have two classes, which should be specified in the inherits""? The super or sub class? I will be posting my code when I'm back on my computer..
BOSS
@BOSS, the "inherits" class shouldn't be the Tree of CoconutTree class. It should be a class which is just for that page (think of it as the controller for the page). Inside that class is where the Page_Load method should go.
tster
So, the class would actually be the first thing in my <script> tag in the ASP.NET page?
BOSS
@BOSS, don't use a <script> tag at all.
tster
What would you replace it with? Don't you need it to separate the style, markup, and script?
BOSS
Have you considered downloading the free ASP.NET development IDE from Microsoft? It does a good job of creating a base template for you, which seems to be the root cause of your troubles here. You are missing some basic connections here which the right tool will make for you automatically. Specifically, the Inherits tag must point to a Page-derived class in your code if you want to put Page_Load in codebehind. And the CodeFile tag must point to your file if you want it to compile dynamically. The .aspx.cs pattern is just a naming convention.
Bryan
Well, I am running a Mac! :) Temporarily of course, but still for a little while. Of course when I get a PC, I'll be using that. Check out the updated code I posted.
BOSS
@BOSS, this question has morphed into too much of a discussion, and you are now changing the code you posted. Please accept an answer and revert the question so it reflects what the question actually was. If you have questions about code behind or any thing else please ask a new question. These comments are not for asking and answering additional questions.
tster
Alright, sorry about that.
BOSS
+2  A: 

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.

tster
Sorry, I'm very new to this, by code behind, do you mean to put the Page_Load code in a separate file and call it with Code Behind?
BOSS