473,775 Members | 2,610 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 4160
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(unsatisfacto ry)
m1 = new MyObject();
...
}

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

On Fri, 12 Nov 2004 12:36:38 -0500, Edward Diener <di****@ORsoftw are.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:opshc5udnm klbvpo@pbn_comp uter...
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(unsatisfacto ry)
m1 = new MyObject();
...
}

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

On Fri, 12 Nov 2004 12:36:38 -0500, Edward Diener <di****@ORsoftw are.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****@ORsoftw are.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****@ORsoftw are.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.co m>
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
3834
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
2556
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 write a method that returns an instance of B and assign that to a reference of type A. (Trivial) Problem:
7
5472
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) here's the function : public void myfunc(ref object foo)
4
6364
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 'ref' keyword, that's fine, and it even makes sense when sending a Value Type parameter (int, float or a struct, for example). However, if I'm sending a class object to the function, and this object is a Reference Type, of course, isn't it being...
0
3940
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. It is almost like it is trying to implement it's own COM interfaces... below is the header, and a link to the dll+code: Zip file with header, example, and DLL:...
13
2136
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 parts of the code, but as far as I can see, the way objects work in javascript is quite awkward. I had it working the way they suggest in the book, and it was going OK until I wanted to call one object method from another - I couldn't find a...
6
3681
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) { List<TimeSpantimes = new List<TimeSpan>(); DateTime start; DateTime end; for (int j = 0; j < 1000; j++)
17
2237
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' is not assigned 'dMax' This is my first steps in C#: i can't manage(understand) this error. I would like to test to variables which can be change on form1 by 2 numeric updown controls before sending them to a UserControl which make some...
65
3927
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
9622
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
10107
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10048
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9916
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
7464
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6718
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
1
4017
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3611
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2853
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.