Function parameter default value? 
Author Message
 Function parameter default value?

Is it possible in C# smth like default values for parameters (as we had in
C++)

void Function1(int nVar1, char chVar2, int nVar3 = 1)

Is it possible to assign default value in C#?

Thanks.



Mon, 28 Mar 2005 16:04:11 GMT  
 Function parameter default value?
Serge,

Quote:
>Is it possible in C# smth like default values for parameters (as we had in
>C++)

>void Function1(int nVar1, char chVar2, int nVar3 = 1)

>Is it possible to assign default value in C#?

No, you have to use overloading instead:

void Function1(int nVar1, char chVar2) {
  Function1( nVar1, chVar2, 1 );

Quote:
}

void Function1(int nVar1, char chVar2, int nVar3) { ... }

Mattias

===

http://www.msjogren.net/dotnet/
Please reply only to the newsgroup.



Mon, 28 Mar 2005 16:12:16 GMT  
 Function parameter default value?

Quote:
> >Is it possible in C# smth like default values for parameters (as we had
in
> >C++)

> >void Function1(int nVar1, char chVar2, int nVar3 = 1)

You don't have default function parameters but there is a way around to have
default parameters in case of constructors. you can assign values to your
instance variables at the point of declaration itelf. in most of the cases,
this should serve your purpose (the exception being when you have overloaded
c'tor where you want to specify different default values. why you would want
to do that is a diff. question).

regards
 mrinal


Quote:
> Serge,

> >Is it possible in C# smth like default values for parameters (as we had
in
> >C++)

> >void Function1(int nVar1, char chVar2, int nVar3 = 1)

> >Is it possible to assign default value in C#?

> No, you have to use overloading instead:

> void Function1(int nVar1, char chVar2) {
>   Function1( nVar1, chVar2, 1 );
> }

> void Function1(int nVar1, char chVar2, int nVar3) { ... }

> Mattias

> ===

> http://www.msjogren.net/dotnet/
> Please reply only to the newsgroup.



Mon, 28 Mar 2005 20:20:04 GMT  
 
 [ 3 post ] 

 Relevant Pages 

1. function parameters: default value, optional parameter

2. template template parameter with default value (VS.NET 2003)

3. Parameter default values

4. Emulate Named Parameters and Default Values

5. default parameter values

6. Need Default values for Interface Parameters ....

7. WORKAROUND: nested template type default parameter values

8. ActiveX default parameter values

9. HOWTO: Default Parameter values and refs and ADO.NET

10. Default values for parameters?

11. function parameters and return values aren't changed by function call

12. How to declare function with default parameters ?

 

 
Powered by phpBB® Forum Software