Re: Passing in SqlConnection by ref

From: LP (lp_at_a.com)
Date: 02/13/05


Date: Sun, 13 Feb 2005 12:44:57 -0500

Craig, Now, that's what I call a good explanation. Thanks a lot.

My concern was if I pass in connection to another method, I wanted to make
sure that both objects will be working with same instance of connection
object. So it can closed/disposed in one place. I thought I needed to pass
it in with "ref" to accomplish that.
>From what you're saying as long as I don't create a new instance in a
receiving method, it will be the same instance of conn object, since it's a
reference type. So "ref" is not really required, though it would more or
less do the same thing (it's a true pointer as oppose to reference type).
Please let me know if my interpretations are correct.

Thanks again.

"Craig Wagner" <craig_d_wagner@hotmail.com> wrote in message
news:2nsu01dc9cun6qp4ks5of4hl0r08as9ha8@4ax.com...
> "LP" <lp@a.com> wrote:
>
> >thank you guys
> >I am a bit confused. On one hand SqlConnection is an object and passed by
> >reference (which is a "memory address" or "pointer") by default but on
> >another hand ref keyword passes in "a pointer"
> >Does ref keyword makes any difference when dealing with refernce types?
What
> >am I missing here?
>
> It's understandable you're confused. I understand what's happening and
after
> reading the explanations I'm confused. :-)
>
> Let's try it this way...
>
> When you pass an object to a method using the default calling syntax, you
are
> always passing a copy of the reference to the object (the address).
>
> If the method has the argument declared as ref, you are passing the
address of
> the address, meaning you could replace the original object. Let's look at
a
> couple of examples.
>
> Let's say the connection argument is called "conn". In the called method
if you
> did:
>
> conn.ConnectionString = "..."
>
> The original object (the one you passed in) would have its connection
string
> changed regardless of whether it was passed by reference or by value.
>
> The real difference comes in where your method creates a new object using
the
> same reference variable (pointer).
>
> private void MyMethod( SqlConnection conn )
> {
> conn = new SqlConnection(...);
> }
>
> private void MyMethod( ref SqlConnection conn )
> {
> conn = new SqlConnection(...);
> }
>
> In the first example, there are now two SqlConnection objects, one pointed
to by
> the copy of the address and one pointed to by the original reference
variable
> (in the calling routine). When you return to the calling routine you would
find
> the original unchanged. You've said "Here's a copy of the original address
to
> the object. If you put a new address in its place that's cool, I still
have the
> original."
>
> In the second example you're telling the framework to replace the original
> connection object (the one passed in) with the new one. You've said
"Here's
> where you'll find the original pointer to the object. Please replace it
with a
> new pointer."
>
> Yet another way to look at it is this:
> a) Passing an object by value is like sending a fax. You've still got the
> original in case someone screws up the faxed copy.
>
> b) Passing an object by ref is like sending the original document by
courier. If
> they mark it up and send it back you've got no way to get back to the
original.
>
> --
> Craig Wagner, craig.wagner(at)comcast.net
> Portland, OR
>
> "Don't ban high-performance vehicles, ban low-performance drivers!"



Relevant Pages

  • Re: Passing in SqlConnection by ref
    ... My concern was if I pass in connection to another method, ... it in with "ref" to accomplish that. ... less do the same thing (it's a true pointer as oppose to reference type). ...
    (microsoft.public.dotnet.framework.adonet)
  • Re: gdb help: debugging a segfault in boost::shared_ptr
    ... PoolObject is a wrapper that contains the pointer and some other data. ... If this connection ... reference as before to return the pointer. ...
    (comp.os.linux.development.apps)
  • Re: how to use List Class in Base class?
    ... If I don't pass by ref and pass the instance of the class to ... Reference types are already a reference to an object allocated ... Passing a reference type as is (i.e. ... you'd be passing a pointer to a pointer to an instance. ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: Passing in SqlConnection by ref
    ... On one hand SqlConnection is an object and passed by ... reference (which is a "memory address" or "pointer") by default but on ... >> Object A opens a Sql Db connection to execute number of SqlCommands. ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: Passing in SqlConnection by ref
    ... On one hand SqlConnection is an object and passed by ... reference (which is a "memory address" or "pointer") by default but on ... >> Object A opens a Sql Db connection to execute number of SqlCommands. ...
    (microsoft.public.dotnet.framework.adonet)

Quantcast