locked
parameter overloading RRS feed

  • Question

  • I am having a fallowing code.

    public void Function(MyClass obj)

    {

       obj.Get();

    }

    I am having 3 different clas MyClass , YourClass, HisClass of same type. I want to use this function for all class. When the input parameter is changed with any of these 3 classes it should take that class object only and execute. Please help me for this problem.


    Thursday, July 4, 2013 6:57 AM

Answers

  • As sebastian told you in previous thread, you can achieve the same scenario, here is the example.

     public interface MyBaseClass
        {
            public void Get();
        }
    
        public class MyClass3 : MyBaseClass
        {
            public void Get()
            {
                
            }
        }
    
        public class MyClass1 : MyBaseClass
        {
            public void Get()
            {
    
            }
        }
    
        public class MyClass2 : MyBaseClass
        {
            public void Get()
            {
    
            }
        }
    
    
     void MainWindow_Loaded(object sender, RoutedEventArgs e)
            { MyFunction(new MyClass1());
                MyFunction(new MyClass2());
                MyFunction(new MyClass3());}
    
    
     public void MyFunction(MyBaseClass obj)
            {
                obj.Get();
    
            }


    Thanks & Regards
    Syed Amjad Sr. Silverlight/WPF Developer,
    yahoo : syedamjad6736@yahoo.com, skype : syedamjad.0786.
    Please use Marked as Answer if my post solved your problem and use Vote As Helpful if a post was useful.

    • Marked as answer by Caillen Wednesday, July 31, 2013 6:04 AM
    Thursday, July 4, 2013 7:38 AM
  • Overloading means the process of having several fucntions with the same name in the same class, but each with different signatures (return value and/or parameters):

    http://msdn.microsoft.com/en-us/library/vstudio/ms229029.aspx

    For example, it is customary for all classes derived from Stream to have a whole bunch of overloaded constructors (one of wich takes a stream, to encapsualte another type of stream if nessesary):

    http://msdn.microsoft.com/en-us/library/system.io.memorystream.aspx

    In most cases of working with classes, Overloading is not the right sapproach.
    What you want to look into instead is Polymorphy and Inheritance - the ability of a class to be fit into a Varriable of a parrent class and use fucntions of it's parent class, or overwrite them. Here is an example for Inheritance:

    http://www.codeproject.com/Articles/1445/Introduction-to-inheritance-polymorphism-in-C


    Let's talk about MVVM: http://social.msdn.microsoft.com/Forums/en-US/wpf/thread/b1a8bf14-4acd-4d77-9df8-bdb95b02dbe2

    • Marked as answer by Caillen Wednesday, July 31, 2013 6:04 AM
    Thursday, July 4, 2013 12:06 PM
  • You could do something along these lines:

    public bool Delete<T>(Func<DbContext, ObjectSet<T>> getSet, T item)
    {
        dbContext.Connection.Open();
        using (var trans = dbContext.Connection.BeginTransaction())
        {
            try
            {
                getSet(dbContext).DeleteObject(item);
                dbContext.SaveChanges();
                return true;
            }
            catch (Exception ex)
            {
                var res = ex.Message;
    
                trans.RollBack();
    
                return false;
            }
            finally
            {
                dbContext.Connection.Close();
            }
        }
    }

    And then call that method like this to remove a MyClass.

    Delete(db => db.MyClasses, myClass);

    Regards


    Vicente Cartas Espinel - MVP XNA/DirectX

    Blog about C# and XNA Development

    Blog about Role Playing Games

    • Edited by Vicente Cartas Thursday, July 18, 2013 4:46 PM mistake
    • Marked as answer by Caillen Wednesday, July 31, 2013 6:01 AM
    Thursday, July 18, 2013 4:46 PM

