all groups > dotnet interop > march 2005 >
You're in the

dotnet interop

group:

Is there any way to hack around C#'s lack of support for optional arguments?


Is there any way to hack around C#'s lack of support for optional arguments? Manny Vellon
3/30/2005 9:36:10 AM
dotnet interop:
I am trying to implement a COM interface in my app that has a member
function with arguments (IDL form) that look like:

Foo( [in, string] LPCWSTR wPath,
[out, OPTIONAL] VARIANT *pdata,
[out, OPTIONAL] long *plFlags);

I can translate this, successfully, to:

Foo( string sPath, [out] object data, [ref] int flags)

but I have no way of testing whether the incoming "data" and "flags"
references are valid. If the caller passes in NULL, there seems to be no way
of testing this in C#. I can put a try/catch block around assignments to
these, but this is really slow in the NULL case.

Is it totally fruitless to try to deal with optional arguments? Arguably,
it's not even strictly an issue with arguments tagged as optional. I can
imagine an interface that omits the OPTIONAL keyword but simply documents
that NULL pointer values are acceptable. How can this condition (invalid
references) be tested in C#?

My apologies if this has been covered already. A search of the newsgroup
titles didn't show up any promising postings.


Re: Is there any way to hack around C#'s lack of support for optional arguments? Kuehn
3/30/2005 4:14:58 PM
You can overload the method as in:
Foo(sting sPath)
Foo(string sPath, VARIANT *pdata)
Foo(string sPath, long *plFlags)
Foo(string sPath, VARIANT *pdata, long *plFlags)

Have the first three methods call the last with null (or some predefined
value) passed in. The forth method will do your busy work. This will give
you control over what is passed in and there are no optional parameters.

Hope that helps.

Mike Kuehn



[quoted text, click to view]

Re: Is there any way to hack around C#'s lack of support for optional arguments? Alan Baljeu
3/30/2005 5:07:04 PM

[quoted text, click to view]

Can't you write this:
if (data == null)


Re: Is there any way to hack around C#'s lack of support for optional arguments? Manny Vellon
3/30/2005 6:55:56 PM
[quoted text, click to view]

No. In .net interop, what is (in c++):

void foo(int *pdata)

becomes

void foo(out pdata)

and pdata is _not_ a pointer. Within C#, there is no way to pass a bad
reference to foo, but when calling from C++ to C#, you could call foo(NULL)
and the C# code would gag.

When you test pdata==null, you are not testing a pointer like you do in C++;
you are testing the value. The value might very well be null before you
assign to it. The question is whether the reference is something valid that
you can assign to.



Re: Is there any way to hack around C#'s lack of support for optional arguments? Manny Vellon
3/31/2005 9:18:47 PM
I don't see how you can do this when implementing a COM interface. When your
code is called via such an interface, you can't implement overloaded
methods - the app is only aware of what's in the interface (for that matter,
that's true when you're called via a C# interface, too).

Additionally, remember, this is C# - your examples mention VARIANT's and
pointers - you're thinking C++. The VARIANT * becomes a "ref object" and the
"long *plFlags" becomes a "ref long". If I was doing this in C++ I could
simply check to see if pdata or plFlags was NULL.

[quoted text, click to view]

Re: Is there any way to hack around C#'s lack of support for optional arguments? EggHead
4/1/2005 5:32:39 PM
Do you try the "System.Reflection.Missing.Value"?
It is like the "IsMissing" in VB6.0 and "IsNothing" in VB.net.
Egghead
[quoted text, click to view]

Re: Is there any way to hack around C#'s lack of support for optional arguments? Mattias Sjögren
4/1/2005 11:17:49 PM
[quoted text, click to view]

The only way I can think of is to modify the interop assembly and
change the parameter types to IntPtr or int*. If you for example
change the plFlags type to int* (forcing you to use unsafe code in C#)
you can then write

if ( flags != null )
*flags = 123;



Mattias

--
Mattias Sjögren [MVP] mattias @ mvps.org
http://www.msjogren.net/dotnet/ | http://www.dotnetinterop.com
Re: Is there any way to hack around C#'s lack of support for optional arguments? Manny Vellon
4/2/2005 3:54:51 PM
This works fine for the int pointers, but there's also a VARIANT * in the
interface that requires some special handling. For the ints, there's some
functions in the Marshal class that lets you treat IntPtrs as memory
addresses and lets you write values into them without using unsafe code.

Thx.

[quoted text, click to view]

Re: Is there any way to hack around C#'s lack of support for optional arguments? Manny Vellon
4/4/2005 9:00:44 AM
Is there a "Set" analog? If the interface passes a VARIANT * and I translate
this to IntPtr (so that I can test the incoming pointer value), I'd need a
"set", but it's not as simple as calling the Marshal.Write's because I need
a CCW for the object I'm returning.

Another solution that I'm pursuing (as per some reading I've done) is to
declare the interfaces as arrays:

foo(VARIANT *po, int *pv)

becomes

foo(object[] po, int [] pv)

then I can test po and pv and return values via po[0]= and pv[0]=. This
should work although it looks weird.

[quoted text, click to view]

Re: Is there any way to hack around C#'s lack of support for optional arguments? Nick Hall
4/4/2005 1:05:50 PM
There's also a couple of functions for dealing with variants
(GetObjectForNativeVariant/GetNativeVariantForObject) which can be used to
similar effect.

Hope this helps,

Nick Hall

[quoted text, click to view]

Re: Is there any way to hack around C#'s lack of support for optional arguments? Nick Hall
4/5/2005 12:35:18 PM
I'd have thought that's where GetNativeVariantForObject would come in. From
the looks of the documentation it looks like it would be something like the
following: -

if (pData != IntPtr.Zero)
{
GetNativeVariantForObject(ObjectToStoreInVariant, pData);
}

In other words GetNativeVariantForObject does the work of creating the CCW
(if required) and writing to the supplied Variant.

Hope this helps,

Nick Hall



[quoted text, click to view]

Re: Is there any way to hack around C#'s lack of support for optional arguments? Manny Vellon
4/12/2005 4:58:54 PM
Cool - I'll have to experiment with this. At worst, I can imagine that it
would only do simple types (integers, strings, etc.), but it would be great
if it also handled the creation of a CCW!

[quoted text, click to view]
AddThis Social Bookmark Button