ref params

B

Bill H

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
 
N

Nicholas Paldino [.NET/C# MVP]

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.
 
B

Bill H

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 said:
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]
- (e-mail address removed)

Bill H said:
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
 
N

Nicholas Paldino [.NET/C# MVP]

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]
- (e-mail address removed)

Bill H said:
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 said:
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]
- (e-mail address removed)

Bill H said:
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
 
N

Noah Sham

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/default.asp?url=/library/en-us/csref/html/vclrfref.asp

and this link discusses value types

http://msdn.microsoft.com/library/default.asp?url=/library/en-us/csref/html/vcrefvaluetypes.asp
 
B

Bill H

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
 
J

Jon Skeet [C# MVP]

Bill H said:
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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top