views:

98

answers:

1

I'm driving myself nuts with this particular problem, i really hope someone can help! In the below example i cant get the value of "4" to appear in the rendered page. The output from below compiles and executes just fine but all three of the values shown are "0".

Here's a short snippet:
(hopefully I'm formatting this correctly)

(from default.aspx)

<%@ Register Src="Modules/StarRating.ascx" TagName="StarRating" TagPrefix="mytag" %>

(from StarRating.ascx)

<%@ Control Language="C#" AutoEventWireup="true" CodeBehind="StarRating.ascx.cs" Inherits="StarRating" %>  
<h1>RATING: <%=OverallRating%></h1>

(from StarRating.ascx.cs)

public partial class StarRating : System.Web.UI.UserControl  
{  
    private int _OverallRating;  
    public string OverallRating  
    {  
        get { return _OverallRating.ToString(); }  
        set { _OverallRating = int.Parse(this.OverallRating); }  
    }

    protected void Page_Load(object sender, EventArgs e)  
    {  
        Response.Write("RATING (from behind code!): " + OverallRating);  
        Response.Write("<BR />RATING (another one): " + _OverallRating);  
    }  
}  
A: 

In your OverallRating set accessor, you're parsing the current value of the underlying integer variable instead of the value being passed to the property. This should fix it:

public string OverallRating  
{  
    get { return _OverallRating.ToString(); }  
    set { _OverallRating = int.Parse(value); }
}

(Presuming that wasn't just a typo)

Jeff Sternal
Wow, you're absolutely right. I was using the variable too soon! Thanks very much :)
Muse