Difference between [In, Out] and ref parameter usage....

S

SpotNet

Hello NewsGroup,

More out of curiosity, now I'd love to know which is a better way, if one
can think in such terms in this context. If I implement the ChooseColor API
function, the documentation states (briefly);

BOOL ChooseColor(LPCHOOSECOLOR lpcc);

lpcc is defined as "...[in, out] Pointer to a CHOOSECOLOR structure that..."
and you know or know where to read the rest of it.

I can interpret hence implement this in two ways (I know this may sound
trivial, I just want to know);

1) CHOOSECOLOR as a class;

private class CCHOOSECOLOR
{
internal Int32 lStructSize = 0;
... //And the rest of it
}

Hence the API call as;
[DllImport("Comdlg32.dll", EntryPoint = "ChooseColor")]
private static extern bool ChooseColor([In, Out] CCHOOSECOLOR lpcc);
internal static bool ChooseColorApi([In, Out] CCHOOSECOLOR lpcc)
{
return ChooseColor(lpcc);
}
---------------------------------------------------------------------------------
2) CHOOSECOLOR as a structure;

private struct CHOOSECOLOR
{
internal Int32 lStructSize;
... //And the rest of it
}

Hence the API call as;
[DllImport("Comdlg32.dll", EntryPoint = "ChooseColor")]
private static extern bool ChooseColor(ref CCHOOSECOLOR lpcc);
internal static bool ChooseColorApi(ref CCHOOSECOLOR lpcc)
{
return ChooseColor(ref lpcc);
}

Both work real well so to get pedantic on you all (sorry about that) would
there be any advantage or disadvantage on one way over the other? Hence
would the be a preferable way? The reason I ask the difference between [In,
Out] and ref is because if I use [In, Out] in the struct version nothing
works and I get errors vice versa as well. Also nothing (obviously) works if
[In, Out] or ref are left out.

Thanks NewGroup, this is no show stopper for me and I only ask out of
curiosity.

Regards,
SpotNet
 
L

Light

If it is a color options then I'd personally prefer to use a property.
It looks like it is taking a pointer to a color object so you should
probably work around the object/class concept. Perhaps adding 1 more
level of indirection would solve your task.
Curtis
http://www.ghostclip.com
The Premier Help System For Developers
 
B

Bill Butler

SpotNet said:
Hello NewsGroup,

More out of curiosity, now I'd love to know which is a better way, if one
can think in such terms in this context. If I implement the ChooseColor API
function, the documentation states (briefly);
<snip the actual interop>

Hi,

The guts of the call to the API should be totally hidden from the End user.
You can implement the icky part however you wish.
The methods exposed to Dotnet, however, should no nothing about the internal stuff.
Implement a ColorChooser class Wrapper (probably a static class).
Implement a Method like this

/* NOT TESTED */
public static Color PickColor() /// assumes using System.Drawing.
{
Color color = Color.Empty
..
//Your interop API stuff goes here
if (OK) // user pressed OK
color = ColorTranslator.FromWin32(cc.rgbResult); // cc is the CHOOSECOLOR struct
return (color);
}
The user simply needs to check if (color == Color.Empty) to see if a color was selected

Make it look like it was Part of Dotnet
Hope this helps
Bill
 

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