.NETGURU
Optional parameters of a COM component in C# and VB.NET
Messages   Related Types
This message was discovered on ASPFriends.com 'aspngbeta' list.


=?iso-8859-1?Q?Lu=EDs_Santo-Tom=E1s_Gutierrez?=
I supposed that I'm doing something wrong so it can be so bad

If you instance and object that has optional parameter you have to introduce
all parameters in C# (it's a nightmare)
In VB.NET it isn't necessary but you loose the tags for documentation and so
on ...

An example:

ADODB.Connection m_dbConn=new ADODB.Connection();
m_dbConn.Open(ConnectionString,"","",-1);

I just want to pass the ConnectionString. I don't want to pass the four
parameters
And if the parameters are byref I have to declare a variable.

Work with ADO is a nightmare with C#. Where is my mistake?
In VB.NET it isn't necessary, why?

Thanks in advance,
Luis Santo Tomas

Reply to this message...
 
    
Jon Strauss
Shouldn't you be using ADOConnection or SQLConnection in .NET? Both of
their constructors are overloaded and have several different parameter
options.

Jon

"Luís Santo-Tomás Gutierrez" <Click here to reveal e-mail address> wrote in message
news:387724@aspngbeta...
[Original message clipped]

Reply to this message...
 
    
Jon Strauss
Sorry Luis, I think I misunderstood your first post. I don't believe that
optional parameters are available in C#, which may be the problem. Not sure
though. Maybe someone else has a better answer.

"Luis Santo-Tomas Gutierrez" <Click here to reveal e-mail address> wrote in message
news:388822@aspngbeta...
[Original message clipped]

Reply to this message...
 
    
Martin Smith
Can't you just pass in null for the optional params?

Although i don't know how this would work for optional value types (and enums).

m

--- Luis Santo-Tomas Gutierrez <Click here to reveal e-mail address> wrote:
[Original message clipped]

__________________________________________________
Do You Yahoo!?
Yahoo! Auctions - buy the things you want at great prices
http://auctions.yahoo.com/

Reply to this message...
 
    
Rob Gray
pass by reference works by passing the address of the variable into the method (like C++ pointers). If you pass 'null' as a parameter, the method has no place to store the data. Unfortunately you need to create variables for each of the optional parameters, then pass then in using the 'ref' keyword.

--------------------------------
From: Rob Gray
Reply to this message...
 
    
Phl Wigglesworth
There are two seperate issues involved here:

* Interoperability between VB & C#

* The need for named optional parameters in a language

For interoperability, many COM objects do not have large numbers of optional parameters - unfortunately, Microsoft uses them extensively - and from C# they become compulsary. For realistic use of most MS Word functions you will need to write sensible wrappers supplying all those unwanted parameters (ouch!). However, because they are optional, you can use the following code:

// Create your object
Word.Application w = new Word.Application();

// Create a dummy object that is used in place of all those unwanted parameters
object o = null;

// finally make your call
Word._Document d = w.Documents.AddOld(ref o,ref o);

// of course, some functions just get silly
w.Run("MACRO",ref o,ref o,ref o,ref o,ref o,ref o,ref o,ref o,ref o,ref o,ref o,ref o,ref o,ref o,ref o,ref o,ref o,ref o,ref o,ref o,ref o,ref o,ref o,ref o,ref o,ref o,ref o,ref o,ref o,ref o);

But before you jump in about the in efficiency of this code, just note what VB does BEHIND THE SCENES:

object o1 = new object()
object o2 = new object()
...
object 30 = new object()
Run("MACRO",o1,o2,...,o30)

Certainly, the VB code is much nicer to write, but add a few wrappers & C# will be fine.

As to the need for named optional parameters:

Visual Basic implements named optional parameters which are not available in most other languages and are not available in C#. The effective functionality of this is delivered must more cleanly using Classes to encapsulate the call. You create an object and fill in those non-default fields that you require - finally call one or more methods for the actual calls. Taken together with the posibility of overloaded functions there is very little difference in coding required but with a lot more flexibility:

e.g.

MyFunc f = new MyFunc();
f.Param5 = "";
f.Param25 = '";
f.Call();

or

new MyFunc(X,Y);

or

MyFunc f= new MyFunc();
f.Param398 = "xyx";
f.Call(X,Y);

These are C++ approaches and work fine within the language.

Finally, don't miss that C# has unnamed optional parameters which work well for the WriteLine("String",a,b,c) environment.

Have fun,

Phil Wigglesworth
Mythlandia, Inc
Reply to this message...
 
    
Stuart C. Salsbury
I think the tables have turned on my language preferences. I used to write
VB code that wrapped Win32 api ugliness. Now I'm going to have to write C#
code that wraps COM ugliness. I still call it progress, with a grin. The
King is Dead!!! Long Live the King!!! The language that Microsoft provides
for will be the language that I use. So far, C# is winning (some
highlights: <summary/> documentation element, boxing, code-compactness,
statement terminator that is not CRLF.

Phil, thanks for sharing your technique.

Regards,
Stuart Salsbury
Ernst & Young LLP

-----Original Message-----
From: Phl Wigglesworth [mailto:Click here to reveal e-mail address]
Sent: Wednesday, May 16, 2001 7:24 PM
To: aspngbeta
Subject: [aspngbeta] Re: Optional parameters of a COM component in C# and
VB.NET

