Re: Passing by ref or by value
From: Peter van der Goes (p_vandergoes_at_toadstool.u)
Date: 07/20/04
- Next message: Hans Kesting: "Re: Performance question..."
- Previous message: KavvY: "DropDownList SelectedIndex"
- In reply to: Just Me: "Passing by ref or by value"
- Next in thread: Just Me: "Re: Passing by ref or by value"
- Reply: Just Me: "Re: Passing by ref or by value"
- Reply: Just Me: "Re: Passing by ref or by value"
- Messages sorted by: [ date ] [ thread ]
Date: Tue, 20 Jul 2004 08:22:39 -0500
" Just Me" <newsgroups@a-znet.com> wrote in message
news:efirDflbEHA.384@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.
- Next message: Hans Kesting: "Re: Performance question..."
- Previous message: KavvY: "DropDownList SelectedIndex"
- In reply to: Just Me: "Passing by ref or by value"
- Next in thread: Just Me: "Re: Passing by ref or by value"
- Reply: Just Me: "Re: Passing by ref or by value"
- Reply: Just Me: "Re: Passing by ref or by value"
- Messages sorted by: [ date ] [ thread ]
Relevant Pages
|