Re: ref params
- From: "Nicholas Paldino [.NET/C# MVP]" <mvp@xxxxxxxxxxxxxxxxxxxxxxxxxxx>
- Date: Mon, 25 Sep 2006 15:52:30 -0400
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]
- mvp@xxxxxxxxxxxxxxxxxxxxxxxxxxx
"Bill H" <bill@xxxxxxxxxxxxxxx> wrote in message
news:evErhsN4GHA.4164@xxxxxxxxxxxxxxxxxxxxxxx
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]" <mvp@xxxxxxxxxxxxxxxxxxxxxxxxxxx> wrote
in message news:ei2AtMN4GHA.1300@xxxxxxxxxxxxxxxxxxxxxxx
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]
- mvp@xxxxxxxxxxxxxxxxxxxxxxxxxxx
"Bill H" <bill@xxxxxxxxxxxxxxx> wrote in message
news:%23m3ZpHN4GHA.4820@xxxxxxxxxxxxxxxxxxxxxxx
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
.
- References:
- ref params
- From: Bill H
- Re: ref params
- From: Nicholas Paldino [.NET/C# MVP]
- Re: ref params
- From: Bill H
- ref params
- Prev by Date: Problem with reference I think
- Next by Date: Re: String Formatting (Like in VB)
- Previous by thread: Re: ref params
- Next by thread: Re: ref params
- Index(es):
Relevant Pages
|