views:

125

answers:

5

OK, so I'm using Mono on Mac OS X to work simple "applications" using ASP.NET and C# and I'm having two issues with my code.

Here is my Default.aspx presentation markup:

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

And here is my CodeBehind C# file:

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

    }
}

Mono gives me the following two errors:

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;

+4  A: 

For 1: Height is int, you can't return a string in it's get.
For 2: HeightToGrow is not a field or property of your class, so you can't use it like that.
I'm not sure what you were trying to achieve in 2, but you may want to call Grow, and then get the tree's height.

Oren A
The issue here is that you are essentially repeating the detailed error message that the OP posted.
ChaosPandion
Yeah, what I'm trying to do, is set the `txtGrowBy.Text` equal to the parameter of the `Grow()` method of `this` or `tree1`. I was told that I didn't need to create a new object, because IIS already creates one. How do I do this, then?
BOSS
@ChaosPndion: I thought I was explaining the errors in human readable language, so that the OP can understand what he was doing wrong. I actually still think so.
Oren A
@BOSS: the parameter of Grow(..) is heightToGrow. You'll have to get it from somewhere (e.g. a textbox). the sentence that contains "IIS" makes no sense, unless you replace "IIS" with ASP.NET - but even so, you'll still need to get the parameter for the method from somewhere.
Oren A
@Oren - Maybe but it seems that @BOSS is missing so many fundamental concepts such as the idea of a type and a member of a type. Knowing what these things means is very important. It is kind of like picking up some paint and an easel and simply painting. Sure you might learn a thing or two but all the other painters that took the time to understand their craft will be light years ahead of you in no time.
ChaosPandion
@Chaos: They're very important. I didn't use those two terms. I think he got my remarks.
Oren A
@Oren - Meh, don't listen to me. I probably take my craft way too seriously.
ChaosPandion
Haha, thanks guys!
BOSS
@BOSS: np, gl .
Oren A
+1  A: 

For the first error, the line

return int.Parse(hd_Height.Value).ToString()

is trying to return a string, for an integer type property.

DJ Quimby
+1  A: 

For 2: It's just saying that you have no field or property named "heightToGrow". It is defined in the Grow method, but that's not accessible outside that method.

Larsenal
+2  A: 
public int Height 
{ 
  get 
  { 
    return int.Parse(hd_Height.Value).ToString(); 
  } 

The property getter should return an int. ToString returns a string. The compiler will not implicitly convert that string to an int. Remove the call to ToString.


public void Grow(int heightToGrow)
{ 
  height += heightToGrow; 
} 

public virtual void btnGrowClicked (object sender, EventArgs args) 
{ 
  txtGrowBy.Text = this.heightToGrow; 
}

heightToGrow is declared as a parameter for another method. this does not have a heightToGrow.

David B
+1  A: 

1) the property is of type int, and the ToString method tha you execute returns a string.

Or you change the property to a string property:

public string Height 
    { 
        get 
        { 
           return int.Parse(hd_Height.Value).ToString(); 
        } 
        set 
        { 
            hd_Height.Value = value;
        } 
    } 

or you don't execute that ToString

public int Height 
    { 
        get 
        { 
           return int.Parse(hd_Height.Value); 
        } 
        set 
        { 
            hd_Height.Value = value.ToString(); 
        } 
    } 

2) While doing this { ´this.heightToGrow; ´ you mean that you have a field with the name heightToGrow on the class... and I don't see it declared.

Declare that field:

private string heghtToGrow;
Limo Wan Kenobi