Click here to Skip to main content
15,885,546 members
Home / Discussions / C#
   

C#

 
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 
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 
Hmm, I thought I had replied to this post, but it's not showing up. Here is the second attempt (good thing I had my answer saved).

Say I did this:

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

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


This will actually change the originalArray variable. It now points to the new array I created in the CheckArray method. Without the ref modifier, it would only change the local iArray variable.

A reference is a variable that points to some object in memory. The originalArray variable is a reference to an array held in memory somewhere. It allows me to perform operations on the array. However, operations on the originalArray variable itself only effect that variable. For example:

int[] arrayA = new int[10];
int[] arrayB = arrayA;  

// Both arrayA and arrayB reference the same array.

// An operation on the array via the arrayA reference variable.
// Does not affect arrayB.
arrayA[0] = 42;

// An operation on the arrayA reference variable itself.
arrayA = null;

// This will print 42 because the arrayB reference variable
// still points to the array.
Console.WriteLine(arrayB[0]);


When a reference variable is passed by value to a method, a copy of the reference variable itself is made, much like what I did above with the arrayB variable. You'll notice that when I nulled out the arrayA, it did not affect the arrayB variable. It's the same when you pass a reference by value to a method. operations on the reference variable itself only affect the local variable.

Now, the ref keyword gives me a reference to a reference, so to speak. If I modify a parameter with ref, and perform operations that change the variable itself, it also affects the reference variable that was originally passed to the method.

This takes awhile to wrap your mind around it, but keep at it and it will eventually make sense. Smile | :)
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 
QuestionDear Inheritance - A question for you Pin
Gulfraz Khan10-Nov-05 6:37
Gulfraz Khan10-Nov-05 6:37 
AnswerRe: Dear Inheritance - A question for you Pin
leppie10-Nov-05 7:02
leppie10-Nov-05 7:02 

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.