473,396 Members | 1,918 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,396 software developers and data experts.

Using a ref parameter on a reference variable

My undersstanding of using a ref parameter is to be able to pass initialized
value variables as references. Is there any purpose to using a ref paraneter
with a reference variable ? If one does it the compiler accepts it. Does
this mean anything ?
Nov 16 '05 #1
4 4141
Hi Edward,

Um, could you clarify what you mean?
If you want the original reference to change according to changes
made inside a method referencing a reference variable is very useful.

MyObject mo = new MyObject()

DoStuff(ref mo);
DoMoreStuff(mo);
....

private void DoStuff(ref MyObject m1)
{
if(unsatisfactory)
m1 = new MyObject();
...
}

private void DoOtherStuff(MyObject m2)
{
// m2 would not be the same as m1 without ref
}

On Fri, 12 Nov 2004 12:36:38 -0500, Edward Diener <di****@ORsoftware.com>
wrote:
My undersstanding of using a ref parameter is to be able to pass
initialized
value variables as references. Is there any purpose to using a ref
paraneter
with a reference variable ? If one does it the compiler accepts it. Does
this mean anything ?

--
Happy Coding!
Morten Wennevik [C# MVP]
Nov 16 '05 #2
In other words, passing a reference as a ref allows one to change the
reference itself as well as using the object to which the reference refers.

"Morten Wennevik" <Mo************@hotmail.com> wrote in message
news:opshc5udnmklbvpo@pbn_computer...
Hi Edward,

Um, could you clarify what you mean?
If you want the original reference to change according to changes
made inside a method referencing a reference variable is very useful.

MyObject mo = new MyObject()

DoStuff(ref mo);
DoMoreStuff(mo);
...

private void DoStuff(ref MyObject m1)
{
if(unsatisfactory)
m1 = new MyObject();
...
}

private void DoOtherStuff(MyObject m2)
{
// m2 would not be the same as m1 without ref
}

On Fri, 12 Nov 2004 12:36:38 -0500, Edward Diener <di****@ORsoftware.com>
wrote:
My undersstanding of using a ref parameter is to be able to pass
initialized
value variables as references. Is there any purpose to using a ref
paraneter
with a reference variable ? If one does it the compiler accepts it. Does
this mean anything ?

--
Happy Coding!
Morten Wennevik [C# MVP]

Nov 16 '05 #3
Hi Edward:

Yes, it is possible to pass a reference type as a ref parameter. Doing
so means the method can change the value of the reference type as seen
by the caller. In other words, it could set the ref parameter to null,
and the caller would have a null reference when the method completes.

See Jon's document:
http://www.yoda.arachsys.com/csharp/...ers.html#check

--
Scott
http://www.OdeToCode.com/blogs/scott/

On Fri, 12 Nov 2004 12:36:38 -0500, "Edward Diener"
<di****@ORsoftware.com> wrote:
My undersstanding of using a ref parameter is to be able to pass initialized
value variables as references. Is there any purpose to using a ref paraneter
with a reference variable ? If one does it the compiler accepts it. Does
this mean anything ?


Nov 16 '05 #4
Edward Diener <di****@ORsoftware.com> wrote:
My undersstanding of using a ref parameter is to be able to pass initialized
value variables as references.
No. It's to pass a parameter *by* reference. There's a big difference.
Is there any purpose to using a ref paraneter with a reference variable ?
Yes.
If one does it the compiler accepts it. Does this mean anything ?


Yes.

Compare:

void DoNothing (string s)
{
s = "something";
}

void DoesSomething (ref string s)
{
s = "something";
}

The first of these is a no-op; the second will change the value of the
variable passed (by reference) to a reference to the string
"something".

See http://www.pobox.com/~skeet/csharp/parameters.html for more
information.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 16 '05 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: sam1967 | last post by:
How do I get a function to return a GMP integer type mpz_t when i try it i get an error message. i am trying mpz_t hooch (int x) { mpz_t y; ........
6
by: Mårten Herberthson | last post by:
Introduction: As we all know, values of any class may be assigned to a reference of a superclass. This is simple polymorphism. So if you have a class A and a class B that inherits from A you can...
7
by: Robert Lario | last post by:
For examples sake I have made up a very simple example. I have an object called foo1 which is of type foo. I want to be able to call a funtion called myfunc as follows: myfunc(ref foo1) ...
4
by: Elad Gutman | last post by:
Hello, Something is quite puzzled to me in using the 'ref' keyword, probably due to some lack of basic understanding. Whenever I wanna change the parameter passed to a function I will use the...
0
by: Lokkju | last post by:
I am pretty much lost here - I am trying to create a managed c++ wrapper for this dll, so that I can use it from c#/vb.net, however, it does not conform to any standard style of coding I have seen....
13
by: Andy Baxter | last post by:
Can anyone recommend a good online guide to using objects in javascript? The book I bought (DHTML Utopia) suggests using objects to keep the code clean and stop namespace clashes between different...
6
by: semkaa | last post by:
Can you explain why using ref keyword for passing parameters works slower that passing parameters by values itself. I wrote 2 examples to test it: //using ref static void Main(string args) {...
17
by: Pascal | last post by:
hello everybody ! I get this error 3 times : "The output parameter must be assigned before the control leaves the current method" (dor dMin and dMax) this one 1 time : Use of the parameter 'out'...
65
by: Arjen | last post by:
Hi, Form a performance perspective, is it wise to use the ref statement as much as possible? Thanks! Arjen
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

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.