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 4 1299
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.com
"Andi Wolf" <wo**@web.dewrote 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
Andi Wolf <wo**@web.dewrote:
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.com> http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
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.deschrieb 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
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 This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
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 :
...
|
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...
|
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.
...
|
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...
|
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...
| |
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...
|
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...
|
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...
|
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...
|
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...
|
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,...
| |
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...
|
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...
|
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: 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...
|
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...
|
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...
| |
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 ...
|
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...
| |