Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

optional parameters - C#

13 views
Skip to first unread message

lakshmi M

unread,
Aug 26, 2003, 1:40:39 PM8/26/03
to
I found that optional parameters are not supported in C#.
We are currently rewriting a C++ dll in C#. The C++ dll
has methods that use optional parameters frequently.
The client is VB script that calls the methods in the
following manner.

method definition in C++
method(VARIANT v1, DATE time, [in, optional] BOOL b1,
[in,optional] short s1]

The VB Script client calls the method like this.

method(v1, time, ,50)

How do I support the above syntax from my .NET C# dll now?
I would appreaciate any help.

Frank Drebin

unread,
Aug 26, 2003, 2:04:54 PM8/26/03
to
It's called "overloading". You'd do something like this:

Method(int intHeight, int intWidth, bool blnShow, string strFileName, string
strLogFile)
{
// does something useful..
}
Method(int intHeight, int intWidth, bool blnShow, string strFileName)
{
Method(intHeight,intWidth,blnShow,strFileName,@"C:\Logfile.log");
}
Method(int intHeight, int intWidth, bool blnShow)
{
Method(intHeight,intWidth,blnShow,@"C:\thefile.txt",@"C:\Logfile.log");
}
Method(int intHeight, int intWidth)
{
Method(intHeight,intWidth, true,@"C:\thefile.txt",@"C:\Logfile.log");
}
Method()
{
Method(600,800, true,@"C:\thefile.txt",@"C:\Logfile.log");
}


If this makes absolutely no sense, paste this into a class.. then try to
call Method() - intellisense will show you that there are 5 overloads and
you can choose which "version" you want to use.. or more specifically,
depending on how many parameters you pass - will determine which version you
use..


"lakshmi M" <lmoo...@teledyne.com> wrote in message
news:098201c36bf9$2a666c10$a001...@phx.gbl...

Chad Myers

unread,
Aug 26, 2003, 2:07:56 PM8/26/03
to

"lakshmi M" <lmoo...@teledyne.com> wrote in message
news:098201c36bf9$2a666c10$a001...@phx.gbl...

Optional parameters were not included in C# for a number
of good design reasons.

The preferred approach is to use overloaded methods:

void method( object v1, DateTime time )
{
// default false for b1
method(v1, time, false);
}

void method( object v1, DateTime time, bool b1 )
{
// default 0 for s1
method(v1, time, b1, 0);
}

void method( object v1, DateTime time, bool b1, short s1 )
{
// Real work here
}

Also, I recommend you use more descriptive parameter
names for code documentation, but I assume that you
made the short names to make it easier for asking
the question.

-c


lakshmi

unread,
Aug 26, 2003, 2:18:37 PM8/26/03
to
Thanks Frank. It makes sense to me. Since C++ supported
optional parameters, our VB script clients
call your example Method like this assuming (bool blnShow,
string strFileName, string strLogFile) optional arguments
in C++.

Method(intHeight, intWidth, , , "log.txt")

The C++ code resolves the above call taking default values
for blnShow and strFileName.

I'm wondering how I can write my .NET dll backwards
compatible and still support the above call.
thanks a lot.

>.
>

Frank Drebin

unread,
Aug 26, 2003, 2:24:56 PM8/26/03
to
Hmm... you *should* be able to do that (so long as those empty parameters
don't break something inside your method)? If not, they might need to pass
like a vbNull for this parameters?


"lakshmi" <lmoo...@teledyne.com> wrote in message
news:007401c36bfe$7805ff30$a601...@phx.gbl...

lakshmi

unread,
Aug 26, 2003, 2:29:22 PM8/26/03
to
Thanks. My real problem is:
The client is NOT passing anything for the first few
optional arguments, but passing a value for the next few
optional arguments.
C++ optional arguments took care of ,,,syntax. Can I write
my C# dll to support this syntax?

>.
>

lakshmi

unread,
Aug 26, 2003, 2:29:25 PM8/26/03
to
Thanks. My real problem is:
The client is NOT passing anything for the first few
optional arguments, but passing a value for the next few
optional arguments.
C++ optional arguments took care of ,,,syntax. Can I write
my C# dll to support this syntax?

>-----Original Message-----
>

>.
>

lakshmi

unread,
Aug 26, 2003, 2:40:28 PM8/26/03
to
It doesn't seem like C# will accept empty parameters.
>.
>

Chad Myers

unread,
Aug 26, 2003, 2:55:27 PM8/26/03
to
Well, since you're dealing with an interop
environment, you may have to just accept object
for every parameter and then some of them
will either be null, or some type of COM
Interop version of VARTIANT with a variant
value of null (vbNull?).

COM Interop stinks :)

