Click here to Skip to main content
15,915,777 members
Home / Discussions / C#
   

C#

 
AnswerRe: TreeView question. Pin
Robert Rohde24-Nov-05 4:39
Robert Rohde24-Nov-05 4:39 
QuestionMethod parameters Pin
1nsp1r3d24-Nov-05 3:15
1nsp1r3d24-Nov-05 3:15 
AnswerRe: Method parameters Pin
Leslie Sanford24-Nov-05 3:40
Leslie Sanford24-Nov-05 3:40 
GeneralRe: Method parameters Pin
Colin Angus Mackay24-Nov-05 4:15
Colin Angus Mackay24-Nov-05 4:15 
AnswerRe: Method parameters Pin
Colin Angus Mackay24-Nov-05 4:25
Colin Angus Mackay24-Nov-05 4:25 
GeneralRe: Method parameters Pin
Leslie Sanford24-Nov-05 4:46
Leslie Sanford24-Nov-05 4:46 
GeneralRe: Method parameters Pin
Colin Angus Mackay24-Nov-05 4:58
Colin Angus Mackay24-Nov-05 4:58 
GeneralRe: Method parameters Pin
Leslie Sanford24-Nov-05 5:34
Leslie Sanford24-Nov-05 5:34 
Colin Angus Mackay wrote:
Anyway, everything's sorted out now.... I hope....


It's kinda tricky to wrap your head around. I started out with C years ago where things were on a much lower level than in C#. So you would deal with pointers directly instead of the higher level references C# gives you.

I can remember years ago doing something like this (in C):

void SomeMethod(int *pointer)
{
    /* Allocate memory */
    pointer = malloc(42);
}


And calling this function elsewhere:

int *p;

SomeMethod(p);

/* Oops!!! The variable p doesn't point to anything! */
p[0] = 0;


And wondering why memory wasn't being assigned to the variable passed into SomeMethod.

I posted to comp.lang.c, not for the faint of heart, and was told rather sternly that the reason the variable being passed to the function wasn't being updated is that it was being passed by value. Think of the pointer has having a value, and that value is the address of the memory it points to. This value is what is passed to the function and is stored in the parameter variable. If I change the parameter variable itself, such as assigning memory to it, it's not reflected outside of the function.

That's when I learned about the wonderful "pointers to pointers." So what I needed to do (and my C is really rusty, so pardon me if this isn't exactly correct), is:

void SomeMethod(int **pointer)
{
    /* Allocate memory */
    *pointer = malloc(42);
}


And calling this function elsewhere:

int *p;

SomeMethod(&p);

/* Yay! */
p[0] = 0;


Now, I'm not sure how things are under the covers in C#, so only take my C example as an analogy of how things might work in C#. But the concepts are the same.

The reference variables reference, or point, to an object somewhere. When a reference variable is passed to a method, its value is passed to the method and stored in the parameter variable. So the parameter variable references the same object; it has the same value. Operations, such as setting properties, calling methods, etc., performed on on the parameter variable local to the method are performed on the same object referenced by the variable passed to the method.

Pointers to pointers are the equivalent of using the ref modifier in C#. ref gives you a reference to a reference so that operations performed on the parameter variable are reflected outside of the method. Think of a ref variable as a reference to the variable itself that was passed to the method.

Well, hopefully some of that makes sense. Wink | ;)
GeneralRe: Method parameters Pin
Colin Angus Mackay24-Nov-05 5:44
Colin Angus Mackay24-Nov-05 5:44 
AnswerRe: Method parameters Pin
Colin Angus Mackay24-Nov-05 5:02
Colin Angus Mackay24-Nov-05 5:02 
QuestionData types in reports (rdls) Pin
doph24-Nov-05 1:50
doph24-Nov-05 1:50 
QuestionConverting decimal to String Pin
Subrahmanyam K24-Nov-05 1:37
Subrahmanyam K24-Nov-05 1:37 
AnswerRe: Converting decimal to String Pin
J4amieC24-Nov-05 2:02
J4amieC24-Nov-05 2:02 
AnswerRe: Converting decimal to String Pin
Craig G Fraser24-Nov-05 2:04
Craig G Fraser24-Nov-05 2:04 
QuestionRegarding Strong name Pin
A.Grover24-Nov-05 1:23
A.Grover24-Nov-05 1:23 
AnswerRe: Regarding Strong name Pin
S. Senthil Kumar24-Nov-05 2:19
S. Senthil Kumar24-Nov-05 2:19 
GeneralRe: Regarding Strong name Pin
A.Grover24-Nov-05 23:14
A.Grover24-Nov-05 23:14 
Questionclient/server communication Pin
batmanAgen24-Nov-05 1:08
batmanAgen24-Nov-05 1:08 
QuestionGenetating XML Pin
Gktony24-Nov-05 0:56
Gktony24-Nov-05 0:56 
AnswerRe: Genetating XML Pin
J4amieC24-Nov-05 1:33
J4amieC24-Nov-05 1:33 
GeneralRe: Genetating XML Pin
Gktony24-Nov-05 3:06
Gktony24-Nov-05 3:06 
GeneralRe: Genetating XML Pin
J4amieC24-Nov-05 4:32
J4amieC24-Nov-05 4:32 
QuestionHow to make a Line Graph Pin
Emyat24-Nov-05 0:24
Emyat24-Nov-05 0:24 
QuestionDesign Issues Pin
Billy Whizz23-Nov-05 23:55
Billy Whizz23-Nov-05 23:55 
QuestionWinWordControl Pin
pramod.21c23-Nov-05 23:49
pramod.21c23-Nov-05 23:49 

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.