WorldofASP.NET : ASP.NET Directory, Tutorial, Hosting, and Source Code
You are 1 of 165 users


Part 1 of 2 : Delegates in .NET and MS Ajax

Blogger : ASP.NET Blogs
All posts : All posts by ASP.NET Blogs
Category : AJAX
Blogged date : 2006 Nov 16

In Part 1 of 2, I want to show the example that I created today for my talk at VSLive in Dallas.

First, let's explore delegates in C# on the server in .net. In part 2, we'll look at how to accomplish the same concept on the client using MS Ajax Extensions. Beta2.

We have the following ASP markup.

   1:  <asp:DropDownList ID="OperationList" runat="server" AutoPostBack="True" OnSelectedIndexChanged="OperationList_SelectedIndexChanged">
   2:      <asp:ListItem Selected="True" Text="Do...." Value="0" />
   3:      <asp:ListItem Text="Add" Value="1" />
   4:      <asp:ListItem Text="Subtract" Value="2" />
   5:      <asp:ListItem Text="Multiply" Value="3" />
   6:  </asp:DropDownList>
   7:  <asp:PlaceHolder ID="MatrixPlaceHolder" runat="server" />

Lines 1 through 6 contains a simple drop down list. These items are listed 0 through 3, which will become an Enum (both on the server, and in JavaScript (part2)). Line 7 is a place holder, that we're going to build a matrix of numbers. Our goal is to write code that will create a 11X11 matrix where each cell in the matrix has a function (delegate) applied to it. Following are the sample outputs.

Server Side Add Function
Server Side Add Function

Server Side Disp;lay X,Y funciton
Server Side Just display X,Y function

 
Server Side Multiply function


Server Side Subtract function

So now we know the output, and our goal is to write code that will perform these outputs. Now, there is now doubt that anyone, in any language, could write two nested loops, and output the above tables. And they could just as easily perform the math. The trick here is to use a delegate in just the right place, so that we only have to write one set of nested loops. When we're creating the cells in our matrix, we will delegate the math operation, out to another function that will actually do the work for us. So when we're creating the matrix, we don't know (and don't really care) what math functions can be done with X and Y. We'll let the delegate coder worry about that.

So ... To the code.

in the C# code behind(side) file, we first want to create our delegate. This is the contract that we want to create, for others to write math methods against. The delegate is nothing more than a signature. That's it. In fact, the delegate itself is the easiest of the code that we're about the write.

   1:  public delegate string DoMath(int a, int b);

So we now have a DoMath delegate. It's functions are to return a string, and take to int's as parameters. That's it. No more no less. any function that returns a string, that takes two int's as parameters, now matches out delegate signature, and can be used in place of the delegate. Let's create a few delegated methods. That is.... let's create a couple of methods that returns a string from two ints.

   1:  private string NoMath(int a, int b)
   2:  {
   3:      return string.Format("{0},{1}", a, b);
   4:  }
   5:  private string SubtractMath(int a, int b)
   6:  {
   7:      return ( a - b ).ToString();
   8:  }
   9:  private string MultiplyMath(int a, int b)
  10:  {
  11:      return (a * b).ToString();
  12:  }    
  13:  private string AddMath(int a, int b)
  14:  {
  15:      return ( a + b ).ToString();
  16:  }

So there we have 4 methods. NoMath, AddMath, SubtractMath, and MultiplyMath. All of these methods have the same signature. They all return a string from two ints. 

Next, we create an Enumerator to handle the values of our possible options in our ASP.NET drop down list.

   1:  public enum MathOperation : int
   2:  {
   3:      None = 0,
   4:      Add = 1,
   5:      Subtract = 2,
   6:      Multiply = 3
   7:  }

This enum will let us mnemonically see the value. It isn't necessary, but makes for much nicer code to read.

Next we need a worker method, that will actually build the matrix for us. Notice that the method takes a delegate as it's only parameter.

   1:  private void BuildMatrix( DoMath operate)
   2:  {
   3:      Table table = new Table();
   4:      
   5:      for (int x = 0; x <= 10; x++)
   6:      {
   7:          TableRow row = new TableRow();
   8:          for (int y = 0; y <= 10; y++)
   9:          {
  10:              TableCell cell = new TableCell();
  11:              cell.BorderWidth = new Unit(1);
  12:              cell.BorderColor = Color.Black;
  13:              string val = operate( x, y );
  14:              LiteralControl lit =new LiteralControl(val);
  15:              cell.Controls.Add(lit);
  16:              row.Cells.Add( cell );
  17:          }
  18:          table.Rows.Add( row );
  19:      }
  20:      MatrixPlaceHolder.Controls.Clear();
  21:      MatrixPlaceHolder.Controls.Add(table);
  22:  }

look closely at line 13. We call operate(x,y) and set its return value to a string variable we call "val". Then this "val" is set to a literal text control, and added to the cell, which is in turn added to the row, which is in turn added to a table, which is finally added to the place holder control.

The only thing left for us to do is handle the SelectedIndexChanged event of the drop down. Here we run a switch statement on the incoming drop down list value, and call the BuildMatrix method. Notice the parameter that we pass in, is JUST THE NAME of the method that we want to call. That is the delegate. That method will be called on line 13 (above) and X and Y will be passed in as parameters. The return value (remember they all return a string?) gets set to the cell text, and the table is rendered.

   1:  protected void OperationList_SelectedIndexChanged(object sender, EventArgs e)
   2:  {
   3:      int val = Convert.ToInt32( ((DropDownList)sender).SelectedValue );
   4:      MathOperation math = (MathOperation) val;
   5:      switch (math)
   6:      {
   7:          case MathOperation.Add:
   8:              {
   9:                  BuildMatrix( AddMath );
  10:                  break;
  11:              }
  12:          case MathOperation.Multiply:
  13:              {
  14:                  BuildMatrix( MultiplyMath );
  15:                  break;
  16:              }
  17:          case MathOperation.Subtract:
  18:              {
  19:                  BuildMatrix( SubtractMath );
  20:                  break;
  21:              }
  22:          case MathOperation.None:
  23:              {
  24:                  BuildMatrix(NoMath);
  25:                  break;
  26:              }
  27:      }
  28:  }

In part two of this (to be posted as the next post) We're going to use Microsoft ASP.NET Ajax Extensions to accomplish the same goal, only we won't use **ANY** c#, it'll be 100% JavaScript. That's an exciting article.


Read comments or post a reply to : Part 1 of 2 : Delegates in .NET and MS Ajax
Page 1520 of 1924
Next | Last

.NET News Categories








Legend : - Within 3 Days - Within 6 Days - Within 9 Days

Home | Add Resources | Sponsored Listings | Advertise with Us | SiteMap 1 | SiteMap 2 | Link To Us | Contact Us
© 2002-2008 Worldofasp.net ASP.NET Directory, Hosting and Tutorials | All rights reserved
Our Partners : ASP.NET Web Hosting | Windows Web Hosting | ASP.NET Hosting | Phone Card | PHP Directory | Bangkok Hotels |Calling Card