Re: Passing by ref or by value

Tech-Archive recommends: Repair Windows Errors & Optimize Windows Performance

From: Peter van der Goes (p_vandergoes_at_toadstool.u)
Date: 07/20/04


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.


Relevant Pages

  • Re: Passing by ref or by value
    ... I was thinking that by ref might have meant the value of the pointer might ... >> Suppose I was passing data rather then receiving it, ... > data returned by the SendMessage() method. ... > If you wanted to pass a struct to a method solely to supply needed ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: call by address vs. call by value
    ... > reference (or pointer)? ... he's talking about the total number of memory references including ... For every access to a member of the struct passed by a pointer, ... exceed the time required to copy the struct for passing by value. ...
    (comp.lang.c)
  • Re: Hi, I wan to build a dtfm detector in c++/qt
    ... pointer to the class instance as its first parameter. ... ANSI C allows passing a struct by value. ... Java variables are Object references. ...
    (comp.dsp)
  • Re: passing char arrays by reference
    ... Et voila - not just one, but two arrays being passed by value. ... Yes, yes, very good - except that what you're really passing by ... are being passed as much by value as the struct itself. ... I, your host for tonight's annual Mental Inflexibility Awards 2008, am in ...
    (comp.lang.c)
  • [PATCH 1/3] ath9k: Handle -ENOMEM on RX gracefully
    ... We would get an oops on RX on -ENOMEM by passing ... NULL to the hardware on ath_rx_buf_link. ... skb we will put in our RX tail directly on the ath_rx_tasklet ... struct ath_recv_status *status, ...
    (Linux-Kernel)