473,796 Members | 2,444 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Passing by reference twice

If I am passing a variable by reference to another routine by reference, do
I need to dereference first?

string testString;
....

FirstSub(ref firstString)
{
HandleString(re f firstString); //do I need to de
reference here or just pass theString without the "ref"
}
void HandleString(re f theString)
{
Console.WriteLi ne(thestring);
}
Apr 18 '06 #1
22 3502
"tshad" <ts**********@f tsolutions.com> wrote in message
news:Oc******** ******@TK2MSFTN GP05.phx.gbl...
If I am passing a variable by reference to another routine by reference,
do I need to dereference first?

string testString;
...

FirstSub(ref firstString)
{
HandleString(re f firstString); //do I need to de
reference here or just pass theString without the "ref"
}
void HandleString(re f theString)
{
Console.WriteLi ne(thestring);
}
No, but why pass it a ref?


Apr 18 '06 #2
Because I am building the string in another function which can get fairly
large and I don't want to pass the actual string which will get very large.

It does seem to work now and I am not sure what I was doing that was causing
the problem. The example actually work.

I am curious as to why it work, however.

If I have testString and I pass it as a reference to FirstSub, then it has
the reference to testString (not the string itself). When I pass it to
HandleString, aren't I passing a reference to a reference of testString?

If that is the case, how does HandleString handle it correctly?

Thanks,

Tom

"Michael C" <no****@nospam. com> wrote in message
news:u4******** ******@TK2MSFTN GP03.phx.gbl...
"tshad" <ts**********@f tsolutions.com> wrote in message
news:Oc******** ******@TK2MSFTN GP05.phx.gbl...
If I am passing a variable by reference to another routine by reference,
do I need to dereference first?

string testString;
...
FirstSub(ref testString);

void FirstSub(ref firstString) {
HandleString(re f firstString); //do I need to de
reference here or just pass theString without the "ref"
}
void HandleString(re f theString)
{
Console.WriteLi ne(thestring);
}


No, but why pass it a ref?



Apr 18 '06 #3
"tshad" <ts**********@f tsolutions.com> wrote in message
news:Ol******** ******@TK2MSFTN GP02.phx.gbl...
Because I am building the string in another function which can get fairly
large and I don't want to pass the actual string which will get very
large.
C# doesn't copy the entire string when passing it by value. It's a reference
type so only a pointer to it will be passed anyway. You only need to pass a
reference if you want to modify the string.
If that is the case, how does HandleString handle it correctly?


It probably just passes on the pointer it has.

Michael
Apr 18 '06 #4

"Michael C" <no****@nospam. com> wrote in message
news:uT******** *****@TK2MSFTNG P02.phx.gbl...
"tshad" <ts**********@f tsolutions.com> wrote in message
news:Ol******** ******@TK2MSFTN GP02.phx.gbl...
Because I am building the string in another function which can get fairly
large and I don't want to pass the actual string which will get very
large.
C# doesn't copy the entire string when passing it by value. It's a
reference type so only a pointer to it will be passed anyway. You only
need to pass a reference if you want to modify the string.


As strings are immutable in C#, in neither case (ref or not) is it possible
for you to modify a string.
If it's passed by ref, you can modify the reference that the caller has
passed to a new string, but you still can not change the existing string.
If you want to actually "modify a string", pass a StringBuilder or char[]
instead.

m
If that is the case, how does HandleString handle it correctly?


It probably just passes on the pointer it has.

Michael

Apr 18 '06 #5
Hi,

In .NET, String is a Reference Type. It is allocated on the heap, and
the variable that you've declared (testString) contains it's address (a
pointer to it). This means that you will always be passing some form of
reference to the instance rather than the instance itself, whether you
pass the testString variable by ref or by value -- so I'm not sure that
you need to put the 'ref' bit in.

If testString was a Value Type, then adding ref would give you the same
behaviour as when you pass a Reference Type by value -- it would allow
you to manipulate the same instance of the Value Type.

In the code in your post, would I think that all of the variables will
be pointing to the same instance of your String. I suspect you could
drop the ref and all would be ok.

Cheers, Mart.

Apr 18 '06 #6
"Mike" <vi********@yah oo.com> wrote in message
news:uT******** ******@TK2MSFTN GP05.phx.gbl...
As strings are immutable in C#, in neither case (ref or not) is it
possible for you to modify a string.
If it's passed by ref, you can modify the reference that the caller has
passed to a new string, but you still can not change the existing string.
If you want to actually "modify a string", pass a StringBuilder or char[]
instead.


That's true, although from outside the function the appearance is the string
has been modified. This is the same as any time you use a string as you're
always getting a reference to a new string, so the case of using the
function is nothing special.

Michael
Apr 18 '06 #7
> Because I am building the string in another function which can get fairly
large and I don't want to pass the actual string which will get very large.
A string is a reference type, so whenever you pass it, it doesn't get
copied on the stack anyway. You're always passing a reference.

The only question is whether you want to replace the string you passed
with a different string, in which case you need the "ref" keyword. What
"ref" says is that you may want to change the variable that contains a
reference (pointer) to the string to contain a reference (pointer) to a
different string upon return.

So, if HandleString changes the string to which theString points, and
wants the caller to FirstSub to receive the change, then you need
"ref". Something like this:

