471,618 Members | 1,285 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 471,618 software developers and data experts.

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


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
Feb 5 '06 #1
2 2231
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

Feb 5 '06 #2
"SpotNet" <Sp*****@msnews.grp> wrote in message news:eL**************@TK2MSFTNGP11.phx.gbl...

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


Feb 5 '06 #3

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

8 posts views Thread by Chris Mayers | last post: by
33 posts views Thread by C# Learner | last post: by
10 posts views Thread by Jon Davis | last post: by
1 post views Thread by Alex Chan | last post: by
9 posts views Thread by aczapran | last post: by
1 post views Thread by ZEDKYRIE | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.