-c

"lakshmi" <lmoo...@teledyne.com> wrote in message
news:038e01c36bff$fa5eb890$a101...@phx.gbl...

MikeO

unread,
Aug 26, 2003, 3:11:42 PM8/26/03
to
It sounds like you don't want to have any client code
rewritten which isn't going to happen :(

You either have to pass dummy values or use overloading.

>.
>

Fergus Cooney

unread,
Aug 26, 2003, 3:12:26 PM8/26/03
to
Hi Lakshmi,

Is there any reason why you can't write your dll in VB.NET ?
Optional parameters are available in that language.

Regards,
Fergus


lakshmi

unread,
Aug 26, 2003, 3:20:26 PM8/26/03
to
Yeah, rewriting the client code is not an option.
overloading probably won't take care of our problem.
>.
>

lakshmi

unread,
Aug 26, 2003, 3:29:27 PM8/26/03
to
yes, if I can't find a workaround in C#, VB.NET would be
my only option. thx.
>.
>

Chad Myers

unread,
Aug 26, 2003, 3:34:57 PM8/26/03
to
VB.NET only supports optional parameters at the end,
though, so your example of a foo(,,val1,val2) syntax
wouldn't work.

My guess is, it'll try to pass nulls or default
values for the parameters. I bet it would do the
same thing for C#. It'll pass false for bool,
0 for int, DateTime.MinValue for DateTime, and
null for reference types.

-c

"lakshmi" <l...@nospam.net> wrote in message
news:0af701c36c08$5ce84550$a001...@phx.gbl...

lakshmi

unread,
Aug 26, 2003, 3:52:14 PM8/26/03
to
there ought to be a way of supplying default values right?
at least in VB 6.0, we could supply default values in the
function prototype itself
Method(int i, int j, optional bool k = false, optional int
l = 1)
I'm hoping that VB.NET would be supporting this syntax.
In C#, I couldn't supply default values in this manner.
thx.

>.
>

Fergus Cooney

unread,
Aug 26, 2003, 3:54:56 PM8/26/03
to
Hi Chad,

|| VB.NET only supports optional parameters at the end,

Well, spotted - Lakshmi has methods with optional central
parameters - darn it!

Here's a possible workaround - I warn you, though, it's horrible.
:-)

Write a script interface (wrapper) which supplies the missing
parameters to the dll. This will minimise the changes to your client's
script - they'll only need a way of including/linking/whatever.

Regards,
Fergus


Chad Myers

unread,
Aug 26, 2003, 4:00:26 PM8/26/03
to
Yes, you can supply default values in VB.NET.
You can let VB.NET supply them (standard) or
customize them.

The only problem is, like I said, optional
parameters must be at the end and you can only
supply defaults for optional parameters.

So your method(,,"foo", bar) wouldn't work.

-c

"lakshmi" <l...@nospam.net> wrote in message

news:047e01c36c0b$8bf53990$a101...@phx.gbl...

Fergus Cooney

unread,
Aug 26, 2003, 4:07:07 PM8/26/03
to
Hi Lakshmi, Chad,

I've just reread the VB blurb on Optional Paarameters.

| You can specify that a procedure argument is optional and does
not have to be supplied when the
| procedure is called. Optional arguments are indicated by the
Optional keyword in the procedure
| definition.
| The following rules apply:
| Every optional argument in the procedure definition must specify
a default value.
| The default value for an optional argument must be a constant
expression.
|
| Every argument following an optional argument in the procedure
definition must also be optional.

