Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / Languages / C# / October 2007

Tip: Looking for answers? Try searching our database.

Parameter as reference but I am not using "ref"

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Everton Berz - 12 Oct 2007 17:22 GMT
Hi,

In the follow program I would like to have the parameter "c" in method
"doSomething" as a value parameter, but it's running as a reference
parameter. Why?  How can I get parameter "c" as a value (or clone,
copy)?

Current result:
value1
value2

Expected result:
value1

class Program
{
       static void Main(string[] args)
       {
               SqlCommand sqlCommandSelect = new SqlCommand();
               sqlCommandSelect.Parameters.AddWithValue("@col1",
"value1");
               doSomething(sqlCommandSelect.Parameters);

               foreach (SqlParameter param in
sqlCommandSelect.Parameters)
               {
                       Console.WriteLine(param.Value);
               }
               Console.ReadLine();
       }

       private static void doSomething(SqlParameterCollection c)
       {
               c.AddWithValue("@col2", "value2");
               //I want to use the new value only here
       }

}
Nicholas Paldino [.NET/C# MVP] - 12 Oct 2007 17:33 GMT
Everton,

   SqlParameterCollection is a reference type.  The parameter itself can
not be changed, but the parameter is a reference.  You can still modify what
the refrerence points to.

Signature

         - Nicholas Paldino [.NET/C# MVP]
         - mvp@spam.guard.caspershouse.com

> Hi,
>
[quoted text clipped - 34 lines]
>
> }
Ignacio Machin ( .NET/ C# MVP ) - 12 Oct 2007 17:42 GMT
Hi,

Signature

Ignacio Machin
www.laceupsolutions.com
Mobile & warehouse Solutions.

> Hi,
>
> In the follow program I would like to have the parameter "c" in method
> "doSomething" as a value parameter, but it's running as a reference
> parameter. Why?  How can I get parameter "c" as a value (or clone,
> copy)?

Take a look at http://www.yoda.arachsys.com/csharp/parameters.html it's a
very good explanation about parameters in C#
Peter Duniho - 12 Oct 2007 18:15 GMT
> In the follow program I would like to have the parameter "c" in method
> "doSomething" as a value parameter, but it's running as a reference
> parameter. Why?  How can I get parameter "c" as a value (or clone,
> copy)?

As Nicholas says, you can't.  You'd have to clone it explicitly before
passing it, as the type is a reference type and so a reference is always
passed.  It's passed by value, but it's still a reference.

This (or at least, the more general topic) is a common question and Jon
Skeet's web site includes an article that should help:
http://www.pobox.com/~skeet/csharp/parameters.html

Pete
Everton Berz - 15 Oct 2007 08:25 GMT
Thank you for answer.

I really did not know that SqlParameterCollection is a reference
type.
There is no Clone method in this class, I think I need to clone my
SqlCommand object.

--
Everton

> > In the follow program I would like to have the parameter "c" in method
> > "doSomething" as a value parameter, but it's running as a reference
[quoted text clipped - 9 lines]
>
> Pete

Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.