Two classes which are very similar, I want to pass one or the other into a method











up vote
-1
down vote

favorite












I have two classes, consider the following:



public class Class1
{
public int CustomerID;
public int DriverID;
public int EmployeeID;
}

public class Class2
{
public int CustomerID;
public int DriverID;
public int EmployeeID;
}


I want to then write a method which does something with either one of these methods, but my program won't know which one until runtime.



public void DoSomething(???)
{
if (???.GetType() == typeof(Class1)
{
//do stuff related to class 1
}

if (???.GetType() == typeof(Class2)
{
//do stuff related to class 2
}
}


How am I able to pass one of those classes into that method, considering they are different types? I then need to check the type to perform separate actions.



The way this will be used in my application is that Class1 and Class2 will be linked to a ParentClass, and on that ParentClass it will dictate which linked object should be used (either Class1 and Class2) e.g. ParentClass will have a bool on it, and if bool is true, then use Class1 else use Class2, and depending on that boolean is what I want to pass into that method. I don't want to create a separate method as I think it will be best to do this.



I'm not sure how to achieve this.










share|improve this question






















  • You could define an interface containing the common method and properties and change the two classes so that both implement it. Then define your method to take a parameter of this interface type
    – Klaus Gütter
    Nov 9 at 15:33






  • 2




    You've tagged this post with "interface", but you're not using one. An interface would solve this problem.
    – gunr2171
    Nov 9 at 15:33















up vote
-1
down vote

favorite












I have two classes, consider the following:



public class Class1
{
public int CustomerID;
public int DriverID;
public int EmployeeID;
}

public class Class2
{
public int CustomerID;
public int DriverID;
public int EmployeeID;
}


I want to then write a method which does something with either one of these methods, but my program won't know which one until runtime.



public void DoSomething(???)
{
if (???.GetType() == typeof(Class1)
{
//do stuff related to class 1
}

if (???.GetType() == typeof(Class2)
{
//do stuff related to class 2
}
}


How am I able to pass one of those classes into that method, considering they are different types? I then need to check the type to perform separate actions.



The way this will be used in my application is that Class1 and Class2 will be linked to a ParentClass, and on that ParentClass it will dictate which linked object should be used (either Class1 and Class2) e.g. ParentClass will have a bool on it, and if bool is true, then use Class1 else use Class2, and depending on that boolean is what I want to pass into that method. I don't want to create a separate method as I think it will be best to do this.



I'm not sure how to achieve this.










share|improve this question






















  • You could define an interface containing the common method and properties and change the two classes so that both implement it. Then define your method to take a parameter of this interface type
    – Klaus Gütter
    Nov 9 at 15:33






  • 2




    You've tagged this post with "interface", but you're not using one. An interface would solve this problem.
    – gunr2171
    Nov 9 at 15:33













up vote
-1
down vote

favorite









up vote
-1
down vote

favorite











I have two classes, consider the following:



public class Class1
{
public int CustomerID;
public int DriverID;
public int EmployeeID;
}

public class Class2
{
public int CustomerID;
public int DriverID;
public int EmployeeID;
}


I want to then write a method which does something with either one of these methods, but my program won't know which one until runtime.



public void DoSomething(???)
{
if (???.GetType() == typeof(Class1)
{
//do stuff related to class 1
}

if (???.GetType() == typeof(Class2)
{
//do stuff related to class 2
}
}


How am I able to pass one of those classes into that method, considering they are different types? I then need to check the type to perform separate actions.



The way this will be used in my application is that Class1 and Class2 will be linked to a ParentClass, and on that ParentClass it will dictate which linked object should be used (either Class1 and Class2) e.g. ParentClass will have a bool on it, and if bool is true, then use Class1 else use Class2, and depending on that boolean is what I want to pass into that method. I don't want to create a separate method as I think it will be best to do this.



I'm not sure how to achieve this.










share|improve this question













I have two classes, consider the following:



public class Class1
{
public int CustomerID;
public int DriverID;
public int EmployeeID;
}

public class Class2
{
public int CustomerID;
public int DriverID;
public int EmployeeID;
}


I want to then write a method which does something with either one of these methods, but my program won't know which one until runtime.



public void DoSomething(???)
{
if (???.GetType() == typeof(Class1)
{
//do stuff related to class 1
}

if (???.GetType() == typeof(Class2)
{
//do stuff related to class 2
}
}


How am I able to pass one of those classes into that method, considering they are different types? I then need to check the type to perform separate actions.



The way this will be used in my application is that Class1 and Class2 will be linked to a ParentClass, and on that ParentClass it will dictate which linked object should be used (either Class1 and Class2) e.g. ParentClass will have a bool on it, and if bool is true, then use Class1 else use Class2, and depending on that boolean is what I want to pass into that method. I don't want to create a separate method as I think it will be best to do this.



I'm not sure how to achieve this.







c# interface






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Nov 9 at 15:29









user3046756

104




104












  • You could define an interface containing the common method and properties and change the two classes so that both implement it. Then define your method to take a parameter of this interface type
    – Klaus Gütter
    Nov 9 at 15:33






  • 2




    You've tagged this post with "interface", but you're not using one. An interface would solve this problem.
    – gunr2171
    Nov 9 at 15:33


















  • You could define an interface containing the common method and properties and change the two classes so that both implement it. Then define your method to take a parameter of this interface type
    – Klaus Gütter
    Nov 9 at 15:33






  • 2




    You've tagged this post with "interface", but you're not using one. An interface would solve this problem.
    – gunr2171
    Nov 9 at 15:33
















You could define an interface containing the common method and properties and change the two classes so that both implement it. Then define your method to take a parameter of this interface type
– Klaus Gütter
Nov 9 at 15:33




You could define an interface containing the common method and properties and change the two classes so that both implement it. Then define your method to take a parameter of this interface type
– Klaus Gütter
Nov 9 at 15:33




2




2




You've tagged this post with "interface", but you're not using one. An interface would solve this problem.
– gunr2171
Nov 9 at 15:33




You've tagged this post with "interface", but you're not using one. An interface would solve this problem.
– gunr2171
Nov 9 at 15:33












3 Answers
3






active

oldest

votes

















up vote
1
down vote













Use a base class with the properties in common.



The classes derive from it with an implementation of the method DoSomeThing.
Then, your method DoSomeThing is called autotomatically for the right type,



public abstract BaseClass
{
public int CustomerID;
public int DriverID;
public int EmployeeID;
public abstract void DoSomeThing();
}

public abstract Class1
{
public override void DoSomeThing(){}
}

public abstract Class
{
public override void DoSomeThing(){}
}

public void DoSomething(BaseClass class)
{
class.DoSomeThing();
}





share|improve this answer






























    up vote
    0
    down vote













    Just my preference is to do interfaces over inheritance if I can. Allows to be a little more flexible I think in the future.



    public static void DoSomething(IDoSomething MyObject)
    {
    MyObject.DoSomeMethod();
    }
    interface IDoSomething
    {
    void DoSomeMethod();
    }
    public class Class1 : IDoSomething
    {
    public int CustomerID;
    public int DriverID;
    public int EmployeeID;

    public void DoSomeMethod()
    {
    throw new NotImplementedException();
    }
    }

    public class Class2 : IDoSomething
    {
    public int CustomerID;
    public int DriverID;
    public int EmployeeID;

    public void DoSomeMethod()
    {
    throw new NotImplementedException();
    }
    }





    share|improve this answer




























      up vote
      -2
      down vote













      you could try somehing like this:



          public static  void DoSomething(object MyObject)
      {
      if (MyObject.GetType() == typeof(Class1))
      {
      // (MyObject as Class1 )
      }

      if (MyObject.GetType() == typeof(Class2))
      {
      // (MyObject as Class2 )
      }
      }





      share|improve this answer





















        Your Answer






        StackExchange.ifUsing("editor", function () {
        StackExchange.using("externalEditor", function () {
        StackExchange.using("snippets", function () {
        StackExchange.snippets.init();
        });
        });
        }, "code-snippets");

        StackExchange.ready(function() {
        var channelOptions = {
        tags: "".split(" "),
        id: "1"
        };
        initTagRenderer("".split(" "), "".split(" "), channelOptions);

        StackExchange.using("externalEditor", function() {
        // Have to fire editor after snippets, if snippets enabled
        if (StackExchange.settings.snippets.snippetsEnabled) {
        StackExchange.using("snippets", function() {
        createEditor();
        });
        }
        else {
        createEditor();
        }
        });

        function createEditor() {
        StackExchange.prepareEditor({
        heartbeatType: 'answer',
        convertImagesToLinks: true,
        noModals: true,
        showLowRepImageUploadWarning: true,
        reputationToPostImages: 10,
        bindNavPrevention: true,
        postfix: "",
        imageUploader: {
        brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
        contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
        allowUrls: true
        },
        onDemand: true,
        discardSelector: ".discard-answer"
        ,immediatelyShowMarkdownHelp:true
        });


        }
        });














         

        draft saved


        draft discarded


















        StackExchange.ready(
        function () {
        StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53228672%2ftwo-classes-which-are-very-similar-i-want-to-pass-one-or-the-other-into-a-metho%23new-answer', 'question_page');
        }
        );

        Post as a guest















        Required, but never shown

























        3 Answers
        3






        active

        oldest

        votes








        3 Answers
        3






        active

        oldest

        votes









        active

        oldest

        votes






        active

        oldest

        votes








        up vote
        1
        down vote













        Use a base class with the properties in common.



        The classes derive from it with an implementation of the method DoSomeThing.
        Then, your method DoSomeThing is called autotomatically for the right type,



        public abstract BaseClass
        {
        public int CustomerID;
        public int DriverID;
        public int EmployeeID;
        public abstract void DoSomeThing();
        }

        public abstract Class1
        {
        public override void DoSomeThing(){}
        }

        public abstract Class
        {
        public override void DoSomeThing(){}
        }

        public void DoSomething(BaseClass class)
        {
        class.DoSomeThing();
        }





        share|improve this answer



























          up vote
          1
          down vote













          Use a base class with the properties in common.



          The classes derive from it with an implementation of the method DoSomeThing.
          Then, your method DoSomeThing is called autotomatically for the right type,



          public abstract BaseClass
          {
          public int CustomerID;
          public int DriverID;
          public int EmployeeID;
          public abstract void DoSomeThing();
          }

          public abstract Class1
          {
          public override void DoSomeThing(){}
          }

          public abstract Class
          {
          public override void DoSomeThing(){}
          }

          public void DoSomething(BaseClass class)
          {
          class.DoSomeThing();
          }





          share|improve this answer

























            up vote
            1
            down vote










            up vote
            1
            down vote









            Use a base class with the properties in common.



            The classes derive from it with an implementation of the method DoSomeThing.
            Then, your method DoSomeThing is called autotomatically for the right type,



            public abstract BaseClass
            {
            public int CustomerID;
            public int DriverID;
            public int EmployeeID;
            public abstract void DoSomeThing();
            }

            public abstract Class1
            {
            public override void DoSomeThing(){}
            }

            public abstract Class
            {
            public override void DoSomeThing(){}
            }

            public void DoSomething(BaseClass class)
            {
            class.DoSomeThing();
            }





            share|improve this answer














            Use a base class with the properties in common.



            The classes derive from it with an implementation of the method DoSomeThing.
            Then, your method DoSomeThing is called autotomatically for the right type,



            public abstract BaseClass
            {
            public int CustomerID;
            public int DriverID;
            public int EmployeeID;
            public abstract void DoSomeThing();
            }

            public abstract Class1
            {
            public override void DoSomeThing(){}
            }

            public abstract Class
            {
            public override void DoSomeThing(){}
            }

            public void DoSomething(BaseClass class)
            {
            class.DoSomeThing();
            }






            share|improve this answer














            share|improve this answer



            share|improve this answer








            edited Nov 9 at 15:46









            Manfred Radlwimmer

            10.3k123350




            10.3k123350










            answered Nov 9 at 15:33









            Antoine V

            4,9472424




            4,9472424
























                up vote
                0
                down vote













                Just my preference is to do interfaces over inheritance if I can. Allows to be a little more flexible I think in the future.



                public static void DoSomething(IDoSomething MyObject)
                {
                MyObject.DoSomeMethod();
                }
                interface IDoSomething
                {
                void DoSomeMethod();
                }
                public class Class1 : IDoSomething
                {
                public int CustomerID;
                public int DriverID;
                public int EmployeeID;

                public void DoSomeMethod()
                {
                throw new NotImplementedException();
                }
                }

                public class Class2 : IDoSomething
                {
                public int CustomerID;
                public int DriverID;
                public int EmployeeID;

                public void DoSomeMethod()
                {
                throw new NotImplementedException();
                }
                }





                share|improve this answer

























                  up vote
                  0
                  down vote













                  Just my preference is to do interfaces over inheritance if I can. Allows to be a little more flexible I think in the future.



                  public static void DoSomething(IDoSomething MyObject)
                  {
                  MyObject.DoSomeMethod();
                  }
                  interface IDoSomething
                  {
                  void DoSomeMethod();
                  }
                  public class Class1 : IDoSomething
                  {
                  public int CustomerID;
                  public int DriverID;
                  public int EmployeeID;

                  public void DoSomeMethod()
                  {
                  throw new NotImplementedException();
                  }
                  }

                  public class Class2 : IDoSomething
                  {
                  public int CustomerID;
                  public int DriverID;
                  public int EmployeeID;

                  public void DoSomeMethod()
                  {
                  throw new NotImplementedException();
                  }
                  }





                  share|improve this answer























                    up vote
                    0
                    down vote










                    up vote
                    0
                    down vote









                    Just my preference is to do interfaces over inheritance if I can. Allows to be a little more flexible I think in the future.



                    public static void DoSomething(IDoSomething MyObject)
                    {
                    MyObject.DoSomeMethod();
                    }
                    interface IDoSomething
                    {
                    void DoSomeMethod();
                    }
                    public class Class1 : IDoSomething
                    {
                    public int CustomerID;
                    public int DriverID;
                    public int EmployeeID;

                    public void DoSomeMethod()
                    {
                    throw new NotImplementedException();
                    }
                    }

                    public class Class2 : IDoSomething
                    {
                    public int CustomerID;
                    public int DriverID;
                    public int EmployeeID;

                    public void DoSomeMethod()
                    {
                    throw new NotImplementedException();
                    }
                    }





                    share|improve this answer












                    Just my preference is to do interfaces over inheritance if I can. Allows to be a little more flexible I think in the future.



                    public static void DoSomething(IDoSomething MyObject)
                    {
                    MyObject.DoSomeMethod();
                    }
                    interface IDoSomething
                    {
                    void DoSomeMethod();
                    }
                    public class Class1 : IDoSomething
                    {
                    public int CustomerID;
                    public int DriverID;
                    public int EmployeeID;

                    public void DoSomeMethod()
                    {
                    throw new NotImplementedException();
                    }
                    }

                    public class Class2 : IDoSomething
                    {
                    public int CustomerID;
                    public int DriverID;
                    public int EmployeeID;

                    public void DoSomeMethod()
                    {
                    throw new NotImplementedException();
                    }
                    }






                    share|improve this answer












                    share|improve this answer



                    share|improve this answer










                    answered Nov 9 at 15:46









                    Ryan Schlueter

                    1,5871816




                    1,5871816






















                        up vote
                        -2
                        down vote













                        you could try somehing like this:



                            public static  void DoSomething(object MyObject)
                        {
                        if (MyObject.GetType() == typeof(Class1))
                        {
                        // (MyObject as Class1 )
                        }

                        if (MyObject.GetType() == typeof(Class2))
                        {
                        // (MyObject as Class2 )
                        }
                        }





                        share|improve this answer

























                          up vote
                          -2
                          down vote













                          you could try somehing like this:



                              public static  void DoSomething(object MyObject)
                          {
                          if (MyObject.GetType() == typeof(Class1))
                          {
                          // (MyObject as Class1 )
                          }

                          if (MyObject.GetType() == typeof(Class2))
                          {
                          // (MyObject as Class2 )
                          }
                          }





                          share|improve this answer























                            up vote
                            -2
                            down vote










                            up vote
                            -2
                            down vote









                            you could try somehing like this:



                                public static  void DoSomething(object MyObject)
                            {
                            if (MyObject.GetType() == typeof(Class1))
                            {
                            // (MyObject as Class1 )
                            }

                            if (MyObject.GetType() == typeof(Class2))
                            {
                            // (MyObject as Class2 )
                            }
                            }





                            share|improve this answer












                            you could try somehing like this:



                                public static  void DoSomething(object MyObject)
                            {
                            if (MyObject.GetType() == typeof(Class1))
                            {
                            // (MyObject as Class1 )
                            }

                            if (MyObject.GetType() == typeof(Class2))
                            {
                            // (MyObject as Class2 )
                            }
                            }






                            share|improve this answer












                            share|improve this answer



                            share|improve this answer










                            answered Nov 9 at 15:43









                            Misael Moneró Thompson

                            193111




                            193111






























                                 

                                draft saved


                                draft discarded



















































                                 


                                draft saved


                                draft discarded














                                StackExchange.ready(
                                function () {
                                StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53228672%2ftwo-classes-which-are-very-similar-i-want-to-pass-one-or-the-other-into-a-metho%23new-answer', 'question_page');
                                }
                                );

                                Post as a guest















                                Required, but never shown





















































                                Required, but never shown














                                Required, but never shown












                                Required, but never shown







                                Required, but never shown

































                                Required, but never shown














                                Required, but never shown












                                Required, but never shown







                                Required, but never shown







                                Popular posts from this blog

                                Schultheiß

                                Liste der Kulturdenkmale in Wilsdruff

                                Android Play Services Check