There are two seperate issues involved here:
* Interoperability between VB & C#
* The need for named optional parameters in a language
For interoperability, many COM objects do not have large numbers of optional
parameters - unfortunately, Microsoft uses them extensively - and from C#
they become compulsary. For realistic use of most MS Word functions you
will need to write sensible wrappers supplying all those unwanted parameters
(ouch!). However, because they are optional, you can use the following
code:
// Create your object
Word.Application w &# n;ew Word.Application();
// Create a dummy object that is used in place of all those unwanted
parameters
object o &# n;ull;
// finally make your call
Word._Document d &# w;.Documents.AddOld(ref o,ref o);
// of course, some functions just get silly
w.Run("MACRO",ref o,ref o,ref o,ref o,ref o,ref o,ref o,ref o,ref o,ref
o,ref o,ref o,ref o,ref o,ref o,ref o,ref o,ref o,ref o,ref o,ref o,ref
o,ref o,ref o,ref o,ref o,ref o,ref o,ref o,ref o);
But before you jump in about the in efficiency of this code, just note what
VB does BEHIND THE SCENES:
object o1 &# n;ew object()
object o2 &# n;ew object()
...
object 30 &# n;ew object()
Run("MACRO",o1,o2,...,o30)
Certainly, the VB code is much nicer to write, but add a few wrappers & C#
will be fine.
As to the need for named optional parameters:
Visual Basic implements named optional parameters which are not available in
most other languages and are not available in C#. The effective
functionality of this is delivered must more cleanly using Classes to
encapsulate the call. You create an object and fill in those non-default
fields that you require - finally call one or more methods for the actual
calls. Taken together with the posibility of overloaded functions there is
very little difference in coding required but with a lot more flexibility:
e.g.
MyFunc f &# n;ew MyFunc();
f.Param5 &# ";";
f.Param25 &# ';";
f.Call();
or
new MyFunc(X,Y);
or
MyFunc f&# n;ew MyFunc();
f.Param398 &# ";xyx";
f.Call(X,Y);
These are C++ approaches and work fine within the language.
Finally, don't miss that C# has unnamed optional parameters which work well
for the WriteLine("String",a,b,c) environment.
Have fun,
Phil Wigglesworth
Mythlandia, Inc
Reply to this message...
 
    
Scott Swigart
kmsurvey artifactsThe problem is that Interop is going to be critical for companies that are migrating from DNA to .NET. Companies just have too much invested to migrate everything en-masse. Also, not everything is in the CLR. If you want to do office automation, for example, you have to talk to COM objects.

I keep praying that C# will support optional arguments, or default arguments (since it's really the same thing. an optional argument is essentially a default argument that defaults to 0, "" or null), but I have my doubts that this will emerge in version 1.

Scott Swigart
3 Leaf Solutions, LLC
Click here to reveal e-mail address
www.3leafsolutions.com

