• Home
  • About
  • BestBloggingIdeas
  • DotNetLearningSource
  • FORUM
  • Joblinks
  • Latest News
  • Policy
  • POSTS
  • SimplySqlServer.Com && SimplyAspDotNet.Com
  • Sitemap

Join Ours Forum

Asp.Net,C#,Ajax,Sql server,silverlight,Javascript codes exambles articles,Programming exambles

RSS Feed
  • Bounty Huge Roll [Amazon Frustration-Free Packaging]
  • XML Introduction to XML VHS Video Training, 1 hr., 32 minutes.
  • The Basic Overview of Windows Mobile Development Asp.Net C#
  • Overview of Sql server extended properties Asp.Net C#
  • How to Use Sql Server Extended properties using visual studio Asp.Net C#
  • Adobe Dreamweaver Templates Accelerate Web Development
  • Top Tips for Web Design Projects
  • How to Achieve a Good Web Design Structure
  • To Use Or Not To Use Website Templates
  • Five Tips to a Successful Website
  • Top 10 Articles,


    Silverlight Datagrid Select Update Delete Insert Asp.Net C#

    Differences Similarities Benefits Between Typed Datasets and Untyped Datasets asp.net c#

    Linq to Sql Introduction Entities Ado.Net C# SqlClasses Attributes Linq Mapping

    Linq Programming/How Linq Works?/Linq Implementation In Asp.Net C# Ado.Net

    Performing Developing Using Investigating Asp.Net 2.0 Ajax Application Development Asp.Net C#

    Hosting/Install Wcf Services in a Windows Service Asp.Net C#

    Connecting Silverlight to Wcf Asp.Net C#

    Silverlight Data Grid Data Binding WCF Asp.Net C#

    Invoking/Accessing/Calling WCF Service Without Adding/Creating Proxy/Reference Asp.Net C#

    Performing Doing Creating Insert Update Delete sql data Using Linq Database Asp.Net C#

    Applying Custom logic using Delegates and Generics Asp.Net C#

    Posted by james on April 27, 2011 Leave a comment (0) Go to comments

    In making the most of your code, you have two options: you can use generic methods or reusable delegates.

    Using Generic Methods

    Of course, one of the benefits of generic methods is that they can be reused with collections of different types. A very simple example is a function that sorts a collection and writes it to the console window:

    static void WriteSortedValues(List list)

    {

    list.Sort();

    list.ForEach(

    delegate(T item) { Console.WriteLine(item); }

    )

    }

    Note: ForEach Uses Action Delegates

    In the above example, we’re using the ForEach method with an action delegate. An action delegate is another type of anonymous delegate, only it doesn’t return anything. The idea behind an action delegate is that it defines an action to be performed for each item in the collection. This compares with predicate delegates, which contain the decision logic. This action delegate uses the same logic that we saw with Find and FindAll in “How do I filter filtering generic collections items in a generic collection generic collections filtering ?”.

    The WriteSortedValues function is generic, as indicated by the parameter that follows the function name. When we call WriteSortedValues and pass it a List, .NET knows to replace those Ts with ints. And as WriteSortedValues is generic, we can use it with lists of any type that .NET knows how to sort. The following example shows the function in action, handling three different types of objects—a string, an int, and a DateTime:

    Predicates.aspx.cs (excerpt)

    private void SortingDemonstration()

    {

    string[] names = { “Bob”, “Sue”, “Jim”, “Edgar” };

    int[] values = { 456, 234, 567, 123, 890 };

    DateTime[] dates = {

    new DateTime(1950,2,3),

    new DateTime(1970,4,5),

    new DateTime(2000,1,1)

    };

    WriteSortedValues(new List(names));

    WriteSortedValues(new List(values));

    WriteSortedValues(new List(dates));

    }

    We can’t necessarily use this solution with a collection of custom objects, such as List, since the List object doesn’t know how to sort them. An attempt to call WriteSortedValues(List) would compile, but it would throw the runtime error: Failed to compare two elements in the array. It’s not tough to fix that problem, though—we can either implement the IComparable interface in our Employee class, or we can call the overloaded Sort method and pass it a Comparison or IComparer delegate.[3]

    Using Reusable Delegates

    Most of our samples so far have implemented predicates and actions as anonymous delegates. We looked at the reasons for this (simpler code, local variable capturing) in “How do I filter filtering generic collections items in a generic collection generic collections filtering ?”. However, you should keep an eye out for delegates that can be reused, and promote them to methods.

    For example, let’s assume that an application we’ve written for our employer consists of multiple classes, including Customers, Employees, Stores, and an Address structure.[4] This Address contains a Region property.

    Our company is headquartered in California, so for various reasons (sales tax, employee taxes, benefits, and so on), we may want to filter our different lists so that our results include only items whose Region is California.

    As such, our delegate method for retrieving Californian employees might look like this:

    return employeeList.Find(

    p.Address.Region == “California”

    );

    We can move this into a reusable delegate method as follows:

    Predicates.aspx.cs (excerpt)

    public static bool IsCalifornian(Person p)

    {

    return (p.Address.Region == “California”);

    }

    Now we can use that method with any list that contains the Address structure—for example, a list of Employees or Customers:

    Predicates.aspx.cs (excerpt)

    public List GetCaliforniaEmployees()

    {

    List employees = GetEmployees();

    return employees.FindAll(Person.IsCalifornian);

    }

    public List GetNonCaliforniaCustomers()

    {

    List customers = GetCustomers();

    customers.RemoveAll(Person.IsCalifornian);

    return customers;

    }

    In the above code listing, we’re using the same predicate in two different ways. In the first example, GetCaliforniaEmployees, we’re using it with FindAll to return all employees who have a Californian address. In the second example, GetNonCaliforniaCustomers, we’re using the predicate with RemoveAll to remove all Customers with Californian addresses from the customer list.

    [3]For more information on implementing the IComparable interface, see David Hayden’s excellent blog post.

    [4]A structure, represented by the keyword struct in C#, is a composite data type. A structure can contain fields, methods, constants, constructors, properties, indexers, operators and other structure types.

    ASP.NETApplying, AspNet, Custom, Delegates, Generics, Logic, using
    ← Effectively/Reason For Using Generics in Asp.Net Project C#
    Creating/Displaying/performing Formatted Strings in Asp.Net Project C# →

    Learn Easily Using Video Tutorials


    How to choose the right Java IDE – explained Eclipse NetBeans BlueJ

    Developing/Creating/Performing/Configuring Java Applications Using Eclipse IDE

    Step By Step Guide for Download/Install Configure Eclipse IDE for Java

    Editing data with the GridView control Asp.Net C#

    Registering/Configuring Web Controls globally in web.config file asp.net c#

    Registering/Configuring Web Controls globally in web.config file asp.net c#

    Best way to prepare asp.net Interview - Success Stories

    Download Important Questions and PPT's:

    Sql Server Important Questions Online free download

    Dotnet Important Questions Online free download

    Exploring Linq to Sql Process Flow

    Learn how to perform silverlight programming

    Learn OOPs concepts in better and well manner

    Learn Ajax in better and well manner

    Leave a Reply Cancel reply

    Your email address will not be published. Required fields are marked *

    *

    *


    You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>

    Enter your email address:

    Delivered by FeedBurner

    • Recent Posts

      • Bounty Huge Roll [Amazon Frustration-Free Packaging]
      • XML Introduction to XML VHS Video Training, 1 hr., 32 minutes.
      • The Basic Overview of Windows Mobile Development Asp.Net C#
      • Overview of Sql server extended properties Asp.Net C#
      • How to Use Sql Server Extended properties using visual studio Asp.Net C#
    • Search by Tags!

      Application AspNet Basic between Black Bluetooth Build Business Collection Consultants Design Development Downloading effective Excel Experts Generics Implement Installing Interview Logic Management Microsoft Minutes Object Outlook Professional Programmer Programming Project Projects Questions Ready Select Server Services Silverlight Source Strings Studio Through using Visual Website Wordpress
    • Archives

      • August 2011
      • June 2011
      • May 2011
      • April 2011
      • March 2011
      • February 2011
      • December 2010
      • November 2010
      • October 2010
      • September 2010
      • August 2010
      • July 2010
      • June 2010
      • May 2010
      • April 2010
      • March 2010
      • February 2010
      • January 2010
      • December 2009
      • November 2009
      • October 2009
      • September 2009

    Copyright © 2012 NetProgrammingHelp.com

    Δ Top