-1

I am having trouble getting my delegate to work. Basically I want it to function so that when the user clicks the button the first time the label will display the driver that came in third, the second time they hit the button the label will clear and the driver that placed in second will display in its place and then finally when the click the button for the third time it will display the winner of the race

This is aspx.cs page

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace WebProgramming3.Week_3
{
    public partial class Exercise1 : System.Web.UI.Page
    {
        static RaceResult UserClick;

       //public int Clicks = 0;

        protected void Page_Load(object sender, EventArgs e)
        {
            UserClick = new RaceResult(0);          

            UserClick.NbrClicksReached += User_Click_NbrClicksReached;             
        }

        private void User_Click_NbrClicksReached(string message)
        {
            lblRaceResults.Text += message;           
        }

        protected void btnRace_Click(object sender, EventArgs e)
        {
            UserClick.BtnClicks++;
        }
    }

    public delegate void StringDelegate(string message);

    public class RaceResult
    {
        public  int BtnClicks { get; set; }

        //Delegate
        public event StringDelegate NbrClicksReached;

        public RaceResult( int btnclicks)
        {
            BtnClicks = btnclicks;
        }

        public void Add(int amount)
        {
            if (BtnClicks == 1)
            {
                if (NbrClicksReached != null)
                    NbrClicksReached("In 3rd place is Kimi Räikkönen");
            }

            if (BtnClicks == 2)
            {
                if (NbrClicksReached != null)
                    NbrClicksReached("In 2nd place is Sebastian Vettel");
            }

            if (BtnClicks >= 3)
            {
                if (NbrClicksReached != null)
                    NbrClicksReached("In 1st place is Lewis Hamilton");
            }
        }
    }
}

This is my aspx page

<body>
    <form id="form1" runat="server">
    <div>
        <asp:Button ID="btnFeed" OnClick="btnRace_Click" runat="server" Text="Button" />
        <asp:Label ID="lblRaceResults" runat="server" Text="Label"></asp:Label>
    </div>
    </form>
</body>

If my problem is unclear please feel free to ask more questions.

Cathal O 'Donnell
  • 515
  • 3
  • 10
  • 33

1 Answers1

1

Your code has some problems. First of all you don't protect the initialization of your global variable in the Page_Load event from postbacks. Every time you run an event on the server (runat=server) then the Page_Load event is executed BEFORE the event declared in your control, so you end up reinitializing your RaceResult variable everytime.
Also, static variables are a not a good concept in a Web Application, see for example this very good question/answer: Static vs Session

So the rewriting of the initial part of your Page is

public partial class Exercise1 : System.Web.UI.Page
{
    private RaceResult UserClick;

   //public int Clicks = 0;

    protected void Page_Load(object sender, EventArgs e)
    {
        // First page load?
        if(!IsPostBack)
        {
             UserClick = new RaceResult(0);          
             UserClick.NbrClicksReached += User_Click_NbrClicksReached;
             Session["UserClick"] = UserClick;
        }
        else
        {
           // Get from first load
           UserClick = Session["UserClick"];
           if(UserClick == null)
           {
             UserClick = new RaceResult(0);          
             UserClick.NbrClicksReached += User_Click_NbrClicksReached;
             Session["UserClick"] = UserClick;
           }
        }
    }

Now the part that should call the delegate is confusing. The UserClick.Add method calls the delegate but you don't call the UserClick.Add anywhere in the Page code shown. Also the integer passed to this method is not used inside it, so it seems to be useless and could be removed. Anyway

protected void btnRace_Click(object sender, EventArgs e)
{
    // Should not be required but....
    if(UserClick != null)
    {
        UserClick.BtnClicks++;
        // The integer is meaningless in the context of your code above
        // It could be removed because the Add logic is driven 
        // by BtnClicks value
        UserClick.Add(0);
    }
}
Community
  • 1
  • 1
Steve
  • 213,761
  • 22
  • 232
  • 286