none
Is OleDbDataReader passed by VALUE ? RRS feed

  • Question

  • I know that if there is an object as a parameter in a Method, it is passed by reference but following example is a different. I wanna send an OleDbDataReader object and run a query.
    After sql execution, method try to set pDR but after method execution I check datareader object's value that is null. I think object sent pass by value but every object is not reference at C# ?  How can you explain this situation ?


    public  class A {
     public int GetData(OleDbDataReader pDR, string pSql)
            {
                OleDbCommand cmd = new OleDbCommand();
                cmd.Connection = oConn;
                cmd.CommandText = pSql;
                pDR  = cmd.ExecuteReader();
                return 1;
            }
        private void deneme()
        {
           OledbDataReader b = null;
           GetData(b, "Select * from users");
           // b is still null...
        }
    }

    Tuesday, June 5, 2007 10:54 AM

All replies

  • Parameters are not necessarily pass by reference.  By default all parameters are passed by value.  For value types a copy of the value is passed.  For reference types a copy of the underlying pointer to the object is passed.  Thus with value types you can change the parameter value all you want but it'll have no impact on the original value that was passed to you.  Reference types on the other hand will point to the same instance so any properties you set within the function will be visible outside of it as well.  However if you were to reassign the parameter value a new instance then it would not be seen outside the function.

     

    To mark a parameter as pass by reference you must include the ref modifier on the parameter.  To mark a parameter as output only (meaning the parameter will always be assigned a value so the input value is not important) then use out but this is not recommended in general.

     

    Code Snippet

    public void Foo (

         int valueTypePassByValue, Control refTypePassByValue,

         ref int valueTypePassByRef, ref Control refTypePassByRef )

    {

        //Changing valueTypePassByValue has no impact on the original argument used

        valueTypePassByValue = 10;

     

        //Changing refTypePassByValue by setting properties or fields impacts the original argument

        //as well since they point to the same instance

        refTypePassByValue.Name = "NewName";

     

        //Changing what refTypePassByValue points to only impacts the parameter.  The original

        //argument still points to its original value

        refTypePassByValue = new TextBox();

     

        //Changing valueTypePassByRef will impact the original argument as well (literals are not

        //allowed as an argument)

        valueTypePassByRef = 20;

     

        //Changing refTypePassByRef will also impact the original argument

        refTypePassByRef = new CheckBox();

    }

     

    In your case I'd recommend returning the reader as a parameter.  This makes the code easier to use.  A null reader indicates something went wrong.  You should throw an exception in error cases rather than returning status codes.

     

    Code Snippet

    public  class A {
         public OleDbDataReader GetData(string pSql)
          {

                OleDbCommand cmd = new OleDbCommand();
                cmd.Connection = oConn;
                cmd.CommandText = pSql;
                return cmd.ExecuteReader();
         }


        private void deneme()
        {
           OledbDataReader b = GetData("Select * from users");
        }

     

    You should prefer return values in lieu of ref parameters in most cases.  If you really needed to do what you wanted then you would do this instead:

     

    Code Snippet

    public  class A {
         public int GetData(ref OleDbDataReader rdr, string pSql)
          {

                OleDbCommand cmd = new OleDbCommand();
                cmd.Connection = oConn;
                cmd.CommandText = pSql;
                rdr = cmd.ExecuteReader();

              return 1;
         }

     

    However you gain nothing by it and your users will probably never check the return code anyway.  Yet they will still need to check for null.

     

    Michael Taylor - 6/5/07

    http://p3net.mvps.org

     

     

     

     

     

    Tuesday, June 5, 2007 12:57 PM
  •  

    when I define an object from OleDbDataReader class, is this object type, reference type or value type ? According to your explanation this object is valueType ?

    Let me know, in general definition in an OOP language; if object defined from  any class, it must be a reference from that class ? is not it ?

    Wednesday, June 6, 2007 5:56 AM