public void FooBar(Int nValue = -1)
{
.....
}
Cheers
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...
"Nicholas Paldino [.NET/C# MVP]" <nicholas...@exisconsulting.com> wrote
in message news:eElc7Af8BHA.2300@tkmsftngp03...
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...
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...
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
"Latha Lakshmi" <Lat...@online.microsoft.com> wrote in message
news:FS#3Q$g8BHA.1532@cpmsftngxa08...
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...
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...
default paramter value != optional parameter
-glenn-
"Latha Lakshmi" <Lat...@online.microsoft.com> wrote in message
news:FS#3Q$g8BHA.1532@cpmsftngxa08...
Richard
"-glenn-" <som...@example.com> wrote in message
news:umCZuyi8BHA.2540@tkmsftngp07...
-glenn-
"Richard A. Lowe" <cha...@yumspamyumYahoo.com> wrote in message
news:uazAhpq8BHA.2256@tkmsftngp02...
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...
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...