All replies

  • Hi,

    You should derive MyClass, YourClass, HisClass from a common base class or interface. Then use a polimorfic method (i.e. Get()) to do the work. But, I don't fully understand what you try to achieve. Can you explain a bit more?

    Sebastiano

    Thursday, July 4, 2013 7:01 AM
  • As sebastian told you in previous thread, you can achieve the same scenario, here is the example.

     public interface MyBaseClass
        {
            public void Get();
        }
    
        public class MyClass3 : MyBaseClass
        {
            public void Get()
            {
                
            }
        }
    
        public class MyClass1 : MyBaseClass
        {
            public void Get()
            {
    
            }
        }
    
        public class MyClass2 : MyBaseClass
        {
            public void Get()
            {
    
            }
        }
    
    
     void MainWindow_Loaded(object sender, RoutedEventArgs e)
            { MyFunction(new MyClass1());
                MyFunction(new MyClass2());
                MyFunction(new MyClass3());}
    
    
     public void MyFunction(MyBaseClass obj)
            {
                obj.Get();
    
            }


    Thanks & Regards
    Syed Amjad Sr. Silverlight/WPF Developer,
    yahoo : syedamjad6736@yahoo.com, skype : syedamjad.0786.
    Please use Marked as Answer if my post solved your problem and use Vote As Helpful if a post was useful.

    • Marked as answer by Caillen Wednesday, July 31, 2013 6:04 AM
    Thursday, July 4, 2013 7:38 AM
  • abstract class PersonalClass

    MyClass , YourClass, HisClass all extends PersonalClass

    void MyFunction(string para)

    {

     switch(para)

    {

    case "YourClass": PersonalClass obj=new MyClass ();break;

    case "HisClass ": PersonalClass obj=new HisClass ();break;

    case "YourClass": PersonalClass obj=new YourClass();break;

    }

    //your code opreate obj.........

    }

    Thursday, July 4, 2013 7:55 AM
  • using System;
    
    class Program
    {
        public static void Main()
        {
    	ShowParameterOverload();
    	ShowParameterOverload("Category");
        }
    
        static void ShowParameterOverload()
        {
    	// Send default parameter to overload.
    	ShowParameterOverload("MyParameter");
        }
    
        static void ShowParameterOverload(string value)
        {
    	// We don't need an if check here, which makes
    	// ... calling this method directly faster.
    	Console.WriteLine(value);
        }
    }

    See here is an overload with no parameter, and one with a string parameter. Overloaded methods always have different parameters. Look at what happens when ShowParameterOverload is called the second time.


    Please mark the post answered your question as the answer, and mark other helpful posts as helpful, so they will appear differently to other users who are visiting your thread for the same problem.

    Thursday, July 4, 2013 9:01 AM
  • Overloading means the process of having several fucntions with the same name in the same class, but each with different signatures (return value and/or parameters):

    http://msdn.microsoft.com/en-us/library/vstudio/ms229029.aspx

    For example, it is customary for all classes derived from Stream to have a whole bunch of overloaded constructors (one of wich takes a stream, to encapsualte another type of stream if nessesary):

    http://msdn.microsoft.com/en-us/library/system.io.memorystream.aspx

    In most cases of working with classes, Overloading is not the right sapproach.
    What you want to look into instead is Polymorphy and Inheritance - the ability of a class to be fit into a Varriable of a parrent class and use fucntions of it's parent class, or overwrite them. Here is an example for Inheritance:

    http://www.codeproject.com/Articles/1445/Introduction-to-inheritance-polymorphism-in-C


    Let's talk about MVVM: http://social.msdn.microsoft.com/Forums/en-US/wpf/thread/b1a8bf14-4acd-4d77-9df8-bdb95b02dbe2

    • Marked as answer by Caillen Wednesday, July 31, 2013 6:04 AM
    Thursday, July 4, 2013 12:06 PM
  • This is my function. Here dbCotext is my entity framework object. I have sql table called MyClass. So this function only can able to delete the object of type MyClass. But I want to use this function to delete different Class object also(for example YourClass and HisClass object also). So how to do it please help me.

    public bool Delete(MyClass car)
            {
                dbContext.Connection.Open();
                using (var trans = dbContext.Connection.BeginTransaction())
                {
                    try
                    {
                        dbContext.MyClasses.DeleteObject(car);
                        dbContext.SaveChanges();
                        return true;
                    }
                    catch (Exception ex)
                    {
                          var res = ex.Message;

                          trans.RollBack();

                           return false;
                    }
                    finally
                    {
                        dbContext.Connection.Close();
                    }

                }
            }

                    
    Thursday, July 4, 2013 2:37 PM
  • HI,

    did you resolve the issue?


    Se questo post risponde alla tua domanda ricorda di contrassegnarlo come risposta. In questo modo aiuterai altri utenti che hanno lo stesso problema a trovare la risposta più velocemente. Grazie.

    Thursday, July 18, 2013 3:30 PM
  • You could do something along these lines:

    public bool Delete<T>(Func<DbContext, ObjectSet<T>> getSet, T item)
    {
        dbContext.Connection.Open();
        using (var trans = dbContext.Connection.BeginTransaction())
        {
            try
            {
                getSet(dbContext).DeleteObject(item);
                dbContext.SaveChanges();
                return true;
            }
            catch (Exception ex)
            {
                var res = ex.Message;
    
                trans.RollBack();
    
                return false;
            }
            finally
            {
                dbContext.Connection.Close();
            }
        }
    }

    And then call that method like this to remove a MyClass.

    Delete(db => db.MyClasses, myClass);

    Regards


    Vicente Cartas Espinel - MVP XNA/DirectX

    Blog about C# and XNA Development

    Blog about Role Playing Games

    • Edited by Vicente Cartas Thursday, July 18, 2013 4:46 PM mistake
    • Marked as answer by Caillen Wednesday, July 31, 2013 6:01 AM
    Thursday, July 18, 2013 4:46 PM