473,327 Members | 2,074 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,327 software developers and data experts.

ref params

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
7 4018
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
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
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
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
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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: Mihaly | last post by:
Hello, i'm using a VC++ com+ object, it's reference in vs .net, this com object have a function that have to params, the interop com object recive Object Params and the original com object say...
2
by: Tobias Olbort | last post by:
Hello, i've a outer function, which takes a params-array as a parameter. I want to pass this array to inner function with a params-array (e. g. string.format). When i've passed an integer to...
0
by: Dominik Tropper | last post by:
Dear all I have created a c# web project in VS.NET 2003 that contains a crystal report with two params. No problems until here, but executing the report selects the wrong records. My params...
1
by: M Jared Finder | last post by:
What are the rules for method overload resolution when there are functions with a params argument? No mention of params is made in section 7.4.2 of the C# specification on MSDN (titled "Overload...
3
by: Stan Huff | last post by:
Is there any way to disable the "params" on a particular invocation so that one can pass an array containing the arguments and not have receiver get an array having you argument array stuffed into...
2
by: Coneection OLAP | last post by:
Hi: Me question is the next: I would like know how, I can do it to pass params, from a control of a page ASPX to other page ASPX, but I don't want that this params can see in the URL, ...
6
by: Praveen | last post by:
As you all know the value of input, checkbox and other "user editable" elements can be retrieved on postback via Request.Params list, if you know their ID. However, if there is a span element...
8
by: David Duerrenmatt | last post by:
Hi there For some reasons, I've to use Python 1.5.2 and am looking for a workaround: In newer Python versions, I can call a function this way: func = some_function func(*params) Then,...
3
by: rushik | last post by:
Hello, In our code i've seen somewhere fun1()->fun2(params), i m not able to understand what it means? our code is completely written in C++. Thanks Ru.
2
by: FFMG | last post by:
Hi, I was looking at http://www.php.net/manual/en/function.call-user-func-array.php and I was wondering... Given, // -- function foo( &$params) {
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.