By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
434,894 Members | 2,002 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 434,894 IT Pros & Developers. It's quick & easy.

ref params

P: n/a
i'm new to C#...

How can I create an out or ref param for a method without having to declare
it in the caling code ? Like the DataAdapter can do on its Fill method.

DataTable dt = new DataTable();
SqlDataAdapter.Fill(dt);

'dt' is passed by ref, but it is not required to pass it as such:
SqlDataAdapter.Fill(ref dt);
Thanks,
Bill
Sep 25 '06 #1
Share this Question
Share on Google+
7 Replies


P: n/a
Bill,

This is incorrect. dt is not passed by ref. Rather, DataTable is a
reference type, meaning that when you assign it, you pass the reference. If
you were passing the parameter by ref, then you would be able to change the
reference that you are passing in.

For example, if the Fill method did something like this:

public void Fill(DataTable table)
{
table = new DataTable();
}

When you return, you will see that the parameter passed to table doesn't
in fact change.

Hope this helps.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

"Bill H" <bi**@hostunknown.comwrote in message
news:%2****************@TK2MSFTNGP06.phx.gbl...
i'm new to C#...

How can I create an out or ref param for a method without having to
declare it in the caling code ? Like the DataAdapter can do on its Fill
method.

DataTable dt = new DataTable();
SqlDataAdapter.Fill(dt);

'dt' is passed by ref, but it is not required to pass it as such:
SqlDataAdapter.Fill(ref dt);
Thanks,
Bill

Sep 25 '06 #2

P: n/a
Thanks for your response Nicholas .

but I'm still a bit confused...

When da.Fill returns, my DataTable param var is populated. That implies
that it was passed by ref, correct ? But, I thought (and read, I think) in
C#, in order to declare a parameter as ref, you need to declare it as such.
You are right in your example, when I create a sub of my own, similiar to
the da.Fill method, the param is not affected on the calling side. So why
is the DataAdapter Fil method able to affect the DT param, and not mine ?

Thanks for being patient. I'm a veteran VB programmer, so this is a
humbling experience.

Bill : )

"Nicholas Paldino [.NET/C# MVP]" <mv*@spam.guard.caspershouse.comwrote in
message news:ei**************@TK2MSFTNGP05.phx.gbl...
Bill,

This is incorrect. dt is not passed by ref. Rather, DataTable is a
reference type, meaning that when you assign it, you pass the reference.
If you were passing the parameter by ref, then you would be able to change
the reference that you are passing in.

For example, if the Fill method did something like this:

public void Fill(DataTable table)
{
table = new DataTable();
}

When you return, you will see that the parameter passed to table
doesn't in fact change.

Hope this helps.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

"Bill H" <bi**@hostunknown.comwrote in message
news:%2****************@TK2MSFTNGP06.phx.gbl...
>i'm new to C#...

How can I create an out or ref param for a method without having to
declare it in the caling code ? Like the DataAdapter can do on its Fill
method.

DataTable dt = new DataTable();
SqlDataAdapter.Fill(dt);

'dt' is passed by ref, but it is not required to pass it as such:
SqlDataAdapter.Fill(ref dt);
Thanks,
Bill


Sep 25 '06 #3

P: n/a
Bill,

No, it was passed by value. The REFERENCE was passed by value. That
means that the parameter can't be changed, but whatever the parameter points
to can be changed.

In VB, it's the same as passing a class without using ByRef. If you
pass the class and change a property on the class, you will see it outside
of the method.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

"Bill H" <bi**@hostunknown.comwrote in message
news:ev**************@TK2MSFTNGP05.phx.gbl...
Thanks for your response Nicholas .

but I'm still a bit confused...

When da.Fill returns, my DataTable param var is populated. That implies
that it was passed by ref, correct ? But, I thought (and read, I think)
in C#, in order to declare a parameter as ref, you need to declare it as
such. You are right in your example, when I create a sub of my own,
similiar to the da.Fill method, the param is not affected on the calling
side. So why is the DataAdapter Fil method able to affect the DT param,
and not mine ?

Thanks for being patient. I'm a veteran VB programmer, so this is a
humbling experience.

Bill : )

"Nicholas Paldino [.NET/C# MVP]" <mv*@spam.guard.caspershouse.comwrote
in message news:ei**************@TK2MSFTNGP05.phx.gbl...
>Bill,