Check out that last line. Any number of optional parameters - as long
as the last one is optional too.

Regards,
Fergus


lakshmi

unread,
Aug 26, 2003, 4:24:24 PM8/26/03
to
Hi guys,
just for your clarification, all my OPTIONAL parameters
are at the end of the parameter list. There is no optional
parameters in the middle of the list.

In the examples I showed you earlier, I had to pass my
arguments like

Method(arg1, arg2,,,arg5,arg6)
in this example arg3, arg4, arg5, arg6 are all optional
parameters. I was not passing arg3 and arg4 (in which case
my C++ code would work with some defaults for those
arguments or just not look at those arguments at all.)
I would pass arg5 and arg6 which would be values other
than the default values.
thx.

>.
>

Daniel Pratt

unread,
Aug 26, 2003, 4:53:00 PM8/26/03
to
Hi Lakshmi,

"lakshmi M" <lmoo...@teledyne.com> wrote in message
news:098201c36bf9$2a666c10$a001...@phx.gbl...

> I found that optional parameters are not supported in C#.
> We are currently rewriting a C++ dll in C#. The C++ dll
> has methods that use optional parameters frequently.
> The client is VB script that calls the methods in the
> following manner.
>
> method definition in C++
> method(VARIANT v1, DATE time, [in, optional] BOOL b1,
> [in,optional] short s1]
>
> The VB Script client calls the method like this.
>
> method(v1, time, ,50)

If you think about the fact that optional parameters are a bit of a
compiler trick, this problem may become a little easier. The VB client
*does* pass a value for every parameter of a method, whether you tell it to
or not. The question is: What value?

If the method in the .dll defines a value for the optional parameter,
that value will be hard-coded into the client for each method call that does
not specify a value for the parameter. To make this point clear, if you
compile a client against a .dll with an optional parameter and then change
the default value of the parameter and recompile the .dll, the client will
still pass the "old" default value until you recompile it.

If the method in the .dll *does not* define a value for the optional
parameter, then VB "picks" a value depending on the parameter type:

type=String; value=""
type=Short, Integer, Long, Single, Double, Date, etc. value = 0
type=Boolean; value=false;
type=Variant; value=VT_MISSING;

All but the last case is straightforward in C#. When you call an ActiveX
function from C#, optional parameters can be specified as
System.Reflection.Missing.Value. I would guess the reverse is true (i.e.
VT_MISSING ~= System.Reflection.Missing.Value).

Regards,
Dan


lakshmi

unread,
Aug 26, 2003, 5:24:23 PM8/26/03
to
thanks. that helped me understand better about optional
parameters.
I understand that I can pass
System.Reflection.Missing.Value if I were to pass an
optional parameter in C#.
But, can C# understand empty parameters
reqdParam1, reqdParam2,,,optionalParam3, optionalParam4.
Is there anyway I can code in C# to say it is receiving
VT_Missing?
>.
>

Chad Myers

unread,
Aug 26, 2003, 5:29:42 PM8/26/03
to
Like I said earlier, if you expect 5 parameters,
a few of which may be missing, define your
parameters as "object" and then check for
null or MissingValue, then supply default
values or cast the real values.

Other than that, the strong-typed nature
of C# will not permit you to pass VT_MISSING
to an int, for example.

-c

"lakshmi" <l...@nospam.net> wrote in message

news:0c5901c36c18$6ba869c0$a001...@phx.gbl...

lakshmi

unread,
Aug 27, 2003, 5:33:19 PM8/27/03
to
include the following lines of code:

using System.Runtime.InteropServices;
prefix the optional argument with [OptionalAttribute]

This takes care if an optional parameter is not supplied
by a VB script client.

>.
>

Fergus Cooney

unread,
Aug 27, 2003, 5:52:02 PM8/27/03
to
Hi Lakshmi,

When you've finally finished bashing your brains out trying to get
C# to do something it can't, remember that VB can <already do> what
you want.

Regards,
Fergus


0 new messages