Click here to Skip to main content
15,867,308 members
Home / Discussions / C#
   

C#

 
GeneralRe: A similar action to a try catch statement? Pin
Leslie Sanford13-Nov-05 17:37
Leslie Sanford13-Nov-05 17:37 
GeneralRe: A similar action to a try catch statement? Pin
Kuira14-Nov-05 11:53
Kuira14-Nov-05 11:53 
QuestionParsing an XML string Pin
Ravi Bhavnani10-Nov-05 11:49
professionalRavi Bhavnani10-Nov-05 11:49 
AnswerRe: Parsing an XML string Pin
Ravi Bhavnani10-Nov-05 12:09
professionalRavi Bhavnani10-Nov-05 12:09 
Question.net 2.0 Membership in Windows Applications? Pin
MightyJoe10-Nov-05 11:11
MightyJoe10-Nov-05 11:11 
QuestionDrag Drop within a Control Pin
miah alom10-Nov-05 10:41
miah alom10-Nov-05 10:41 
QuestionByValue parameter is not used Pin
Seraphin10-Nov-05 10:08
Seraphin10-Nov-05 10:08 
AnswerRe: ByValue parameter is not used Pin
Leslie Sanford10-Nov-05 11:02
Leslie Sanford10-Nov-05 11:02 
Seraphin wrote:
Hello, I want to call a method which accepts an array. I've defined an array (originalArray) and want work with it within the method CheckArray. But if I change something within the array inside the method, the original array will be modified too. I thought the parameters are ByValue automatically? Isn't it?


Yes. But...

private void button4_Click(object sender, System.EventArgs e)
{
    CheckArray(originalArray);
}

private void CheckArray(int[,] iArray)
{
    int[,] copyArray = iArray;
    copyArray[0,0] = 0;
} 


You've not copied the array, you've only created another reference to the same array. When you assign a value to the array using the copied reference, the original array is affected, too. iArray and copyArray both reference the same array.

Remember, iArray is just a reference to an array held somewhere in memory. References are just variables. The reference is passed by value so that if you did something like this:

private void button4_Click(object sender, System.EventArgs e)
{
    CheckArray(originalArray);
}

private void CheckArray(int[,] iArray)
{
    iArray = new int[10, 10];
    iArray[0, 0] = 0;
} 


The originalArray will not be affected. You've only changed the iArray variable, which has local scope within the method.

To copy the array, you have to create a new array, not just a new reference, and copy the contents of the old array into the new array. Unfortunately, the CopyTo method only works for one-dimensional arrays, so you may have to write code that does the copying by hand. Or maybe someone else knows of a better way.
GeneralRe: ByValue parameter is not used Pin
Seraphin10-Nov-05 13:14
Seraphin10-Nov-05 13:14 
GeneralRe: ByValue parameter is not used Pin
Leslie Sanford10-Nov-05 16:44
Leslie Sanford10-Nov-05 16:44 
QuestionOkay, i gotta question - who wants to look? Pin
Anthony Mushrow10-Nov-05 9:54
professionalAnthony Mushrow10-Nov-05 9:54 
AnswerRe: Okay, i gotta question - who wants to look? Pin
Christian Graus10-Nov-05 13:35
protectorChristian Graus10-Nov-05 13:35 
QuestionPackage load error in VS2005? Pin
TheGreatAndPowerfulOz10-Nov-05 8:38
TheGreatAndPowerfulOz10-Nov-05 8:38 
QuestionLabel question Pin
Stanciu Vlad10-Nov-05 7:52
Stanciu Vlad10-Nov-05 7:52 
AnswerRe: Label question Pin
Anthony Mushrow10-Nov-05 8:01
professionalAnthony Mushrow10-Nov-05 8:01 
GeneralRe: Label question Pin
Stanciu Vlad10-Nov-05 8:35
Stanciu Vlad10-Nov-05 8:35 
AnswerRe: Label question Pin
Stanciu Vlad10-Nov-05 9:08
Stanciu Vlad10-Nov-05 9:08 
GeneralRe: Label question Pin
Anthony Mushrow10-Nov-05 9:27
professionalAnthony Mushrow10-Nov-05 9:27 
GeneralRe: Label question Pin
Dan Neely10-Nov-05 10:10
Dan Neely10-Nov-05 10:10 
QuestionExporting to Excel takes a very long time Pin
zaboboa10-Nov-05 7:33
zaboboa10-Nov-05 7:33 
AnswerRe: Exporting to Excel takes a very long time Pin
KaptinKrunch10-Nov-05 8:49
KaptinKrunch10-Nov-05 8:49 
GeneralRe: Exporting to Excel takes a very long time Pin
zaboboa10-Nov-05 9:02
zaboboa10-Nov-05 9:02 
QuestionC# Book for a Course Pin
1nsp1r3d10-Nov-05 7:10
1nsp1r3d10-Nov-05 7:10 
Questiongroup box control Pin
Manu_8110-Nov-05 7:03
Manu_8110-Nov-05 7:03 
AnswerRe: group box control Pin
Dan Neely10-Nov-05 7:07
Dan Neely10-Nov-05 7:07 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.