locked
How to use many conditions in the parameter of an extenison method? RRS feed

  • Question

  • I have this extensión method for ObservableCollection:

    public static void Insert<T>(this ObservableCollection<T> paramOC, T paramNewElement, Func<T, bool> condition)
    {
        //code
    }>

    And I would like to use two conditions in the function argument, something like that:

    myOC.Insert(myNewElement, (c=> c.ID != 1 && c.Name.CompareTo(myString) > 0))

    However, I get an error that says that the delegate function does not take 1 argument. When I delete the first condition and only use one, the code works as I execpt.

    Is it possible to use many conditions in the function?

    thank so much.

    Monday, March 23, 2015 8:42 AM

Answers

  • Hello,

    I have tried to make a sample like that :

        public partial class MainWindow : Window
        {
            public ObservableCollection<Person> myOC;
            Person pers = new Person();
    
            public MainWindow()
            {
                InitializeComponent();
                myOC = new ObservableCollection<Person>();
                myOC.Insert<Person>(pers, (c => c.ID != 1 && c.Name.CompareTo("") > 0));
            }
        }
    
        public class Person
        {
            public int ID = 0;
            public string Name = "";
        }
    
        public static class MyExtend
        {
            public static void Insert<T>(this ObservableCollection<T> paramOC, T paramNewElement, Func<T, bool> condition)
            {
                //code
            }
        }

    it works on my side.

    Could you explain more your problem please ?

    Regards 

    Cédric

    • Marked as answer by ComptonAlvaro Monday, March 23, 2015 3:36 PM
    Monday, March 23, 2015 9:07 AM

All replies

  • Hello,

    I have tried to make a sample like that :

        public partial class MainWindow : Window
        {
            public ObservableCollection<Person> myOC;
            Person pers = new Person();
    
            public MainWindow()
            {
                InitializeComponent();
                myOC = new ObservableCollection<Person>();
                myOC.Insert<Person>(pers, (c => c.ID != 1 && c.Name.CompareTo("") > 0));
            }
        }
    
        public class Person
        {
            public int ID = 0;
            public string Name = "";
        }
    
        public static class MyExtend
        {
            public static void Insert<T>(this ObservableCollection<T> paramOC, T paramNewElement, Func<T, bool> condition)
            {
                //code
            }
        }

    it works on my side.

    Could you explain more your problem please ?

    Regards 

    Cédric

    • Marked as answer by ComptonAlvaro Monday, March 23, 2015 3:36 PM
    Monday, March 23, 2015 9:07 AM
  • You can compile and run the application?

    Because when I try to compile I get the error:

    "Delegate 'System.Func<Catalogo.Repositorio.Codecs,Catalogo.Repositorio.Codecs,bool>' does not take 1 arguments"

    I have the seme as you.

    My extension method is in:

    namespace MyProject.ExtensionMethods.ObservableCollection
    {
        public static class ObservableCollection
        {
            public static void Insert<T>(this ObservableCollection<T> paramOC, T paramNewElement, Func<T, bool> condition)
            {
                //Code
            }
        }
    }



    Monday, March 23, 2015 9:44 AM
  • I compile and run.

    My aplication is  an "Windows Application" with ".Net Framework 4.5" in "VS studio 2012".

    Monday, March 23, 2015 10:25 AM
  • I am using VS2013 community with .NET 4.5.1. Really I don't see any difference. I will see more in deph.

    Thank you so much.

    Monday, March 23, 2015 10:41 AM
  • It works fine for me and the code is valid. The only thing that seems off is the parens around the function.  Remove those and you have a function that accepts c as a parameter and returns a Boolean condition.

    class Program
    {
        static void Main ( string[] args )
        {
            var items = new Data[] { };
    
            var newItem = new Data() { ID = 10, Name = "Bob" };
            items.Insert(newItem, (c => c.ID != 1 && c.Name.CompareTo("Miller") > 0));
        }
    }
    
    class Data
    {
        public int ID { get; set; }
        public string Name { get; set; }
    }
    
    static class Extensions
    {
        public static void Insert<T>( this IEnumerable<T> source, T newValue, Func<T, bool> condition )
        {
        }
    }

    Michael Taylor
    http://blogs.msmvps.com/p3net

    Monday, March 23, 2015 2:02 PM
  • You are defining a generic Insert<T> method but you are calling the built-in, non-generic, Insert method.

    You must specify the type (YourType is the type of myNewElement) in the same code below:


                myOC.Insert<YourType>(myNewElement, (c => c.ID != 1 && c.Name.CompareTo(myString) > 0));


    Please remember to close your threads by marking all helpful posts as answer then start a new thread if you have a new question.

    Monday, March 23, 2015 2:11 PM
  • That's work, thanks.
    Monday, March 23, 2015 3:32 PM