473,569 Members | 2,716 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

strange? by ref instead of by value

Hello,

as i belived c# will always transfer the variables as by vale if not marked
with a ref infront of the decleration.
By using the folowing code there seams to be (at least for me) a strange
behavior:

namespace xyz
{
public class Class1
{
public static double[] Real(double[] yValue, bool invers)
{

yValue = test(yValue);
return (yValue);
}

private static double[] test(double[] data)
{
data[1] = 11;
return (new double[] { 3, 4, 5 });
}
}
}
Main Programm:

using xyz;
....
double[] d = new double[] { 1, 0, 3, 0};
double[] d1 = new double[] { 1, 2, 3, 4};
d1 = Class1.Real(d, false);
....

If I watch the variables d and d1 by the debugger i get the folowing
results: d1 := { 3, 4, 5 } and d := { 1, 11, 3, 0}. I would not have
expexted, that the d[1] value would have been changed to 11, because the d
parameter wasn' t given by the ref. In my opinion the d value shouln't have
been changed at all?

Any ideas why this happens? Im using VS2005 SP1 and I think there are no
parameters in VS which can be changed to get this reaktion of the code?

regards Andi
May 28 '07 #1
4 1303
Andi,

When you pass yValue to test, the array reference is passed by value.
In test, you could not assign a new array to the data parameter, but you
could most definitely modify items in whatever the reference points to. By
value and by reference only refer to the parameter, not to whatever the
parameter might point to if it is a reference.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard. caspershouse.co m

"Andi Wolf" <wo**@web.dewro te in message news:f3******** **@online.de...
Hello,

as i belived c# will always transfer the variables as by vale if not
marked with a ref infront of the decleration.
By using the folowing code there seams to be (at least for me) a strange
behavior:

namespace xyz
{
public class Class1
{
public static double[] Real(double[] yValue, bool invers)
{

yValue = test(yValue);
return (yValue);
}

private static double[] test(double[] data)
{
data[1] = 11;
return (new double[] { 3, 4, 5 });
}
}
}
Main Programm:

using xyz;
...
double[] d = new double[] { 1, 0, 3, 0};
double[] d1 = new double[] { 1, 2, 3, 4};
d1 = Class1.Real(d, false);
...

If I watch the variables d and d1 by the debugger i get the folowing
results: d1 := { 3, 4, 5 } and d := { 1, 11, 3, 0}. I would not have
expexted, that the d[1] value would have been changed to 11, because the d
parameter wasn' t given by the ref. In my opinion the d value shouln't
have been changed at all?

Any ideas why this happens? Im using VS2005 SP1 and I think there are no
parameters in VS which can be changed to get this reaktion of the code?

regards Andi
May 28 '07 #2
Andi Wolf <wo**@web.dewro te:
as i belived c# will always transfer the variables as by vale if not marked
with a ref infront of the decleration.
Yes, it will. But for reference types, the value of the variable is a
reference, not the object itself. Arrays are reference types.

<snip>
If I watch the variables d and d1 by the debugger i get the folowing
results: d1 := { 3, 4, 5 } and d := { 1, 11, 3, 0}. I would not have
expexted, that the d[1] value would have been changed to 11, because the d
parameter wasn' t given by the ref. In my opinion the d value shouln't have
been changed at all?
The value of the d variable hasn't changed. That variable's value is
just a reference to an array. The *contents* of the array has
changed, but the reference is the same thing.

See http://pobox.com/~skeet/csharp/parameters.html for more details on
this.

--
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
May 28 '07 #3
so the only way to avoid, that d is changed is to copy the array in to a
other array ?

namespace xyz
{
public class Class1
{
public static double[] Real(double[] yValue, bool invers)
{
double[] x = new double[yValue.Length];
yValue.CopyTo(x ,0);
x = test(x);
return(x)
}
}
}

or is there a better way?

regards andi

"Andi Wolf" <wo**@web.desch rieb im Newsbeitrag
news:f3******** **@online.de...
Hello,

as i belived c# will always transfer the variables as by vale if not
marked with a ref infront of the decleration.
By using the folowing code there seams to be (at least for me) a strange
behavior:

namespace xyz
{
public class Class1
{
public static double[] Real(double[] yValue, bool invers)
{

yValue = test(yValue);
return (yValue);
}

private static double[] test(double[] data)
{
data[1] = 11;
return (new double[] { 3, 4, 5 });
}
}
}
Main Programm:

