By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
446,142 Members | 1,890 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 446,142 IT Pros & Developers. It's quick & easy.

Passing by ref or by value

P: n/a
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
Nov 16 '05 #1
Share this Question
Share on Google+
9 Replies


P: n/a

" Just Me" <ne********@a-znet.com> wrote in message
news:ef*************@TK2MSFTNGP10.phx.gbl...
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.

--
Peter [MVP Visual Developer]
Jack of all trades, master of none.
Nov 16 '05 #2

P: n/a
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" <p_**********@toadstool.u> wrote in message
news:uo***************@TK2MSFTNGP11.phx.gbl...

" Just Me" <ne********@a-znet.com> wrote in message
news:ef*************@TK2MSFTNGP10.phx.gbl...
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.

--
Peter [MVP Visual Developer]
Jack of all trades, master of none.

Nov 16 '05 #3

P: n/a
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" <p_**********@toadstool.u> wrote in message
news:uo***************@TK2MSFTNGP11.phx.gbl...

" Just Me" <ne********@a-znet.com> wrote in message
news:ef*************@TK2MSFTNGP10.phx.gbl...
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.

--
Peter [MVP Visual Developer]
Jack of all trades, master of none.

Nov 16 '05 #4

P: n/a
Just Me <ne********@a-znet.com> wrote:
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.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 16 '05 #5

P: n/a
>
Passing a reference type by value means what?


See http://www.pobox.com/~skeet/csharp/parameters.html

Great site.
If I understand, passing a reference variable by value and changing it
changes the reference (the pointer value). Please correct if not so.
Thanks
Nov 16 '05 #6

P: n/a
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?
" Just Me" <ne********@a-znet.com> wrote in message
news:%2****************@TK2MSFTNGP09.phx.gbl...
Passing a reference type by value means what?


See http://www.pobox.com/~skeet/csharp/parameters.html

Great site.
If I understand, passing a reference variable by value and changing it
changes the reference (the pointer value). Please correct if not so.
Thanks

Nov 16 '05 #7

P: n/a
Just Me <ne********@a-znet.com> wrote:
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 :)

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 16 '05 #8

P: n/a
Thanks

"Jon Skeet [C# MVP]" <sk***@pobox.com> wrote in message
news:MP************************@msnews.microsoft.c om...
Just Me <ne********@a-znet.com> wrote:
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 :)

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too

Nov 16 '05 #9

P: n/a
Thanks

"Jon Skeet [C# MVP]" <sk***@pobox.com> wrote in message
news:MP************************@msnews.microsoft.c om...
Just Me <ne********@a-znet.com> wrote:
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 :)

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too

Nov 16 '05 #10

This discussion thread is closed

Replies have been disabled for this discussion.