void FirstSub(ref string theString)
{
...
HandleString(re f theString);
...
}

void HandleString(re f string aString)
{
aString = String.Format(. ..);
}

Then any caller to FirstSub will get a changed string on return.

If FirstSub and HandleString are just _using_ the string for something,
but not replacing it with a different string, there is no reason to use
"ref".
I am curious as to why it works, however.

If I have testString and I pass it as a reference to FirstSub, then it has
the reference to testString (not the string itself). When I pass it to
HandleString, aren't I passing a reference to a reference of testString?


No, because C# isn't like C. In C#, when you mention the variable name
then the language dereferences automatically. So, in

void FirstSub(ref string theString)
{
...
}

when you say "theString" , you are not talking about the reference to
"theString" . You are talking about "theString" itself (which is a
reference to a string). So, no, you never get a reference to a
reference to a reference, because there is no language construct for
talking about "the reference to the reference called 'theString'". The
"ref" simply indicates how "theString" gets passed to the method. Once
inside the FirstSub method, "theString" is just a normal string again.

Apr 18 '06 #8
Mart <ma***********@ simpl.co.nz> wrote:
In .NET, String is a Reference Type. It is allocated on the heap, and
the variable that you've declared (testString) contains it's address (a
pointer to it). This means that you will always be passing some form of
reference to the instance rather than the instance itself, whether you
pass the testString variable by ref or by value -- so I'm not sure that
you need to put the 'ref' bit in.


He does if the point is that a method lower down the call chain will
change the value of the parameter to be a different string. For
instance:

using System;

public class Test
{
static void Main()
{
string x = "hello";

Foo (ref x);
Console.WriteLi ne (x);
}

static void Foo (ref string y)
{
Bar (ref y);
}

static void Bar (ref string z)
{
z = "there";
}
}

The code actually being shown doesn't use this facility, admittedly,
and it looks like the OP does have misconceptions given his reluctance
to pass the string reference by value because "I don't want to pass the
actual string which will get very large".

Obligatory link for this kind of thread:
http://www.pobox.com/~skeet/csharp/parameters.html

--
Jon Skeet - <sk***@pobox.co m>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
Apr 18 '06 #9
"tshad" <ts**********@f tsolutions.com> ha scritto nel messaggio
news:Ol******** ******@TK2MSFTN GP02.phx.gbl...
Because I am building the string in another function which can get fairly
large and I don't want to pass the actual string which will get very
large.


IMHO it is not a good design the use of "ref" in a method.
It only add confusion for the use of the method.

For strings also, since they are immutable, there is no reasons to use ref.

Best a simple method following the designing guideline:

public string GetHandledStrin g(string myString)
{
myString = ...
return myString;
}
--

Free .Net Reporting Tool - http://www.neodatatype.net
Apr 18 '06 #10

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

Similar topics

58
10183
by: jr | last post by:
Sorry for this very dumb question, but I've clearly got a long way to go! Can someone please help me pass an array into a function. Here's a starting point. void TheMainFunc() { // Body of code... TCHAR myArray; DoStuff(myArray);
25
2947
by: Victor Bazarov | last post by:
In the project I'm maintaining I've seen two distinct techniques used for returning an object from a function. One is AType function(AType const& arg) { AType retval(arg); // or default construction and then.. // some other processing and/or changing 'retval' return retval; }
6
8831
by: keepyourstupidspam | last post by:
Hi, I want to pass a function pointer that is a class member. This is the fn I want to pass the function pointer into: int Scheduler::Add(const unsigned long timeout, void* pFunction, void* pParam)
8
2118
by: Dennis Myrén | last post by:
I have these tiny classes, implementing an interface through which their method Render ( CosWriter writer ) ; is called. Given a specific context, there are potentially a lot of such objects, each requiring a call to that method to fulfill their purpose. There could be 200, there could be more than 1000. That is a lot of references passed around. It feels heavy. Let us say i changed the signature of the interface method to:
6
6002
by: MSDNAndi | last post by:
Hi, I get the following warning: "Possibly incorrect assignment to local 'oLockObject' which is the argument to a using or lock statement. The Dispose call or unlocking will happen on the original value of the local." My code is: using System; using System.Collections.Generic;
12
2689
by: Andrew Bullock | last post by:
Hi, I have two classes, A and B, B takes an A as an argument in its constructor: A a1 = new A(); B b = new B(a1);
7
3307
by: TS | last post by:
I was under the assumption that if you pass an object as a param to a method and inside that method this object is changed, the object will stay changed when returned from the method because the object is a reference type? my code is not proving that. I have a web project i created from a web service that is my object: public class ExcelService : SoapHttpClientProtocol {
8
1378
by: Sanders Kaufman | last post by:
I think I finally get the idea of passing by reference, but I'm struggling with the syntax and grammar of it. I've got an object and I want to send it to a function to have its properties modified like so: class MyClass { var $a; function MyClass(){
8
3506
by: S. | last post by:
Hi all, Can someone please help me with this? I have the following struct: typedef struct { char *name; int age; } Student;
0
9680
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
9528
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
1
10173
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
10006
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
7547
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
6788
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();...
0
5441
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5573
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
3
2925
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.