none
Best practice: Passing around object references vs. private member variable RRS feed

  • Question

  • What is the best practice for referencing the same object.

    I have an object which has 1 public method.  When this method is called an object is passed through.  Internally this object is passed from one private method to the next and its values are read.

    Should I rather be setting a private member and then all the private methods simple access the same reference?

    private object _object;

    public void A(object object)
    {
       _object = object

       B();
    }

    public void B()
    {
     ... = _object.Property1;
    }


    • Changed type TheLearner Monday, July 6, 2009 2:05 PM
    Monday, July 6, 2009 1:59 PM

Answers

  • Not unless you need to.  Stick with passing the object as a parameter instead.  There are times when you will want to store a class-level field, but you shouldn't ever use a field simply as a convenient place to hold data while you call a series of methods.  Unless the field has a reason to stay resident inside the class after the hierarchy of methods has been called, use a parameter instead.


    David Morton - http://blog.davemorton.net/ - @davidmmorton - ForumsBrowser, a WPF MSDN Forums Client
    • Marked as answer by TheLearner Monday, July 6, 2009 2:06 PM
    Monday, July 6, 2009 2:03 PM
    Moderator
  • If this is a real example I would suggest passing as a parameter, simply because method B() is marked public and could therefore be called without A() setting up the field with the correct object first, and therefore a null exception would follow.

    If this is an error, and B() will be private, then I would still consider passing the object as a parameter to B(), this is because subsequent calls to A() will change the value of the private field and may break the logic of the original call.

    Hope this helps

    Tom


    • Marked as answer by TheLearner Monday, July 6, 2009 2:06 PM
    Monday, July 6, 2009 2:05 PM

All replies

  • Not unless you need to.  Stick with passing the object as a parameter instead.  There are times when you will want to store a class-level field, but you shouldn't ever use a field simply as a convenient place to hold data while you call a series of methods.  Unless the field has a reason to stay resident inside the class after the hierarchy of methods has been called, use a parameter instead.


    David Morton - http://blog.davemorton.net/ - @davidmmorton - ForumsBrowser, a WPF MSDN Forums Client
    • Marked as answer by TheLearner Monday, July 6, 2009 2:06 PM
    Monday, July 6, 2009 2:03 PM
    Moderator
  • OK tks!
    Monday, July 6, 2009 2:05 PM
  • If this is a real example I would suggest passing as a parameter, simply because method B() is marked public and could therefore be called without A() setting up the field with the correct object first, and therefore a null exception would follow.

    If this is an error, and B() will be private, then I would still consider passing the object as a parameter to B(), this is because subsequent calls to A() will change the value of the private field and may break the logic of the original call.

    Hope this helps

    Tom


    • Marked as answer by TheLearner Monday, July 6, 2009 2:06 PM
    Monday, July 6, 2009 2:05 PM