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.
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...
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
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.
>.
>
"lakshmi" <lmoo...@teledyne.com> wrote in message
news:007401c36bfe$7805ff30$a601...@phx.gbl...
>.
>
>-----Original Message-----
>
>.
>
COM Interop stinks :)
-c
"lakshmi" <lmoo...@teledyne.com> wrote in message
news:038e01c36bff$fa5eb890$a101...@phx.gbl...
You either have to pass dummy values or use overloading.
>.
>
Is there any reason why you can't write your dll in VB.NET ?
Optional parameters are available in that language.
Regards,
Fergus
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...
>.
>
|| 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
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...
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
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.
>.
>
"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
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...
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.
>.
>
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