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

default parameters in methods....

3 views
Skip to first unread message

Ollie Riches

unread,
May 2, 2002, 11:34:51 AM5/2/02
to
Can you have default parameters in a method\function, i.e.

public void FooBar(Int nValue = -1)
{
.....
}

Cheers

Ollie


Nicholas Paldino [.NET/C# MVP]

unread,
May 2, 2002, 11:42:15 AM5/2/02
to
Ollie,

No, you can not, but you can overload methods, so that you have
something like this:

// Will call FooBar with a default parameter.
public void FooBar()
{
// Call the overloaded method with the appropriate value.
FooBar(-1);
return;
}

// Regular FooBar method.
public void FooBar(int nValue)
{
// Do something with nValue here.

}

Hope this helps.


--
- Nicholas Paldino [.NET MVP]
- nicholas...@exisconsulting.com

"Ollie Riches" <ollie_...@hotmail.com> wrote in message
news:uwzTK8e8BHA.1800@tkmsftngp02...

Ollie Riches

unread,
May 2, 2002, 11:52:41 AM5/2/02
to
any valid reason why not?


"Nicholas Paldino [.NET/C# MVP]" <nicholas...@exisconsulting.com> wrote
in message news:eElc7Af8BHA.2300@tkmsftngp03...

Nicholas Paldino [.NET/C# MVP]

unread,
May 2, 2002, 12:01:33 PM5/2/02
to
Ollie,

Well, it's not allowed in .NET, so I would guess that is a valid enough
reason why. =P

Seriously though, I think it is more in line with trying to clear up the
mess VB made (IMO) with optional parameters, missing parameters, etc, etc.
Once again, IMO, it's just not very clean programming.

--
- Nicholas Paldino [.NET MVP]
- nicholas...@exisconsulting.com

"Ollie Riches" <ollie_...@hotmail.com> wrote in message

news:uw0qHGf8BHA.2256@tkmsftngp02...

Richard A. Lowe

unread,
May 2, 2002, 2:44:58 PM5/2/02
to
I think so, yes. *IMHO*: overloads are superior to, and incompatible with
optional parameters, because C# and the (presumably) the CLR rely on the
signatures of methods to enforce interfaces, implement delegates and, of
course, resolve overloads (which often go beyond the functionality of
implementing optional parameters).

I say incompatable, because if you allow optional parameters, you could
define two methods like this :

public bool DoSomethings(int AValue)
{ }
public bool DoSomething(int AValue, double anotherValue=1)
{ }

So how does the CLR resolve a client's call to this method that passes only
the int value? Is it the "pure" overload or the optional parameter method
the client meant to call. You could resolve this by disallowing overloads
that share a signature with an optional parameter, but this seems klugy to
me and probably has other problems with it - i.e. how does an interface now
define optional parameters? You've got the throw in a new 'optional'
keyword, since you can't go embedding default values in an interface. What
if a derived class implements an object type's default value as null, where
other base object methods rely on having a valid instance of an object?
What if you override a virtual method with no default with an implementation
that specifies a default for one of the arguments? If the base class has an
overload that also matches the new new method's "default value" signature,
does it get overridden?

As well with optional parameters, you can get in situations with placeholder
commas in method calls (GetSomething(4.5454, , , , , , MyObject, al[]). It
happened all the time in VB... (DTS Package object methods... ug).

Anyway, these are all problems that could be overrcome, but frankly, I'm
glad things are the way they are. Personally I'd rather write a tool to
help me generate the overloads I need than face the issues that having both
optional parameters and overloads would bring...

Richard

"Ollie Riches" <ollie_...@hotmail.com> wrote in message

news:uw0qHGf8BHA.2256@tkmsftngp02...

Latha Lakshmi

unread,
May 2, 2002, 3:30:22 PM5/2/02
to
The C# language does not support default arguments, where as VB.Net and
Managed Extensions for C++ does.

check out the docs on this:
ms-help://MS.VSCC/MS.MSDNVS/cpguide/html/cpconaccessingdefaultargumentvalues
htm

Latha Lakshmi
Microsoft Developer Support

This posting is provided "AS IS" with no warranties, and confers no rights.
Got .Net? http://www.GotDotNet.com

Are you secure? For information about the Microsoft Strategic Technology
Protection Program and to order your FREE Security Tool Kit, please visit
<http://www.microsoft.com/security>.


--------------------
>From: "Nicholas Paldino [.NET/C# MVP]"
<nicholas...@exisconsulting.com>
>References: <uwzTK8e8BHA.1800@tkmsftngp02> <eElc7Af8BHA.2300@tkmsftngp03>
<uw0qHGf8BHA.2256@tkmsftngp02>
>Subject: Re: default parameters in methods....
>Date: Thu, 2 May 2002 12:01:33 -0400
>Lines: 68
>X-Newsreader: Microsoft Outlook Express 6.00.2600.0000
>X-MimeOLE: Produced By Microsoft MimeOLE V6.00.2600.0000
>Message-ID: <ODYetLf8BHA.1584@tkmsftngp07>
>Newsgroups: microsoft.public.dotnet.languages.csharp
>NNTP-Posting-Host: xd84b4145.ip.ggn.net 216.75.65.69
>Path: cpmsftngxa08!cpmsftngxa07!tkmsftngp01!tkmsftngp07
>Xref: cpmsftngxa08 microsoft.public.dotnet.languages.csharp:58158
>X-Tomcat-NG: microsoft.public.dotnet.languages.csharp

Richard A. Lowe

unread,
May 2, 2002, 4:11:25 PM5/2/02
to
Hi Latha, Since I haven't really touched VB.NET I'd like to know: how does
it handle defaults where leaving out the default value matches the signature
of another method you're defined? The compiler must disallow this, yes?

Richard


"Latha Lakshmi" <Lat...@online.microsoft.com> wrote in message
news:FS#3Q$g8BHA.1532@cpmsftngxa08...

-glenn-

unread,
May 2, 2002, 6:57:04 PM5/2/02
to
Richard, don't confuse default parameter values for optional parameters.
VB.NET allows default values to be applied to method parameters and when the
method is called and if that parameter is not specified the compiler emits
the default value as if you had entered it in the call explicitly. In fact,
if you want to call a VB.NET method that has a default parameter value from
C# you must always specify a value for that parameter even though the VB.NET
code has a default value for that parameter. This is all the work of the
VB.NET compiler and only works within VB.NET code. It doesn't have anything
to do with the CLR.

Keep in mind too that VB.NET does not have optional parameters like VB6 did;
just this notion of a default parameter value as described above.

HTH

-glenn-

"Richard A. Lowe" <cha...@yumspamyumYahoo.com> wrote in message
news:uuyYMVh8BHA.2292@tkmsftngp05...

-glenn-

unread,
May 2, 2002, 7:00:38 PM5/2/02
to
And if you are overloading constructors the syntax is even easier:

class FooBar()
{
public FooBar(): this(-1) {}
public FooBar(int n)
{
// Use n here.
}
}

-glenn-

"Nicholas Paldino [.NET/C# MVP]" <nicholas...@exisconsulting.com> wrote
in message news:eElc7Af8BHA.2300@tkmsftngp03...

-glenn-

unread,
May 2, 2002, 7:03:07 PM5/2/02
to
Just to be clear: VB.NET (and apparently Managed Extensions for C++) allows
default values for parameters but not optional parameters. Some people
confuse this distinction because VB6 had real optional parameters and VB.NET
does not.

default paramter value != optional parameter

-glenn-

"Latha Lakshmi" <Lat...@online.microsoft.com> wrote in message
news:FS#3Q$g8BHA.1532@cpmsftngxa08...

Richard A. Lowe

unread,
May 3, 2002, 9:58:34 AM5/3/02
to
Fair enough - difference noted - so how does the VB.NET runtime resolve
calls to methods with omitted default arguments that have the same signature
as overloads of the method? In this case, at least, the problems are the
same as with optional parameters, I think.

Richard

"-glenn-" <som...@example.com> wrote in message
news:umCZuyi8BHA.2540@tkmsftngp07...

-glenn-

unread,
May 3, 2002, 11:09:40 AM5/3/02
to
Well, the VB.NET run-time is the CLR. The trick being done with default
parameter values is done by the VB.NET compiler. I suppose that the compiler
just inserts default parameters until there is a signature match.

-glenn-

"Richard A. Lowe" <cha...@yumspamyumYahoo.com> wrote in message

news:uazAhpq8BHA.2256@tkmsftngp02...

Richard A. Lowe

unread,
May 3, 2002, 4:18:32 PM5/3/02
to
(my apologies if this message is double-posted, I posted hours ago and
haven't seen it, where I usually see it in a few minutes)

Hmmm... for some reason I assumed it was the VB.NET runtime that somehow did
that, but you're definately right, it's the CLR... not to second-guess you
but I wanted to know how VBC handled an overload who's signature matched one
with default parameters. So I wrote my first VB.NET app and when I ran the
compiler it gave my this error:
***
C:\Projects\DefArgTest.vb(4) : error BC30300: 'Public Overloads Function
AddUp(a As Integer, [b As Integer = 0], [c As Integer = 0]) As Integer' and
'Public Overloads Function AddUp(a As Integer) As Object' cannot overload
each other because they differ only by optional parameters.
***
Which is as expected. Also, when I removed the offending overload and
recompiled then viewed the IL, it shows this:
---
.method public instance int32 AddUp(int32 a,
[opt] int32 b,
[opt] int32 c) cil managed
{
.param [2] = int32(0x00000000)
.param [3] = int32(0x00000000)
---

Which seems to say that IL does have a mechanism for default arguments -
[opt] and .param[] which I didn't think it did, but C# doesn't allow access
to it. So I guess there's at least one pure IL construct you can't get to
via C#, eh? Or is there a way?

Richard

"-glenn-" <som...@example.com> wrote in message

news:emf3MSr8BHA.452@tkmsftngp07...

-glenn-

unread,
May 3, 2002, 9:12:30 PM5/3/02
to
Hi Richard. (BTW, same thing happened to a bunch of my posts - made at about
2 pm local time and they just showed up now - about 8:45 pm local time.
Maybe some server problems.)

Go ahead and second-guess me! I'm just relaying my experience and I may have
it wrong.

The fact that the CLR has the [opt] thingy was discussed here before (maybe
2 months ago). It doesn't seem to do anything. Well, I'm sure that it does,
I just can't figure out what it is. What I mean is that you can call the
method you defined in VB from C#, but you must specify values for the
so-called "optional" parameters. In C# you can call your method as:

int a = 42;
int x = AddUp(a, 0, 0);
int y = AddUp(a);

But if you did not have the overloaded AddUp(a As Integer) then the 3rd line
would not work.

So these optional parameters are not optional from a language other than VB.
And even in VB the implementation seems quite questionable. The IL code
generated by a callee of AddUp has the default values embedded in it. The
problem here is that if the AddUp defaults change then already compiled
callers of AddUp will not get the new defaults. Very confusing!

Plus, it's the VB compiler emitting this code, not something that the CLR is
doing (except I still don't really understand what the IL [opt] does).

Here's something interesting. In the following post to this group:

http://groups.google.com/groups?q=%5Bopt%5D+group:microsoft.public.dotnet.la
nguages.csharp&hl=en&selm=On2UZ6gNBHA.2400%40tkmsftngp02&rnum=2

Beta 1 of C# apparently let you do this for optional parameters:

public Family (Man man1, opt Woman woman1, opt Child child1){};

The opt keyword was gone in Beta 2 and later. Probably because the default
values were placed into the IL of the caller and this was deemed to be a Bad
Thing.

-glenn-

"Richard A. Lowe" <cha...@yumspamyumYahoo.com> wrote in message

news:#MtO39t8BHA.2232@tkmsftngp03...

0 new messages