Click here to Skip to main content
15,891,375 members
Please Sign up or sign in to vote.
4.00/5 (2 votes)
See more:
Hi All,

I want to create a function as below VB Code with optional parameters
VB
Sub FIND_BTN(frm As Form, strTable As String, strCode As String, strDesc As String, ctlCode As String, Optional ctlDesc As Variant, _
    Optional vConn As Variant, Optional vFilter As Variant, Optional vDb As Variant, Optional Title As Variant, Optional ctlTo As Variant, _
    Optional ctlToDesc As Variant, Optional idx As Integer = -1, Optional fpsCtrl, Optional CodeCol As Long, Optional DescCol As Long)


Please help.
Posted
Updated 7-May-11 8:38am
v2

As from .NET 4 you can make
Named and Optional Arguments[^]in method. Try read this link. Perhaps it can help you?
 
Share this answer
 
Comments
Tarun.K.S 7-May-11 13:46pm    
Definitely should! 5+
Kim Togo 7-May-11 14:03pm    
Thanks Tarun. If I remember correct. VB.NET had optional parameter long before C# ?
NuttingCDEF 7-May-11 14:27pm    
That's my recollection too.
NuttingCDEF 7-May-11 14:27pm    
Good answer - my 5.
Kim Togo 7-May-11 14:39pm    
Thanks
Since .Net Framework 4.0 released, C# supports optional parameter.For detail just look Kim Togo link.
 
Share this answer
 
In addition to the answer by Kim:

Before v.4.0, the only way of getting the effect of optional parameters with default was method overloading.
Here is how:

C#
const int DefaultB = 13;

//...

void MethodWithOptionalArguments(int a, int b, string c) { /*...*/ } // full parameter list

void MethodWithOptionalArguments(int a, int b ) { // shortened list
   MethodWithOptionalArguments(a, b, string.Empty);
}
void MethodWithOptionalArguments(int a) { // even shorter
   MethodWithOptionalArguments(a, DefaultB, string.Empty);
}


—SA
 
Share this answer
 
v2
Comments
Kim Togo 9-May-11 3:05am    
My 5. Yes you are right SA. And if there many parameters that you what to have default values in. Then you have to make many overloads of one method. I come from a C++ world and moved to C#. And optional parameters was one that I really misses.
Sergey Alexandrovich Kryukov 9-May-11 3:31am    
And this is the major reason for me to welcome v.4...
--SA

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900