Wanted: Help making minimal example of delegation work in ASP/C#

Discussion in 'ASP .Net' started by Siegfried Heintze, Nov 25, 2006.

  1. I'm practicing for the C# brain bench test by reviewing how delegates work.
    (Delegates are easy to to with visual studio because you normally use the
    delegates that some API as already defined. I'm worried they are going to
    make me do my own delegates from scratch.) I see that line 33 of
    Default.aspx is indeed executing when I single step with the debugger. As
    you can see (on lines of 17 and 18 of propertyDemo.cs I have set up a
    delegate and I'm single stepping thu this too). But why do not the contents
    '"old=abc" new="xyz"'show up in the list box I have set up? It is clearly
    being added when I single step with the debugger.

    I have set up session state and when that did not work I set up ViewState
    and that did not help either!

    File Default.aspx:
    <%@ Page Language="C#" %>
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    <script runat="server">
    PropertyDemo pd;
    /*005*/ protected void Clear_Click(object sender, EventArgs e) {
    /*006*/ tbValue.Text = "";
    /*007*/ }
    /*008*/ protected void PropertyDemo_Click(object sender, EventArgs e){
    /*009*/ pd.name = tbValue.Text;
    /*010*/ }
    /*011*/ protected void Page_Init(object sender, EventArgs e){
    /*012*/ pd = new PropertyDemo();
    /*013*/ pd.m_eventNameChange += new
    PropertyDemo.NameChangeEvent(pd_m_eventNameChange);
    /*014*/ lbLog.Items.Add("init");
    /*015*/ }
    /*016*/ protected void Page_Load(object sender, EventArgs e){
    /*017*/ lbLog.Items.Add("load");
    /*018*/ pd = (PropertyDemo)Session["pd"];
    /*019*/ if (pd == null){
    /*020*/ pd = new PropertyDemo();
    /*021*/ pd.m_eventNameChange += new
    PropertyDemo.NameChangeEvent(pd_m_eventNameChange);
    /*022*/ }
    /*023*/ ArrayList litems = (ArrayList)Session["log"];
    /*024*/ if (litems != null)
    /*025*/ foreach (object o in litems)
    /*026*/ lbLog.Items.Add(""+o);
    /*027*/ litems = (ArrayList)ViewState["log"];
    /*028*/ if (litems != null)
    /*029*/ foreach (object o in litems)
    /*030*/ lbLog.Items.Add(""+o);
    /*031*/ }
    /*032*/ void pd_m_eventNameChange(string sOld, string sNew){
    /*033*/ lbLog.Items.Add("old=" + sOld + " new=" + sNew);
    /*034*/ SaveState();
    /*035*/ }
    /*036*/ protected void Page_Unload(object sender, EventArgs e){
    /*037*/ SaveState();
    /*038*/ }
    /*039*/ private void SaveState(){
    /*040*/ lbLog.Items.Add("page unload");
    /*041*/ Session["pd"] = pd;
    /*042*/ ArrayList log = new ArrayList();
    /*043*/ foreach (object o in lbLog.Items){
    /*044*/ log.Add(o);
    /*045*/ }
    /*046*/ Session["log"] = log;
    /*047*/ ArrayList log2 = (ArrayList)Session["log"];
    /*048*/ ViewState["log"] = log;
    /*049*/ }
    /*050*/ protected void Set_Click(object sender, EventArgs e){
    /*051*/ pd.name = tbValue.Text;
    /*052*/ }
    /*053*/ protected void getter_Click(object sender, EventArgs e){
    /*054*/ tbValue.Text = pd.name;
    /*055*/ }
    </script>
    <html xmlns="http://www.w3.org/1999/xhtml" >
    <head runat="server">
    <title>Demonstrate Getters and Setters and Delegates</title>
    </head>
    <body>
    <form id="form1" runat="server">
    <table border="1" style="width: 40px; height: 38px">
    <tbody>
    <tr>
    <td><asp:Button ID="setter" runat="server" Text="set"
    OnClick="Set_Click" /></td>
    <td>
    <asp:Button ID="getter" runat="server" Text="get"
    OnClick="getter_Click" /></td>
    <td><asp:Button ID="clearer" runat="server" Text="Clear"
    OnClick="Clear_Click" /></td>
    <td>
    <asp:TextBox ID="tbValue" runat="server"></asp:TextBox></td>
    </tr>
    <tr><td colspan="4"><asp:ListBox ID="lbLog" runat="server"
    Width="278px" Height="736px"></asp:ListBox></td></tr>
    </tbody>
    </table>
    </form>
    </body>
    </html>
    File PropertyDemo.cs
    /*001*/ using System;
    /*002*/ using System.Data;
    /*003*/ using System.Configuration;
    /*004*/ using System.Web;
    /*005*/ using System.Web.Security;
    /*006*/ using System.Web.UI;
    /*007*/ using System.Web.UI.WebControls;
    /*008*/ using System.Web.UI.WebControls.WebParts;
    /*009*/ using System.Web.UI.HtmlControls;
    /*010*/
    /*011*/ /// <summary>
    /*012*/ /// Summary description for PropertyDemo
    /*013*/ /// </summary>
    /*014*/ public class PropertyDemo
    /*015*/ {
    /*016*/ private string m_sName;
    /*017*/ public delegate void NameChangeEvent(String sOld, String sNew);
    /*018*/ public PropertyDemo()
    /*019*/ {
    /*020*/ }
    /*021*/ public string name
    /*022*/ {
    /*023*/ get {
    /*024*/ this.m_eventNameChange(m_sName,"");
    /*025*/ return m_sName;
    /*026*/ }
    /*027*/ set {
    /*028*/ this.m_eventNameChange(m_sName, value);
    /*029*/ m_sName = value;
    /*030*/ }
    /*031*/ }
    /*032*/ }
    /*033*/
     
    Siegfried Heintze, Nov 25, 2006
    #1
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Harvey
    Replies:
    0
    Views:
    790
    Harvey
    Jul 16, 2004
  2. Harvey
    Replies:
    1
    Views:
    896
    Daniel
    Jul 16, 2004
  3. Daniel Lidström
    Replies:
    0
    Views:
    308
    Daniel Lidström
    Dec 21, 2007
  4. Frank Steinmetzger
    Replies:
    0
    Views:
    293
    Frank Steinmetzger
    Jul 28, 2010
  5. Sam Roberts
    Replies:
    4
    Views:
    340
    Sam Roberts
    May 7, 2008
Loading...

Share This Page