Click here to Skip to main content
15,920,896 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have the following code:

FORM
Code:
C#
private void buttonTest_Click(object sender, EventArgs e)
{
      AnalyseBBL analyseBBL = new AnalyseBBL();

      analyseBBL.DBActie(Actie.select, DataBron.Access, 21914546);
      analyseBBL.DBActie(DataSource.Access, 21912817, "UITWBL028", 1, "Balans", "Biovolume", "N", 0.0006, "g"); 
}

AnalyseBBL
Code:
C#
public class AnalyseBBL
    {
       

        public void DBActie(Actie actie, DataBron dataSource, int limsNummer)
        {
            try
            {
                if (actie == Actie.select)
                {                    
                   
                }
                else if (actie == Actie.insert)
                {
                  
                }
            }
            catch
            {
                throw;
            }
        }              
    }

I'm trying to access the DBActie method in the AnalyseBBL class with different parameters. The parameters depends on my action. (Select, Insert)

Example 1:
C#
analyseBBL.DBActie(Actie.select, DataBron.Access, 00000000);


Example 2:
C#
analyseBBL.DBActie(DataSource.Access, 00000000, "Dit is een string", Nummer, "Dit is een string", "Dit is een string", "Nummer");


Actie.select en DataSource.Access are enums.
Posted
Updated 14-Jan-14 23:33pm
v2

You should try optional parameter or named parameter

http://msdn.microsoft.com/en-us/library/dd264739.aspx[^]
 
Share this answer
 
Comments
BillWoodruff 15-Jan-14 8:40am    
+4 you are on the right track with this answer.
Sandeep Singh Shekhawat 15-Jan-14 10:58am    
Thanks dear @BillWoodruff
I think you are asking about method overloading.
Please look at this..

http://msdn.microsoft.com/en-us/library/aa691131(v=vs.71).aspx[^]
http://www.dotnetperls.com/overload[^]
 
Share this answer
 
 
Share this answer
 
There are several ways to do that, for instance, you may define a method

or
  • taking a List of, say MyPar, where each MyPar is an object representing an argument, but carrying as well additional info (like its type, ecc...)

or
  • taking just an object implementing IMyAction interface. This way you would exploit polymorphism for obtaining different operations by the same method called on different kind of objects.
 
Share this answer
 
C# 4.0 enabled optional parameters, which greatly simplify the case where you want to pass arguments in variable order, or pass arguments where some of the arguments are omitted, and you wish to have the inner variables of the Class corresponding to the arguments omitted set to default values.

An important requirement of optional parameters is: if one optional variable has a value declared in the parameter list, then all other optional parameters must also have their values declared: that can be worked around by using the [Optional] Attribute as shown here:
C#
// required
using System.Runtime.InteropServices; // required to use [Optional] Attribute

public class OptionalParameterClassDemo
{
    public int requiredInt = 100;
    public string requiredString = "default string required";

    public int? optionalInt = 999;
    public string optionalString = "default string optional";

    public OptionalParameterClassDemo()
    {

    }

    public OptionalParameterClassDemo(int reqInt, string reqString, [Optional] int? optInt, [Optional] string optString)
    {
        requiredInt = reqInt;
        requiredString = reqString;

        if (optInt != null) optionalInt = optInt;
        if (optString != null) optionalString = optString;
    }
}
I suggest you test the use of the Class like this:
//both optional parameters missing
OptionalParameterClassDemo optTest0 = new OptionalParameterClassDemo(11, "eleven");

// integer optional parameter missing, string optional parameter set
OptionalParameterClassDemo optTest1 = new OptionalParameterClassDemo(100, "one hundred", optString: "777");

// string optional parameter missing, integer optional parameter set
OptionalParameterClassDemo optTest2 = new OptionalParameterClassDemo(100, "one hundred", optInt: 888);
I suggest you try this code, set a breakpoint after the three calls to create an instance of OptionalParameterClassDemo, and examine the internal values in each instance.

Note that you can pass parameter arguments out-of-sequence using this syntax
OptionalParameterClassDemo optTest3 = new OptionalParameterClassDemo(optInt: 888, optString: "some string", reqInt: 1111, reqString: "some required string");
But, in this case, you must specify all arguments.

Note the use of a nullable int in the above code which enables us to test whether the optional integer parameter is supplied in the same way we test if the optional string parameter is supplied.

If we didn't use a nullable int, then we couldn't tell if the parameter was omitted, since an int is automatically initialized to zero: how would we recognize the difference between the user supplying a zero, and the default initialization supplying the zero ?
 
Share this answer
 
v2

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