Passing by ref or by value

J

Just Me

PARAFORMAT2 is a structure that SendMessage will return stuff in.

Is the "ref" correct or since only a pointer is being passed should it be by
value?



Suppose I was passing data rather then receiving it,

would that change the answer to the above?



[DllImport("user32.dll",

EntryPoint="SendMessage",CharSet=CharSet.Auto )]

public static extern int SendMessage(IntPtr hwnd, int wMsg, int wParam,

ref PARAFORMAT2 pf2);





Thanks in advance
 
P

Peter van der Goes

Just Me said:
PARAFORMAT2 is a structure that SendMessage will return stuff in.

Is the "ref" correct or since only a pointer is being passed should it be by
value?



Suppose I was passing data rather then receiving it,

would that change the answer to the above?



[DllImport("user32.dll",

EntryPoint="SendMessage",CharSet=CharSet.Auto )]

public static extern int SendMessage(IntPtr hwnd, int wMsg, int wParam,

ref PARAFORMAT2 pf2);





Thanks in advance
I'll take a shot at this, and I'm sure others will explain it
better/further.

From the C# Programmer's Reference:

"A struct type is a value type..."

That means passing a struct by value passes a copy of the contents, and
disallows modification of the original data.
You stated that pf2 is in the parameter list to serve as a repository for
data returned by the SendMessage() method. To do that, ref is a necessary
part of the method definition to cause the address of pf2 to be passed to
SendMessage() and allowing SendMessage to alter the data at the original
location.
If you wanted to pass a struct to a method solely to supply needed
information to the method, with no intention to modify any data within the
struct, then passing by value (no ref) will work fine.
Caveat: Structs can be large, and you should consider the efficiency
tradeoffs associated with passing large blocks of data to your methods vs.
passing a hex memory address.
You might also want to check the help for the use of and differences between
the ref and out keywords.
 
J

Just Me

That tells me what I needed to know. Very clearly.

I was thinking that by ref might have meant the value of the pointer might
be changed.

Thanks


Peter van der Goes said:
Just Me said:
PARAFORMAT2 is a structure that SendMessage will return stuff in.

Is the "ref" correct or since only a pointer is being passed should it
be
by
value?



Suppose I was passing data rather then receiving it,

would that change the answer to the above?



[DllImport("user32.dll",

EntryPoint="SendMessage",CharSet=CharSet.Auto )]

public static extern int SendMessage(IntPtr hwnd, int wMsg, int wParam,

ref PARAFORMAT2 pf2);





Thanks in advance
I'll take a shot at this, and I'm sure others will explain it
better/further.

From the C# Programmer's Reference:

"A struct type is a value type..."

That means passing a struct by value passes a copy of the contents, and
disallows modification of the original data.
You stated that pf2 is in the parameter list to serve as a repository for
data returned by the SendMessage() method. To do that, ref is a necessary
part of the method definition to cause the address of pf2 to be passed to
SendMessage() and allowing SendMessage to alter the data at the original
location.
If you wanted to pass a struct to a method solely to supply needed
information to the method, with no intention to modify any data within the
struct, then passing by value (no ref) will work fine.
Caveat: Structs can be large, and you should consider the efficiency
tradeoffs associated with passing large blocks of data to your methods vs.
passing a hex memory address.
You might also want to check the help for the use of and differences between
the ref and out keywords.
 
J

Just Me

I think String is a reference type. I also think that I should therefore
pass it by value in which case a pointer is passed. Is that correct?

Passing a reference type by value means what?


Thanks again

PS before I wrote this I wanted to confirm that String was a reference type
but couldn't find that in the doc.


Peter van der Goes said:
Just Me said:
PARAFORMAT2 is a structure that SendMessage will return stuff in.

Is the "ref" correct or since only a pointer is being passed should it
be
by
value?



Suppose I was passing data rather then receiving it,

would that change the answer to the above?



[DllImport("user32.dll",

EntryPoint="SendMessage",CharSet=CharSet.Auto )]

public static extern int SendMessage(IntPtr hwnd, int wMsg, int wParam,

ref PARAFORMAT2 pf2);





Thanks in advance
I'll take a shot at this, and I'm sure others will explain it
better/further.

From the C# Programmer's Reference:

"A struct type is a value type..."

That means passing a struct by value passes a copy of the contents, and
disallows modification of the original data.
You stated that pf2 is in the parameter list to serve as a repository for
data returned by the SendMessage() method. To do that, ref is a necessary
part of the method definition to cause the address of pf2 to be passed to
SendMessage() and allowing SendMessage to alter the data at the original
location.
If you wanted to pass a struct to a method solely to supply needed
information to the method, with no intention to modify any data within the
struct, then passing by value (no ref) will work fine.
Caveat: Structs can be large, and you should consider the efficiency
tradeoffs associated with passing large blocks of data to your methods vs.
passing a hex memory address.
You might also want to check the help for the use of and differences between
the ref and out keywords.
 
J

Jon Skeet [C# MVP]

Just Me said:
I think String is a reference type.

Indeed it is.
I also think that I should therefore pass it by value in which case a
pointer is passed. Is that correct?
Yes.

Passing a reference type by value means what?

See http://www.pobox.com/~skeet/csharp/parameters.html
PS before I wrote this I wanted to confirm that String was a reference type
but couldn't find that in the doc.

Just look at the docs for the String type and you'll see it's a class,
not a struct.
 
J

Just Me

I didn't say what I meant.
I think a copy is made of the reference and the method might change the copy
but not the original value. Correct?
 
J

Jon Skeet [C# MVP]

Just Me said:
I didn't say what I meant.
I think a copy is made of the reference and the method might change the copy
but not the original value. Correct?

Exactly right :)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top