"Poliashenko, Max" <Click here to reveal e-mail address> wrote in message news:390576@aspngbeta...
I think, you are a little too negative towards C#.
It think, in some previous messages, people have pointed out that VB does some tricks behind the scene (i.e., essentially, creates these dummy parameters for you) to accomplish optional parameters feature since most of the languages read method parameters from the stack by their position. It is pretty kludgy. I think, the reason why optional parameters were invented in VB (and I don't know any other language that would support them) was the fact that VB could not support method overloading. Now, as VB.NET does, you can try it to see if you would like it better.
Essentially, C# takes that route and allows you to create as many overloaded method versions as you need to vary number of parameters for your method. The benefit is: it is clean and straight forward, it does not require any trickery on part of the compiler, it is supported by intellisense. In addition, it has a concept of variable number of parameters using an object array, if you really don't know the number and type of your parameters.
One feature that I miss from C++, though, is default argument values, although, we can do the same thing with method overloading, again.
-----Original Message-----
From: Lu&#ED;s Santo-Tom&#E1;s Gutierrez [mailto:Click here to reveal e-mail address]
Sent: Thursday, May 17, 2001 5:41 AM
To: aspngbeta
Subject: [aspngbeta] Re: Optional parameters of a COM component in C# and VB.NET

Oh my God!

Yes like a say before an option is, use dummy objects for optional parameters, but this approach is a bad approach the best that I have found but a bad bad approach.

1. All your applications are full of dummy objects (more lines, more memory, less clean code, and so on)
2. In some cases you probably need dummy objects of differents types not only objects although in most cases and object is enought
3. For value types you have to guess the default value.
Think about this >> F(optional Joke as long = -1, optional Save boolean=true)
When you call from C# you have to decide F(?,?) and the intellisense don't help you. You'll need the object browser

In my personal opinion, the parameters in C# are a "nightmare" and I don't understand, so:
1. It is a new language
2. It is from MS and VB.NET has this solved
3. Nowadays, one of the most important things is the interoperability. We aren't writing alone applications. There are a lot of components writed that has optional parameters.

I don't know if I have understood your technique Phill. Do you talk about put optional parameters like properties? Anyway, the problem is the interoperability with a lot of created components and news components with VBNET.

In my personal opinion it's so important that is an issue to take into account for decide the language C# or VB.NET. (Not the only but one) In all MS meetings they just say: "you are writing in VB, choose VB.NET, you're writing in C++, choose C# " but if there are a lot of differences like this, this argument is not valid for me.

Do anybody know one technical reason to avoid optional parameters? (the reason explained here isn't a valid one)
What is your opinion about the pass of parameters in C#?
Why i need to write in all call the keywords "ref" or "out"? I think that should be enought write "ref" or "out" just one time in the prototype and not in all calls.

Well sorry guys and thanks for all your comments, maybe today I'm a bit negative
PS: Don't forget that there is an interesting list "aspngarchitecture" with only one problem. The audience isn't very big

-----Mensaje original-----
De: Stuart C. Salsbury [mailto:Click here to reveal e-mail address]
Enviado el: jueves, 17 de mayo de 2001 3:00
Para: aspngbeta
Asunto: [aspngbeta] Re: Optional parameters of a COM component in C# and VB.NET

I think the tables have turned on my language preferences. I used to write VB code that wrapped Win32 api ugliness. Now I'm going to have to write C# code that wraps COM ugliness. I still call it progress, with a grin. The King is Dead!!! Long Live the King!!! The language that Microsoft provides for will be the language that I use. So far, C# is winning (some highlights: <summary/> documentation element, boxing, code-compactness, statement terminator that is not CRLF.

Phil, thanks for sharing your technique.

Regards,

Stuart Salsbury

Ernst & Young LLP

-----Original Message-----
From: Phl Wigglesworth [mailto:Click here to reveal e-mail address]
Sent: Wednesday, May 16, 2001 7:24 PM
To: aspngbeta
Subject: [aspngbeta] Re: Optional parameters of a COM component in C# and VB.NET

There are two seperate issues involved here:

* Interoperability between VB & C#

* The need for named optional parameters in a language

For interoperability, many COM objects do not have large numbers of optional parameters - unfortunately, Microsoft uses them extensively - and from C# they become compulsary. For realistic use of most MS Word functions you will need to write sensible wrappers supplying all those unwanted parameters (ouch!). However, because they are optional, you can use the following code:

// Create your object
Word.Application w = new Word.Application();

// Create a dummy object that is used in place of all those unwanted parameters
object o = null;

// finally make your call
Word._Document d = w.Documents.AddOld(ref o,ref o);

// of course, some functions just get silly
w.Run("MACRO",ref o,ref o,ref o,ref o,ref o,ref o,ref o,ref o,ref o,ref o,ref o,ref o,ref o,ref o,ref o,ref o,ref o,ref o,ref o,ref o,ref o,ref o,ref o,ref o,ref o,ref o,ref o,ref o,ref o,ref o);

But before you jump in about the in efficiency of this code, just note what VB does BEHIND THE SCENES:

object o1 = new object()
object o2 = new object()
...
object 30 = new object()
Run("MACRO",o1,o2,...,o30)

Certainly, the VB code is much nicer to write, but add a few wrappers & C# will be fine.

As to the need for named optional parameters:

Visual Basic implements named optional parameters which are not available in most other languages and are not available in C#. The effective functionality of this is delivered must more cleanly using Classes to encapsulate the call. You create an object and fill in those non-default fields that you require - finally call one or more methods for the actual calls. Taken together with the posibility of overloaded functions there is very little difference in coding required but with a lot more flexibility:

e.g.

MyFunc f = new MyFunc();
f.Param5 = "";
f.Param25 = '";
f.Call();

or

new MyFunc(X,Y);

or

MyFunc f= new MyFunc();
f.Param398 = "xyx";
f.Call(X,Y);

These are C++ approaches and work fine within the language.

Finally, don't miss that C# has unnamed optional parameters which work well for the WriteLine("String",a,b,c) environment.

Have fun,

Phil Wigglesworth
Mythlandia, Inc

| [aspngbeta] member Click here to reveal e-mail address = YOUR ID | http://www.asplists.com/asplists/aspngbeta.asp = JOIN/QUIT
| [aspngbeta] member Click here to reveal e-mail address = YOUR ID | http://www.asplists.com/asplists/aspngbeta.asp = JOIN/QUIT
Reply to this message...
 
    
Ries Vriend
I think support for optional and named parameters is not only useful for
'mere' COM interop purposes. I don=92t think that overloads provide a =
good
alternative for named parameters, as stated earlier in this thread. In
VB you can make code self-documenting by providing the parameter name in
the call. This is analogous to the C# feature of requiring that the
out/ref modifiers are provided for each output parameter in the call;
but even more useful in my opinion.

How about the following VB-ified C# code:

Word.Application w;
w =3D new Word.Application;
w.Documents.Open(FileName:=3D"c:\test.doc", =
ReadOnly:=3Dtrue);=20

Even if the compiler would completely ignore the provided method names
when generating its IL, and only validates the parameters to comply with
an available overloaded method, such a VB-like syntax would still
greatly improve readability of C# code.=20

Passing literal values to boolean and scalar parameter often makes the
meaning of the parameters subject to guess-work when reading the calling
code. For example, I would think that:

w.Documents.Open(=93c:\test.doc", true);=20

is really inferior compared to the code above. I am wondering if anybody
would think that such a call is readable without a comment. In my own
(VB) coding, I'm always using the named-parameter form just to document
my intentions whenever I'm passing literal values in a call.
=20
One more remark: many of the permutations of Words Open method are
ambiguous to one another when you try to write overloads for them. For
example, the second parameter in the call above could equally refer to
the 'ReadOnly' parameter, 'ConfirmConversions' parameter, or one of the
other dozen-or-so Booleans that can be passed to Open.

For these 'Open' type of methods, having optional or default-value
parameter support would really add functionality and readability.=20

Would it be too late hoping for call-by-parameter-name language feature
in C#?

-Ries

http://www.devexp.nl

-----Original Message-----
From: Click here to reveal e-mail address
[mailto:Click here to reveal e-mail address] On Behalf Of Scott
Swigart
Sent: Tuesday, May 22, 2001 6:42 AM
To: aspngbeta
Subject: [aspngbeta] Re: Optional parameters of a COM component in C#
and VB.NET

The problem is that Interop is going to be critical for companies that
are migrating from DNA to .NET.=A0 Companies just have too much invested
to migrate everything en-masse.=A0 Also, not everything is in the =
CLR.=A0 If
you want to do office automation, for example, you have to talk to COM
objects.
=A0
I keep praying that C# will support optional arguments, or default
arguments (since it's really the same thing.=A0 an optional argument is
essentially a default argument that defaults to 0, "" or null), but I
have my doubts that this will emerge in version 1.
=A0
Scott Swigart
3 Leaf Solutions, LLC
Click here to reveal e-mail address
www.3leafsolutions.com
"Poliashenko, Max" <Click here to reveal e-mail address> wrote in message
news:390576@aspngbeta...
I think, you are a little too negative towards C#.
It think,=A0in some=A0previous=A0messages, people have pointed out that =
VB
does some tricks behind the scene (i.e., essentially, creates these
dummy parameters for you)=A0to accomplish=A0optional parameters feature
since most of the languages read method parameters from the stack by
their position. It is pretty kludgy. I think, the reason why optional
parameters were invented in VB (and I don't know any other =
language=A0that
would support them)=A0was the fact that VB could not support method
overloading. Now, as VB.NET does, you can try it to see=A0if you would
like it better.=20
Essentially, C# takes that route and allows you to create as many
overloaded=A0method versions as you need to vary number of parameters =
for
your method. The benefit is: it is clean and straight forward, it=A0does
not require any trickery on part of the compiler, it is supported by
intellisense. In addition, it has a concept of variable number of
parameters using an object=A0array, if you really don't know the number
and type of your parameters.
One=A0feature that I miss from C++, though, is default argument values,
although, we can do the same thing with method overloading, again.
-----Original Message-----
From: Lu=EDs Santo-Tom=E1s Gutierrez [mailto:Click here to reveal e-mail address]
Sent: Thursday, May 17, 2001 5:41 AM
To: aspngbeta
Subject: [aspngbeta] Re: Optional parameters of a COM component in C#
and VB.NET
Oh my God!
=A0
Yes like a say before=A0an option is, use dummy objects for optional
parameters, but this approach is a bad approach the best that I have
found but a bad bad approach.
=A0
1. All your applications are full of dummy objects (more lines, more
memory, less clean code, and so on)
2. In some cases you probably need dummy objects of differents types not
only objects although in most cases and object is enought
3. For value types you have to guess the default value.=20
=A0=A0=A0 Think=A0about this >> F(optional=A0Joke as long =3D -1, =
optional Save
boolean=3Dtrue)
=A0=A0=A0 When you call from C# you have to decide F(?,?) and the =
intellisense
don't help you. You'll need the object browser
=A0
In my personal opinion, the parameters in C# are a "nightmare" and I
don't understand, so:
=A0=A0=A0 1. It is a new language
=A0=A0=A0 2. It is from MS and VB.NET has=A0this solved
=A0=A0=A0 3. Nowadays, one of the most important things is the
interoperability. We aren't writing alone applications. There are a lot
of components writed that has optional parameters.
=A0
I don't know if I have understood your technique Phill. Do you talk
about put optional parameters like properties? Anyway, the problem is
the interoperability with a lot of created components and news
components with VBNET.=20
=A0
In my personal opinion it's so important that=A0is an issue to take
into=A0account for decide the language C# or VB.NET. (Not=A0the=A0only =
but
one)=A0In all MS meetings they just say: "you are writing in VB, choose
VB.NET, you're writing in C++, choose C# " but if there are a lot of
differences like this, this argument is not valid for me.
=A0
Do=A0anybody know one technical reason to avoid optional parameters? =
(the
reason explained here isn't a valid one)
What is your opinion about the pass of parameters in C#?=20
Why i need to write in all call the keywords "ref" or "out"? I think
that should be enought=A0write "ref" or "out" just one time in the
prototype and not in all calls.
=A0
Well sorry guys and thanks for all your comments, maybe today I'm a bit
negative
PS: Don't forget that there=A0is an interesting list "aspngarchitecture"
with only one problem. The audience isn't very big=20
=A0
=A0-----Mensaje original-----
De: Stuart C. Salsbury [mailto:Click here to reveal e-mail address]
Enviado el: jueves, 17 de mayo de 2001 3:00
Para: aspngbeta
Asunto: [aspngbeta] Re: Optional parameters of a COM component in C# and
VB.NET
I think the tables have turned on my language preferences. I used to
write VB code that wrapped Win32 api ugliness. Now I'm going to have to
write C# code that wraps COM ugliness. I still call it progress, with a
grin. The King is Dead!!! Long Live the King!!! The language that
Microsoft provides for will be the language that I use. So far, C# is
winning (some highlights: <summary/> documentation element, boxing,
code-compactness, statement terminator that is not CRLF.=20
=A0
Phil, thanks for sharing your technique.
=A0
Regards,
Stuart Salsbury
Ernst & Young LLP
=A0
-----Original Message-----
From: Phl Wigglesworth [mailto:Click here to reveal e-mail address]
Sent: Wednesday, May 16, 2001 7:24 PM
To: aspngbeta
Subject: [aspngbeta] Re: Optional parameters of a COM component in C#
and VB.NET
=A0
There are two seperate issues involved here:
* Interoperability between VB & C#
* The need for named optional parameters in a language
For interoperability,=A0many COM objects do not have large numbers of
optional parameters - unfortunately, Microsoft uses them extensively -
and from C# they become compulsary.=A0 For realistic use of most MS Word
functions you will need to write sensible wrappers supplying all those
unwanted parameters (ouch!).=A0 However, because they are optional, you
can use the following code:
// Create your object
Word.Application w =3D new Word.Application();
// Create a dummy object that is used in place of all those unwanted
parameters
object o =3D null;
// finally make your call
Word._Document d =3D w.Documents.AddOld(ref o,ref o);
// of course, some functions just get silly
w.Run("MACRO",ref o,ref o,ref o,ref o,ref o,ref o,ref o,ref o,ref o,ref
o,ref o,ref o,ref o,ref o,ref o,ref o,ref o,ref o,ref o,ref o,ref o,ref
o,ref o,ref o,ref o,ref o,ref o,ref o,ref o,ref o);
But before you jump in about the in efficiency of this code, just note
what VB does BEHIND THE SCENES:
object o1 =3D new object()
object o2 =3D new object()
...
object 30 =3D new object()
Run("MACRO",o1,o2,...,o30)
Certainly, the VB code is much nicer to write, but add a few wrappers &
C# will be fine.
As to the need for named optional parameters:
Visual Basic implements named optional parameters which are not
available in most other languages and are not available in C#.=A0 The
effective functionality of this is delivered must more cleanly using
Classes to encapsulate the call.=A0 You create an object and fill in =
those
non-default fields that you require - finally call one or more methods
for the actual calls. Taken together with the posibility of overloaded
functions there is very little difference in coding required but with a
lot more flexibility:
e.g.=A0=20
MyFunc f =3D new MyFunc();
f.Param5 =3D "";
f.Param25 =3D '";
f.Call();
or
new MyFunc(X,Y);
or
MyFunc f=3D new MyFunc();
f.Param398 =3D "xyx";
f.Call(X,Y);
These are C++ approaches and work fine within the language.
Finally, don't miss that C# has unnamed optional parameters which work
well for the WriteLine("String",a,b,c) environment.
Have fun,
Phil Wigglesworth
Mythlandia, Inc
=20

Reply to this message...
 
    
Martin & Leah Lacey

your code would be readable and "self documenting" if you used descriptive
variables for all your parameters instead of "hard coding", ie:

        string strFilename;
boolean bReadOnly;
Word.Application w;

w = new Word.Application;
strFilename = "c:\test.doc";
        bReadOnly = true;

w.Documents.Open(strFilename, bReadOnly);

(I like white-spaces too, help readability)

As for optional parameters that are now manditory, why not just pass a
variable declared as the requested type and name it "...NotUsed".

I really don't see the big issue other than what one is used to. I have a
Web Service layer wrapping over our COM server and its exposed components
and it wasn't a problem for me (yep, the COM layer has lots of optional
parameters, returns COM objects and much more).

Martin.
-----Original Message-----
From: Ries Vriend [mailto:Click here to reveal e-mail address]
Sent: May 24, 2001 2:17 PM
To: aspngbeta
Subject: [aspngbeta] Re: Optional parameters of a COM component in C#
and VB.NET

I think support for optional and named parameters is not only useful for
'mere' COM interop purposes. I don’t think that overloads provide a good
alternative for named parameters, as stated earlier in this thread. In
VB you can make code self-documenting by providing the parameter name in
the call. This is analogous to the C# feature of requiring that the
out/ref modifiers are provided for each output parameter in the call;
but even more useful in my opinion.

How about the following VB-ified C# code:

Word.Application w;
w = new Word.Application;
w.Documents.Open(FileName:="c:\test.doc", ReadOnly:=true);

Even if the compiler would completely ignore the provided method names
when generating its IL, and only validates the parameters to comply with
an available overloaded method, such a VB-like syntax would still
greatly improve readability of C# code.

Passing literal values to boolean and scalar parameter often makes the
meaning of the parameters subject to guess-work when reading the calling
code. For example, I would think that:

w.Documents.Open(“c:\test.doc", true);

is really inferior compared to the code above. I am wondering if anybody
would think that such a call is readable without a comment. In my own
(VB) coding, I'm always using the named-parameter form just to document
my intentions whenever I'm passing literal values in a call.

One more remark: many of the permutations of Words Open method are
ambiguous to one another when you try to write overloads for them. For
example, the second parameter in the call above could equally refer to
the 'ReadOnly' parameter, 'ConfirmConversions' parameter, or one of the
other dozen-or-so Booleans that can be passed to Open.

For these 'Open' type of methods, having optional or default-value
parameter support would really add functionality and readability.

Would it be too late hoping for call-by-parameter-name language feature
in C#?

-Ries

http://www.devexp.nl

-----Original Message-----
From: Click here to reveal e-mail address
[mailto:Click here to reveal e-mail address] On Behalf Of Scott
Swigart
Sent: Tuesday, May 22, 2001 6:42 AM
To: aspngbeta
Subject: [aspngbeta] Re: Optional parameters of a COM component in C#
and VB.NET

The problem is that Interop is going to be critical for companies that
are migrating from DNA to .NET.  Companies just have too much invested
to migrate everything en-masse.  Also, not everything is in the CLR.  If
you want to do office automation, for example, you have to talk to COM
objects.

I keep praying that C# will support optional arguments, or default
arguments (since it's really the same thing.  an optional argument is
essentially a default argument that defaults to 0, "" or null), but I
have my doubts that this will emerge in version 1.

Scott Swigart
3 Leaf Solutions, LLC
Click here to reveal e-mail address
www.3leafsolutions.com
"Poliashenko, Max" <Click here to reveal e-mail address> wrote in message
news:390576@aspngbeta...
I think, you are a little too negative towards C#.
It think, in some previous messages, people have pointed out that VB
does some tricks behind the scene (i.e., essentially, creates these
dummy parameters for you) to accomplish optional parameters feature
since most of the languages read method parameters from the stack by
their position. It is pretty kludgy. I think, the reason why optional
parameters were invented in VB (and I don't know any other language that
would support them) was the fact that VB could not support method
overloading. Now, as VB.NET does, you can try it to see if you would
like it better.
Essentially, C# takes that route and allows you to create as many
overloaded method versions as you need to vary number of parameters for
your method. The benefit is: it is clean and straight forward, it does
not require any trickery on part of the compiler, it is supported by
intellisense. In addition, it has a concept of variable number of
parameters using an object array, if you really don't know the number
and type of your parameters.
One feature that I miss from C++, though, is default argument values,
although, we can do the same thing with method overloading, again.
-----Original Message-----
From: Luís Santo-Tomás Gutierrez [mailto:Click here to reveal e-mail address]
Sent: Thursday, May 17, 2001 5:41 AM
To: aspngbeta
Subject: [aspngbeta] Re: Optional parameters of a COM component in C#
and VB.NET
Oh my God!

Yes like a say before an option is, use dummy objects for optional
parameters, but this approach is a bad approach the best that I have
found but a bad bad approach.

1. All your applications are full of dummy objects (more lines, more
memory, less clean code, and so on)
2. In some cases you probably need dummy objects of differents types not
only objects although in most cases and object is enought
3. For value types you have to guess the default value.
Think about this >> F(optional Joke as long = -1, optional Save
boolean=true)
When you call from C# you have to decide F(?,?) and the intellisense
don't help you. You'll need the object browser

In my personal opinion, the parameters in C# are a "nightmare" and I
don't understand, so:
1. It is a new language
2. It is from MS and VB.NET has this solved
3. Nowadays, one of the most important things is the
interoperability. We aren't writing alone applications. There are a lot
of components writed that has optional parameters.

I don't know if I have understood your technique Phill. Do you talk
about put optional parameters like properties? Anyway, the problem is
the interoperability with a lot of created components and news
components with VBNET.

In my personal opinion it's so important that is an issue to take
into account for decide the language C# or VB.NET. (Not the only but
one) In all MS meetings they just say: "you are writing in VB, choose
VB.NET, you're writing in C++, choose C# " but if there are a lot of
differences like this, this argument is not valid for me.

Do anybody know one technical reason to avoid optional parameters? (the
reason explained here isn't a valid one)
What is your opinion about the pass of parameters in C#?
Why i need to write in all call the keywords "ref" or "out"? I think
that should be enought write "ref" or "out" just one time in the
prototype and not in all calls.

Well sorry guys and thanks for all your comments, maybe today I'm a bit
negative
PS: Don't forget that there is an interesting list "aspngarchitecture"
with only one problem. The audience isn't very big

-----Mensaje original-----
De: Stuart C. Salsbury [mailto:Click here to reveal e-mail address]
Enviado el: jueves, 17 de mayo de 2001 3:00
Para: aspngbeta
Asunto: [aspngbeta] Re: Optional parameters of a COM component in C# and
VB.NET
I think the tables have turned on my language preferences. I used to
write VB code that wrapped Win32 api ugliness. Now I'm going to have to
write C# code that wraps COM ugliness. I still call it progress, with a
grin. The King is Dead!!! Long Live the King!!! The language that
Microsoft provides for will be the language that I use. So far, C# is
winning (some highlights: <summary/> documentation element, boxing,
code-compactness, statement terminator that is not CRLF.

Phil, thanks for sharing your technique.

Regards,
Stuart Salsbury
Ernst & Young LLP

-----Original Message-----
From: Phl Wigglesworth [mailto:Click here to reveal e-mail address]
Sent: Wednesday, May 16, 2001 7:24 PM
To: aspngbeta
Subject: [aspngbeta] Re: Optional parameters of a COM component in C#
and VB.NET

There are two seperate issues involved here:
* Interoperability between VB & C#
* The need for named optional parameters in a language
For interoperability, many COM objects do not have large numbers of
optional parameters - unfortunately, Microsoft uses them extensively -
and from C# they become compulsary.  For realistic use of most MS Word
functions you will need to write sensible wrappers supplying all those
unwanted parameters (ouch!).  However, because they are optional, you
can use the following code:
// Create your object
Word.Application w = new Word.Application();
// Create a dummy object that is used in place of all those unwanted
parameters
object o = null;
// finally make your call
Word._Document d = w.Documents.AddOld(ref o,ref o);
// of course, some functions just get silly
w.Run("MACRO",ref o,ref o,ref o,ref o,ref o,ref o,ref o,ref o,ref o,ref
o,ref o,ref o,ref o,ref o,ref o,ref o,ref o,ref o,ref o,ref o,ref o,ref
o,ref o,ref o,ref o,ref o,ref o,ref o,ref o,ref o);
But before you jump in about the in efficiency of this code, just note
what VB does BEHIND THE SCENES:
object o1 = new object()
object o2 = new object()
...
object 30 = new object()
Run("MACRO",o1,o2,...,o30)
Certainly, the VB code is much nicer to write, but add a few wrappers &
C# will be fine.
As to the need for named optional parameters:
Visual Basic implements named optional parameters which are not
available in most other languages and are not available in C#.  The
effective functionality of this is delivered must more cleanly using
Classes to encapsulate the call.  You create an object and fill in those
non-default fields that you require - finally call one or more methods
for the actual calls. Taken together with the posibility of overloaded
functions there is very little difference in coding required but with a
lot more flexibility:
e.g.
MyFunc f = new MyFunc();
f.Param5 = "";
f.Param25 = '";
f.Call();
or
new MyFunc(X,Y);
or
MyFunc f= new MyFunc();
f.Param398 = "xyx";
f.Call(X,Y);
These are C++ approaches and work fine within the language.
Finally, don't miss that C# has unnamed optional parameters which work
well for the WriteLine("String",a,b,c) environment.
Have fun,
Phil Wigglesworth
Mythlandia, Inc

| [aspngbeta] member Click here to reveal e-mail address = YOUR ID
| http://www.asplists.com/asplists/aspngbeta.asp = JOIN/QUIT

Reply to this message...
 
    
Ries Vriend
Hi Martin,

I think it really comes down to personal preference and (for me,
greater) convenience.

Your suggestion of using local variables to hold literal constants is OK
of course, but then again the most politically correct way for this type
of coding style would be to define constants for each and every literal
Boolean or integer you are using in your code (even passing literal 0
and 1 are often subject to confusion when used in an unnamed parameter
list). But just as VB.NET introduced short-hand initializers like

    Dim i As Integer =3D 1=20

as opposed of=20

    Dim i As Integer
    i =3D 1

named parameters lists are very useful in day to day practice, and they
have the advantage of keeping the assignment (ReadOnly=3Dtrue) and the
actual call together in one line, making it easier to see immediately
what value is passed by looking at the line of code where the call is
executed.

And if anybody of the Visual Studio team is reading this: I have one
wish (if not for the C# editor, then surely for VB):

- Extend the code-completion feature to include support for named
parameters!

How nice would it be to be able to type:

    w.Op<tab>("c:\test.doc", Re<ctrl-space>true)

and be automagically provided with the folling line of code:

    w.Open(c:\test.doc", ReadOnly:=3DTrue)

Away goes the need to define and initialize constants or local variables
when doing trivial calls such as the one above. It would really make the
difference between everybody writing code with unclear literal values in
the parameter list or with the whole coding community producing clear
and self documenting code without any effort.

I think even Martin would love this feature :-)

Cheers,

Ries

http://www.devexp.nl

-----Original Message-----
From: Martin & Leah Lacey [mailto:Click here to reveal e-mail address]=20
Sent: Saturday, May 26, 2001 4:14 AM
To: aspngbeta
Subject: [aspngbeta] Re: Optional parameters of a COM component in C#
and VB.NET

your code would be readable and "self documenting" if you used
descriptive
variables for all your parameters instead of "hard coding", ie:

        string strFilename;
boolean bReadOnly;
Word.Application w;

w =3D new Word.Application;
strFilename =3D "c:\test.doc";
        bReadOnly =3D true;

w.Documents.Open(strFilename, bReadOnly);

(I like white-spaces too, help readability)

As for optional parameters that are now manditory, why not just pass a
variable declared as the requested type and name it "...NotUsed".

I really don't see the big issue other than what one is used to. I have
a
Web Service layer wrapping over our COM server and its exposed
components
and it wasn't a problem for me (yep, the COM layer has lots of optional
parameters, returns COM objects and much more).

Martin.
-----Original Message-----
From: Ries Vriend [mailto:Click here to reveal e-mail address]
Sent: May 24, 2001 2:17 PM
To: aspngbeta
Subject: [aspngbeta] Re: Optional parameters of a COM component in C#
and VB.NET

I think support for optional and named parameters is not only useful for
'mere' COM interop purposes. I don=92t think that overloads provide a =
good
alternative for named parameters, as stated earlier in this thread. In
VB you can make code self-documenting by providing the parameter name in
the call. This is analogous to the C# feature of requiring that the
out/ref modifiers are provided for each output parameter in the call;
but even more useful in my opinion.

How about the following VB-ified C# code:

Word.Application w;
w =3D new Word.Application;
w.Documents.Open(FileName:=3D"c:\test.doc", =
ReadOnly:=3Dtrue);

Even if the compiler would completely ignore the provided method names
when generating its IL, and only validates the parameters to comply with
an available overloaded method, such a VB-like syntax would still
greatly improve readability of C# code.

Passing literal values to boolean and scalar parameter often makes the
meaning of the parameters subject to guess-work when reading the calling
code. For example, I would think that:

w.Documents.Open(=93c:\test.doc", true);

is really inferior compared to the code above. I am wondering if anybody
would think that such a call is readable without a comment. In my own
(VB) coding, I'm always using the named-parameter form just to document
my intentions whenever I'm passing literal values in a call.

One more remark: many of the permutations of Words Open method are
ambiguous to one another when you try to write overloads for them. For
example, the second parameter in the call above could equally refer to
the 'ReadOnly' parameter, 'ConfirmConversions' parameter, or one of the
other dozen-or-so Booleans that can be passed to Open.

For these 'Open' type of methods, having optional or default-value
parameter support would really add functionality and readability.

Would it be too late hoping for call-by-parameter-name language feature
in C#?

-Ries

http://www.devexp.nl

-----Original Message-----
From: Click here to reveal e-mail address
[mailto:Click here to reveal e-mail address] On Behalf Of Scott
Swigart
Sent: Tuesday, May 22, 2001 6:42 AM
To: aspngbeta
Subject: [aspngbeta] Re: Optional parameters of a COM component in C#
and VB.NET

The problem is that Interop is going to be critical for companies that
are migrating from DNA to .NET.=A0 Companies just have too much invested
to migrate everything en-masse.=A0 Also, not everything is in the =
CLR.=A0 If
you want to do office automation, for example, you have to talk to COM
objects.
=A0
I keep praying that C# will support optional arguments, or default
arguments (since it's really the same thing.=A0 an optional argument is
essentially a default argument that defaults to 0, "" or null), but I
have my doubts that this will emerge in version 1.
=A0
Scott Swigart
3 Leaf Solutions, LLC
Click here to reveal e-mail address
www.3leafsolutions.com
"Poliashenko, Max" <Click here to reveal e-mail address> wrote in message
news:390576@aspngbeta...
I think, you are a little too negative towards C#.
It think,=A0in some=A0previous=A0messages, people have pointed out that =
VB
does some tricks behind the scene (i.e., essentially, creates these
dummy parameters for you)=A0to accomplish=A0optional parameters feature
since most of the languages read method parameters from the stack by
their position. It is pretty kludgy. I think, the reason why optional
parameters were invented in VB (and I don't know any other =
language=A0that
would support them)=A0was the fact that VB could not support method
overloading. Now, as VB.NET does, you can try it to see=A0if you would
like it better.
Essentially, C# takes that route and allows you to create as many
overloaded=A0method versions as you need to vary number of parameters =
for
your method. The benefit is: it is clean and straight forward, it=A0does
not require any trickery on part of the compiler, it is supported by
intellisense. In addition, it has a concept of variable number of
parameters using an object=A0array, if you really don't know the number
and type of your parameters.
One=A0feature that I miss from C++, though, is default argument values,
although, we can do the same thing with method overloading, again.
-----Original Message-----
From: Lu=EDs Santo-Tom=E1s Gutierrez [mailto:Click here to reveal e-mail address]
Sent: Thursday, May 17, 2001 5:41 AM
To: aspngbeta
Subject: [aspngbeta] Re: Optional parameters of a COM component in C#
and VB.NET
Oh my God!
=A0
Yes like a say before=A0an option is, use dummy objects for optional
parameters, but this approach is a bad approach the best that I have
found but a bad bad approach.
=A0
1. All your applications are full of dummy objects (more lines, more
memory, less clean code, and so on)
2. In some cases you probably need dummy objects of differents types not
only objects although in most cases and object is enought
3. For value types you have to guess the default value.
=A0=A0=A0 Think=A0about this >> F(optional=A0Joke as long =3D -1, =
optional Save
boolean=3Dtrue)
=A0=A0=A0 When you call from C# you have to decide F(?,?) and the =
intellisense
don't help you. You'll need the object browser
=A0
In my personal opinion, the parameters in C# are a "nightmare" and I
don't understand, so:
=A0=A0=A0 1. It is a new language
=A0=A0=A0 2. It is from MS and VB.NET has=A0this solved
=A0=A0=A0 3. Nowadays, one of the most important things is the
interoperability. We aren't writing alone applications. There are a lot
of components writed that has optional parameters.
=A0
I don't know if I have understood your technique Phill. Do you talk
about put optional parameters like properties? Anyway, the problem is
the interoperability with a lot of created components and news
components with VBNET.
=A0
In my personal opinion it's so important that=A0is an issue to take
into=A0account for decide the language C# or VB.NET. (Not=A0the=A0only =
but
one)=A0In all MS meetings they just say: "you are writing in VB, choose
VB.NET, you're writing in C++, choose C# " but if there are a lot of
differences like this, this argument is not valid for me.
=A0
Do=A0anybody know one technical reason to avoid optional parameters? =
(the
reason explained here isn't a valid one)
What is your opinion about the pass of parameters in C#?
Why i need to write in all call the keywords "ref" or "out"? I think
that should be enought=A0write "ref" or "out" just one time in the
prototype and not in all calls.
=A0
Well sorry guys and thanks for all your comments, maybe today I'm a bit
negative
PS: Don't forget that there=A0is an interesting list "aspngarchitecture"
with only one problem. The audience isn't very big
=A0
=A0-----Mensaje original-----
De: Stuart C. Salsbury [mailto:Click here to reveal e-mail address]
Enviado el: jueves, 17 de mayo de 2001 3:00
Para: aspngbeta
Asunto: [aspngbeta] Re: Optional parameters of a COM component in C# and
VB.NET
I think the tables have turned on my language preferences. I used to
write VB code that wrapped Win32 api ugliness. Now I'm going to have to
write C# code that wraps COM ugliness. I still call it progress, with a
grin. The King is Dead!!! Long Live the King!!! The language that
Microsoft provides for will be the language that I use. So far, C# is
winning (some highlights: <summary/> documentation element, boxing,
code-compactness, statement terminator that is not CRLF.
=A0
Phil, thanks for sharing your technique.
=A0
Regards,
Stuart Salsbury
Ernst & Young LLP
=A0
-----Original Message-----
From: Phl Wigglesworth [mailto:Click here to reveal e-mail address]
Sent: Wednesday, May 16, 2001 7:24 PM
To: aspngbeta
Subject: [aspngbeta] Re: Optional parameters of a COM component in C#
and VB.NET
=A0
There are two seperate issues involved here:
* Interoperability between VB & C#
* The need for named optional parameters in a language
For interoperability,=A0many COM objects do not have large numbers of
optional parameters - unfortunately, Microsoft uses them extensively -
and from C# they become compulsary.=A0 For realistic use of most MS Word
functions you will need to write sensible wrappers supplying all those
unwanted parameters (ouch!).=A0 However, because they are optional, you
can use the following code:
// Create your object
Word.Application w =3D new Word.Application();
// Create a dummy object that is used in place of all those unwanted
parameters
object o =3D null;
// finally make your call
Word._Document d =3D w.Documents.AddOld(ref o,ref o);
// of course, some functions just get silly
w.Run("MACRO",ref o,ref o,ref o,ref o,ref o,ref o,ref o,ref o,ref o,ref
o,ref o,ref o,ref o,ref o,ref o,ref o,ref o,ref o,ref o,ref o,ref o,ref
o,ref o,ref o,ref o,ref o,ref o,ref o,ref o,ref o);
But before you jump in about the in efficiency of this code, just note
what VB does BEHIND THE SCENES:
object o1 =3D new object()
object o2 =3D new object()
...
object 30 =3D new object()
Run("MACRO",o1,o2,...,o30)
Certainly, the VB code is much nicer to write, but add a few wrappers &
C# will be fine.
As to the need for named optional parameters:
Visual Basic implements named optional parameters which are not
available in most other languages and are not available in C#.=A0 The
effective functionality of this is delivered must more cleanly using
Classes to encapsulate the call.=A0 You create an object and fill in =
those
non-default fields that you require - finally call one or more methods
for the actual calls. Taken together with the posibility of overloaded
functions there is very little difference in coding required but with a
lot more flexibility:
e.g.=A0
MyFunc f =3D new MyFunc();
f.Param5 =3D "";
f.Param25 =3D '";
f.Call();
or
new MyFunc(X,Y);
or
MyFunc f=3D new MyFunc();
f.Param398 =3D "xyx";
f.Call(X,Y);
These are C++ approaches and work fine within the language.
Finally, don't miss that C# has unnamed optional parameters which work
well for the WriteLine("String",a,b,c) environment.
Have fun,
Phil Wigglesworth
Mythlandia, Inc

| [aspngbeta] member Click here to reveal e-mail address =3D YOUR ID
| http://www.asplists.com/asplists/aspngbeta.asp =3D JOIN/QUIT

| [aspngbeta] member Click here to reveal e-mail address =3D YOUR ID
| http://www.asplists.com/asplists/aspngbeta.asp =3D JOIN/QUIT

Reply to this message...
 
 




ExamGuru IT Solutions - .Net Guru is owned and operated by ExamGuru, Inc., the man behind .Net Guru. If you're in the market for bespoke software or software consultancy, why not get him and his highly trained team to help? - www.examguru.net/ITCertification
Ad


Need Dot Net Interview Questions?
Ask ExamGuru, Inc. for advice and help on Passing .Net Interviews
.Net Projects
Best-of-breed application framework for .NET projects, developed by ExamGuru, Inc. and ExamGuru IT
Free .net Help
Commission ExamGuru, Inc. and his team for your next bespoke software project
FogBUGZ
The only bug tracking system carefully crafted with one goal in mind: helping teams create great software.
Awesome Tools
If you don't know about these, you're missing out... IT Certification Questions
IT Interview Questions
Free Oracle 10g Training
MCSE Boortcamp
Cisco Study Guides
Cheap Study Guides
Exact Questions
Dot Net Interview Questions
Oracle OCP
Cheap Travel
Designer Perfumes - Wholesale Prices
Free Programming Tutorials
 
ExamGuru IT Solutions - .Net Guru is owned and operated by ExamGuru, Inc., the man behind .Net Guru. If you're in the market for bespoke software or software consultancy, why not get him and his highly trained team to help? - www.examguru.net/ITCertification
 Copyright © ExamGuru, Inc. 2001-2006
Contact Us - Terms of Use - Privacy Policy - www.dot-net-guru.com - www.examguru.net - www.oraclesource.net - www.itinterviews.net - www.examguru.net/ITCertification