Re: ref params

Tech Tip: Click here to run a free scan for Windows Errors and optimize PC performance



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







.



Relevant Pages

  • Re: perl OOP
    ... XC> sub name_me { ... no. it is always a blessed reference. ... XC> pass a param and act on its value only; ... objects are always passed as a ref since only refs can be blessed. ...
    (comp.lang.perl.misc)
  • Re: different forms
    ... You can declare a static variable of the main form's type at the main ... You can pass a reference of the form that opens the second/third/etc form ... they are being copied always, exept when you pass them by ref), you don't ... If you want to pass vars that are not of a ref type, ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: ref params
    ... my DataTable param var is populated. ... C#, in order to declare a parameter as ref, you need to declare it as such. ... reference type, meaning that when you assign it, you pass the reference. ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: Problem Passing control as a param into a class function
    ... a reference or out type. ... I would think that you just need to declare a parameter of type ... ControlCollection and that's that. ... if you don't make it "ref" or "out"? ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: Problem Passing control as a param into a class function
    ... > a reference or out type. ... I would think that you just need to declare a parameter of type ... ControlCollection with ref yields the following errors. ...
    (microsoft.public.dotnet.languages.csharp)