Default parameter in Method
-
Hi, I tried to use a default value of a parameter in a method like this, public void myMethod(bool value=false) { } But, I got error saying "Default parameter is not permitted". I was wondering why it is not permitted, then MUST I have to Overload the method with another copy of the method definision ?
-
Hi, I tried to use a default value of a parameter in a method like this, public void myMethod(bool value=false) { } But, I got error saying "Default parameter is not permitted". I was wondering why it is not permitted, then MUST I have to Overload the method with another copy of the method definision ?
Yes you have to. C# doesn't support default parameters. This[^] FAQ entry explains why. Regards Senthil _____________________________ My Blog | My Articles | WinMacro
-
Hi, I tried to use a default value of a parameter in a method like this, public void myMethod(bool value=false) { } But, I got error saying "Default parameter is not permitted". I was wondering why it is not permitted, then MUST I have to Overload the method with another copy of the method definision ?
you could do something like the following; class ParameterClass { public string Name; public int IDNumber; public ParameterClass() { //set some default values; //since both fields are public they can //be overridden if necessary this.Name = ""; this.IDNumber = 0; } } class OptionalParameters { [STAThread] static void Main(string[] args) { //instantiate a parameter class object //and override the name field ParameterClass c = new ParameterClass(); c.Name = "Lamont Adams"; optionalObject(c); //show that the changed ID came back Console.WriteLine("c.IDNumber={0}",c.IDNumber); //call the method with only defaults optionalObject(new ParameterClass()); //pause so we can see the output Console.ReadLine(); } public static void optionalObject(ParameterClass arg) { //because the parameters received are encapsulated //in an object, they are all optional but have //a valid state even if not explicitly set by the caller Console.WriteLine("arg.Name={0}, arg.IDNumber={1}", arg.Name, arg.IDNumber); //change one of the field values arg.IDNumber = 10; } } this way you don't have to have an overload for every single combination of parameters. hope this helps...
-
Yes you have to. C# doesn't support default parameters. This[^] FAQ entry explains why. Regards Senthil _____________________________ My Blog | My Articles | WinMacro
-
you could do something like the following; class ParameterClass { public string Name; public int IDNumber; public ParameterClass() { //set some default values; //since both fields are public they can //be overridden if necessary this.Name = ""; this.IDNumber = 0; } } class OptionalParameters { [STAThread] static void Main(string[] args) { //instantiate a parameter class object //and override the name field ParameterClass c = new ParameterClass(); c.Name = "Lamont Adams"; optionalObject(c); //show that the changed ID came back Console.WriteLine("c.IDNumber={0}",c.IDNumber); //call the method with only defaults optionalObject(new ParameterClass()); //pause so we can see the output Console.ReadLine(); } public static void optionalObject(ParameterClass arg) { //because the parameters received are encapsulated //in an object, they are all optional but have //a valid state even if not explicitly set by the caller Console.WriteLine("arg.Name={0}, arg.IDNumber={1}", arg.Name, arg.IDNumber); //change one of the field values arg.IDNumber = 10; } } this way you don't have to have an overload for every single combination of parameters. hope this helps...
-
it really isn't very scary ... all you're doing is creating an object (class or struct either way) that contains your parameters. you assign your defaults in your parameter object and pass that into your method. that way you can have one method that has the ability to have default values for a parameter. run the code sample i sent, step through it and you'll see exactly what it is doing ... if you still unclear, let me know and i'll explain it for you line by line ciao RC