Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. Default parameter in Method

Default parameter in Method

Scheduled Pinned Locked Moved C#
helpquestion
6 Posts 3 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • E Offline
    E Offline
    emran834
    wrote on last edited by
    #1

    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 ?

    S R 2 Replies Last reply
    0
    • E emran834

      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 ?

      S Offline
      S Offline
      S Senthil Kumar
      wrote on last edited by
      #2

      Yes you have to. C# doesn't support default parameters. This[^] FAQ entry explains why. Regards Senthil _____________________________ My Blog | My Articles | WinMacro

      E 1 Reply Last reply
      0
      • E emran834

        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 ?

        R Offline
        R Offline
        ryancrawcour
        wrote on last edited by
        #3

        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...

        E 1 Reply Last reply
        0
        • S S Senthil Kumar

          Yes you have to. C# doesn't support default parameters. This[^] FAQ entry explains why. Regards Senthil _____________________________ My Blog | My Articles | WinMacro

          E Offline
          E Offline
          emran834
          wrote on last edited by
          #4

          thanks Senthil for the article link.

          1 Reply Last reply
          0
          • R ryancrawcour

            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...

            E Offline
            E Offline
            emran834
            wrote on last edited by
            #5

            Thanks ryancrawcour, Although the code looked Scary to me. :-D

            R 1 Reply Last reply
            0
            • E emran834

              Thanks ryancrawcour, Although the code looked Scary to me. :-D

              R Offline
              R Offline
              ryancrawcour
              wrote on last edited by
              #6

              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

              1 Reply Last reply
              0
              Reply
              • Reply as topic
              Log in to reply
              • Oldest to Newest
              • Newest to Oldest
              • Most Votes


              • Login

              • Don't have an account? Register

              • Login or register to search.
              • First post
                Last post
              0
              • Categories
              • Recent
              • Tags
              • Popular
              • World
              • Users
              • Groups