This is incorrect. dt is not passed by ref. Rather, DataTable is a
reference type, meaning that when you assign it, you pass the reference.
If you were passing the parameter by ref, then you would be able to
change the reference that you are passing in.

For example, if the Fill method did something like this:

public void Fill(DataTable table)
{
table = new DataTable();
}

When you return, you will see that the parameter passed to table
doesn't in fact change.

Hope this helps.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

"Bill H" <bi**@hostunknown.comwrote in message
news:%2****************@TK2MSFTNGP06.phx.gbl...
>>i'm new to C#...

How can I create an out or ref param for a method without having to
declare it in the caling code ? Like the DataAdapter can do on its Fill
method.

DataTable dt = new DataTable();
SqlDataAdapter.Fill(dt);

'dt' is passed by ref, but it is not required to pass it as such:
SqlDataAdapter.Fill(ref dt);
Thanks,
Bill



Sep 25 '06 #4

P: n/a
DataTable is a reference type in the CLR. Value types like int or bool need
to be passed by reference if you intend to modify their value as a
side-effect of the method invocation. In the case of the DataTable, Fill()
requires an initialized instance of the DataTable class. Because the
DataTable is a reference type in the CLR, the Fill() populates the DataTable
instance by calling on the appropriate DataTable methods. The DataTable
instance is modified and returned from the method. The following link
discusses 'ref'
http://msdn.microsoft.com/library/de...l/vclrfref.asp

and this link discusses value types

http://msdn.microsoft.com/library/de...valuetypes.asp

Sep 25 '06 #5

P: n/a
Bill H <bi**@hostunknown.comwrote:
Thanks for your response Nicholas .

but I'm still a bit confused...

When da.Fill returns, my DataTable param var is populated. That implies
that it was passed by ref, correct ?
Nope. It's a reference passed by value.

See http://www.pobox.com/~skeet/csharp/parameters.html

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
Sep 25 '06 #6

P: n/a
thank you everyone. I'm sorry to have dragged you all into something that I
probably would have realized sooner or later anyway.

I now understand where I was going wrong. Your link, Jon, to the Params web
page, helped me realize my err.
I've been working with reference and value types forever now, and do truly
understand the difference and how they work.
You see, I've never encountered this situation before, because of my own
work habits. I've always declared my params as Ref if I knew I wanted to
affect that var. The Da.Fill method threw me off, because it modifies my DT
without the 'ref' keyword. I realize now that it was manipulating the data
within the param, as opposed to reassigning the reference pointer. I tend
to reassign the pointer, and that's what I naturally was trying to do here,
except without the 'ref' keyword.

Your input was valuable. I thank you all again.

Bill


"Jon Skeet [C# MVP]" <sk***@pobox.comwrote in message
news:MP************************@msnews.microsoft.c om...
Bill H <bi**@hostunknown.comwrote:
>Thanks for your response Nicholas .

but I'm still a bit confused...

When da.Fill returns, my DataTable param var is populated. That implies
that it was passed by ref, correct ?

Nope. It's a reference passed by value.

See http://www.pobox.com/~skeet/csharp/parameters.html

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too

Sep 25 '06 #7

P: n/a
Bill H <bi**@hostunknown.comwrote:
thank you everyone. I'm sorry to have dragged you all into something that I
probably would have realized sooner or later anyway.

I now understand where I was going wrong. Your link, Jon, to the Params web
page, helped me realize my err.
I've been working with reference and value types forever now, and do truly
understand the difference and how they work.
You see, I've never encountered this situation before, because of my own
work habits. I've always declared my params as Ref if I knew I wanted to
affect that var. The Da.Fill method threw me off, because it modifies my DT
without the 'ref' keyword. I realize now that it was manipulating the data
within the param, as opposed to reassigning the reference pointer. I tend
to reassign the pointer, and that's what I naturally was trying to do here,
except without the 'ref' keyword.

Your input was valuable. I thank you all again.
I'm glad the page helped, but I'd urge you to *try* working without
reference parameters wherever possible. They're sometimes useful, but
they usually go against the idea of a method doing exactly one thing.
You'll notice that very few framework methods take out/ref parameters.
I'm not saying your code doesn't work, or that the larger design is bad
or wrong, but it's worth at least thinking about how you'd code
differently if you didn't use out/ref much.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
Sep 26 '06 #8

This discussion thread is closed

Replies have been disabled for this discussion.