using xyz;
...
double[] d = new double[] { 1, 0, 3, 0};
double[] d1 = new double[] { 1, 2, 3, 4};
d1 = Class1.Real(d, false);
...

If I watch the variables d and d1 by the debugger i get the folowing
results: d1 := { 3, 4, 5 } and d := { 1, 11, 3, 0}. I would not have
expexted, that the d[1] value would have been changed to 11, because the d
parameter wasn' t given by the ref. In my opinion the d value shouln't
have been changed at all?

Any ideas why this happens? Im using VS2005 SP1 and I think there are no
parameters in VS which can be changed to get this reaktion of the code?

regards Andi

May 28 '07 #4
Andi Wolf wrote:
so the only way to avoid, that d is changed is to copy the array in to a
other array ?
The only way if you need to pass an array to the method.

Another way could be to create a wrapper class for the array that
implements something like ICollection or IEnumerable, and that only
exposes the values as read-only.

--
Göran Andersson
_____
http://www.guffa.com
May 28 '07 #5

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

Similar topics

4
5601
by: Ben | last post by:
Hi all, I'm trying to figure out how how complex map, filter and reduce work based on the following piece of code from http://www-106.ibm.com/developerworks/linux/library/l-prog.html : bigmuls = lambda xs,ys: filter(lambda (x,y):x*y > 25, combine(xs,ys)) combine = lambda xs,ys: map(None, xs*len(ys), dupelms(ys,len(xs))) dupelms = lambda...
11
1844
by: F. Da Costa | last post by:
Hi, This is a strange issue I'v been staring at for half a day now. It concerns catching keys via the onkeydown handler. In IE5+ it works fine but in Moz 1.6 (& Firebird 0.7+) it behaves most peculiar. The 'offensive' code is included below. The weird thing lies in the alert box preceded by // **. * function textboxReplaceSelect *
3
2335
by: Bruno van Dooren | last post by:
Hi All, i have some (3) different weird pointer problems that have me stumped. i suspect that the compiler behavior is correct because gcc shows the same results. ---------------------------------------------- //example 1: typedef int t_Array; int main(int argc, char* argv)
9
2188
by: Wescotte | last post by:
Here is a small sample program I wrote in PHP to help illustrates problem I'm having. The data base is using DB2 V5R3M0. The client is WinXP machine using the iSeries Client Access Driver ver 10.00.04.00 to connect to the database. The problem is that executing the exact same SQL select statement more than twice int a row stops produces...
3
2847
by: Jim Irvine | last post by:
I have a small database made up of 2 tables. I have a report that is driven from a query. On the report are a couple of fields linked to memo boxes in the table. Everytime I run the report I get the following strange characters instead of data. �_ Ἃ These actually show us as square boxes and capital A's with a tilde etc.
6
8508
by: leonecla | last post by:
Hi everybody, I'm facing a very very strange problem with a very very simple C program... My goal should be to write to a binary file some numbers (integers), each one represented as a sequence of 32 bit. I made this stupid trial code: --------------------------------------------- FILE *fout;
7
1718
by: | last post by:
hi, I have got XML tag <fo:block font-weight="bold" font-size="13pt"><!]></fo:block>. Problem is when i gives xmlURL from c# to InputStreamReader in J# code with the aspx web page as XML which contain tag. When i try to display output on PDF, it'll get the value from the tag <!]> some thing like a~,^ 845 strange charachers instead of Euro...
5
1672
by: cody | last post by:
I have a very funny/strange effect here. if I let the delegate do "return prop.GetGetMethod().Invoke(info.AudioHeader, null);" then I get wrong results, that is, a wrong method is called and I have no clue why. But if I store the MethodInfo in a local variable I works as expected. I do not understand why this is so, shouldn't both ways...
5
1054
by: John | last post by:
I have created a program that accesses a flat 2 dimensional array and have developed a routine to sum the four neighbors of a specific row/column position. Here is an example, this will sum all the values of row 1, column 1 (base 0) which is the number 5 int a = {1,2,3,4,5,6,7,8,9}; int* pa = &a; // row 1, column 0 (i.e. number 4)
3
2419
by: senfo | last post by:
I developed a Windows control in VS 2005 that inherits from the PictureBox Control that adds the ability to select images in a Windows application. It is, however, experiencing a strange issue that I can't explain. One of the desired affects was to provide the ability to change the background color on images that were selected, as well as...
0
8118
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...
1
7666
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...
0
7964
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...
0
6278
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
1
5504
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...
0
5217
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...
0
3636
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2107
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
1
1208
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.