How to use predicate

Discussion in 'ASP .Net' started by jyothi, Dec 15, 2008.

  1. jyothi

    jyothi Guest

    Hi All,

    when i buid this code i get an error saying the syntax is incorrect to use
    the delegate .w.r.t anonymous method. Upon more digging i found that i need
    to define the delegate method.
    The method i need to pass to the delegate needs two parameters i.e the
    customer object and the id to check. I'm stuck how to pass 2 parameters to
    the method.

    Can somebody explain me how to do it?

    //C#
    using System;
    using System.Data;
    using System.Configuration;
    using System.Web;
    using System.Web.Security;
    using System.Web.UI;
    using System.Web.UI.WebControls;
    using System.Web.UI.WebControls.WebParts;
    using System.Web.UI.HtmlControls;
    using System.Collections.Generic;

    public class CustomerList
    {
    private static List<Customer> custList = new List<Customer>();

    public List<Customer> Select()
    {
    return custList;
    }

    public Customer SelectSingle(int selectCustomerId)
    {
    if (selectCustomerId == -1) return null;
    return custList.Find(
    delegate(Customer customer)
    { return customer.Id == selectCustomerId; });
    }

    public void Update(Customer updateCustomer )
    {
    Customer customerFound = custList.Find(
    delegate(Customer customer)
    { return customer.Id == updateCustomer.Id; });
    customerFound.Id = updateCustomer.Id;
    customerFound.Name = updateCustomer.Name;
    customerFound.City = updateCustomer.City;
    customerFound.State = updateCustomer.State;
    customerFound.Phone = updateCustomer.Phone;
    }

    public void Insert(Customer _customer)
    {
    custList.Add(_customer);
    }

    public void Delete(Customer deleteCustomer)
    {
    Customer customerFound = custList.Find(
    delegate(Customer customer)
    { return customer.Id == deleteCustomer.Id; } );
    custList.Remove(customerFound);
    }
    }
     
    jyothi, Dec 15, 2008
    #1
    1. Advertising

  2. jyothi

    bruce barker Guest

    its unclear what your question is. you don't say which line is the
    error, nor do any of the delegates have two args. also if you are using
    3.5, lambda expressions are simpler:

    public Customer SelectSingle(int selectCustomerId)
    {
    if (selectCustomerId == -1) return null;
    return custList.Find(c => c.id == selectCustomerId);
    }

    -- bruce (sqlwork.com)

    jyothi wrote:
    > Hi All,
    >
    > when i buid this code i get an error saying the syntax is incorrect to use
    > the delegate .w.r.t anonymous method. Upon more digging i found that i need
    > to define the delegate method.
    > The method i need to pass to the delegate needs two parameters i.e the
    > customer object and the id to check. I'm stuck how to pass 2 parameters to
    > the method.
    >
    > Can somebody explain me how to do it?
    >
    > //C#
    > using System;
    > using System.Data;
    > using System.Configuration;
    > using System.Web;
    > using System.Web.Security;
    > using System.Web.UI;
    > using System.Web.UI.WebControls;
    > using System.Web.UI.WebControls.WebParts;
    > using System.Web.UI.HtmlControls;
    > using System.Collections.Generic;
    >
    > public class CustomerList
    > {
    > private static List<Customer> custList = new List<Customer>();
    >
    > public List<Customer> Select()
    > {
    > return custList;
    > }
    >
    > public Customer SelectSingle(int selectCustomerId)
    > {
    > if (selectCustomerId == -1) return null;
    > return custList.Find(
    > delegate(Customer customer)
    > { return customer.Id == selectCustomerId; });
    > }
    >
    > public void Update(Customer updateCustomer )
    > {
    > Customer customerFound = custList.Find(
    > delegate(Customer customer)
    > { return customer.Id == updateCustomer.Id; });
    > customerFound.Id = updateCustomer.Id;
    > customerFound.Name = updateCustomer.Name;
    > customerFound.City = updateCustomer.City;
    > customerFound.State = updateCustomer.State;
    > customerFound.Phone = updateCustomer.Phone;
    > }
    >
    > public void Insert(Customer _customer)
    > {
    > custList.Add(_customer);
    > }
    >
    > public void Delete(Customer deleteCustomer)
    > {
    > Customer customerFound = custList.Find(
    > delegate(Customer customer)
    > { return customer.Id == deleteCustomer.Id; } );
    > custList.Remove(customerFound);
    > }
    > }
    >
    >
     
    bruce barker, Dec 15, 2008
    #2
    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. Jacques

    Predicate by attribute?

    Jacques, Jun 24, 2004, in forum: XML
    Replies:
    2
    Views:
    378
    Jacques
    Jun 24, 2004
  2. awebguynow
    Replies:
    1
    Views:
    359
    George Bina
    Jan 27, 2006
  3. Sabba
    Replies:
    0
    Views:
    631
    Sabba
    May 9, 2006
  4. mikets
    Replies:
    18
    Views:
    1,156
    Chris \( Val \)
    Jan 7, 2004
  5. William Payne

    std::stable_sort with predicate woes

    William Payne, Apr 21, 2004, in forum: C++
    Replies:
    8
    Views:
    1,620
    tom_usenet
    Apr 21, 2004
Loading...

Share This Page