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

Optional Parameter

Scheduled Pinned Locked Moved C#
help
9 Posts 6 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.
  • K Offline
    K Offline
    ksss_maheshece
    wrote on last edited by
    #1

    Hi friends, I need to declere some optional parameter to the function. Is it any possible to fix the problem. Thanks & Regards, MAHESH

    L P V D 4 Replies Last reply
    0
    • K ksss_maheshece

      Hi friends, I need to declere some optional parameter to the function. Is it any possible to fix the problem. Thanks & Regards, MAHESH

      L Offline
      L Offline
      Lost User
      wrote on last edited by
      #2

      what do you mean with "optional parameter" ?

      1 Reply Last reply
      0
      • K ksss_maheshece

        Hi friends, I need to declere some optional parameter to the function. Is it any possible to fix the problem. Thanks & Regards, MAHESH

        P Offline
        P Offline
        Pete OHanlon
        wrote on last edited by
        #3

        Assuming you are talking about C#, optional parameter may mean 2 things: 1. You have method overloads, where certain parameters are defaulted:

        public void DoThis(string name)
        {
        DoThis(name, true);
        }

        public void DoThis(string name, bool someTest)
        {
        // ...
        }

        2. You are talking about a feature that's being introduced in C#4.0, so you can have a signature that looks like this:

        public void DrawMyLine(int x1, int y1, int x2, int y2, Color color = new Color())
        {
        }

        "WPF has many lovers. It's a veritable porn star!" - Josh Smith

        As Braveheart once said, "You can take our freedom but you'll never take our Hobnobs!" - Martin Hughes.

        My blog | My articles | MoXAML PowerToys | Onyx

        K 1 Reply Last reply
        0
        • P Pete OHanlon

          Assuming you are talking about C#, optional parameter may mean 2 things: 1. You have method overloads, where certain parameters are defaulted:

          public void DoThis(string name)
          {
          DoThis(name, true);
          }

          public void DoThis(string name, bool someTest)
          {
          // ...
          }

          2. You are talking about a feature that's being introduced in C#4.0, so you can have a signature that looks like this:

          public void DrawMyLine(int x1, int y1, int x2, int y2, Color color = new Color())
          {
          }

          "WPF has many lovers. It's a veritable porn star!" - Josh Smith

          As Braveheart once said, "You can take our freedom but you'll never take our Hobnobs!" - Martin Hughes.

          My blog | My articles | MoXAML PowerToys | Onyx

          K Offline
          K Offline
          ksss_maheshece
          wrote on last edited by
          #4

          Optional Parameters in sense Default parameter like in VB public myfunction(byval s as string,

          optional

          byval arr string(,))as string in C#: public string myfunction(string QryName, [Optional, DefaultParameterValue({"",""})] string[,] Qry) { .... } I tried to use this code but it shoming error.Here i'll make this Qry as a OPtional one Thanks in advance

          M 1 Reply Last reply
          0
          • K ksss_maheshece

            Optional Parameters in sense Default parameter like in VB public myfunction(byval s as string,

            optional

            byval arr string(,))as string in C#: public string myfunction(string QryName, [Optional, DefaultParameterValue({"",""})] string[,] Qry) { .... } I tried to use this code but it shoming error.Here i'll make this Qry as a OPtional one Thanks in advance

            M Offline
            M Offline
            Mirko1980
            wrote on last edited by
            #5

            This feature will be available only in .NET 4.0. Are you using Visual Studio 2010 Beta? What error are you getting.

            K 1 Reply Last reply
            0
            • M Mirko1980

              This feature will be available only in .NET 4.0. Are you using Visual Studio 2010 Beta? What error are you getting.

              K Offline
              K Offline
              ksss_maheshece
              wrote on last edited by
              #6

              The Error is The type of the argument to the DefaultValue attribute must match the parameter type Thanks

              M 1 Reply Last reply
              0
              • K ksss_maheshece

                The Error is The type of the argument to the DefaultValue attribute must match the parameter type Thanks

                M Offline
                M Offline
                Mirko1980
                wrote on last edited by
                #7

                I don't know much WS2010, yet, but {"", ""} is not a string[,], it's a string[]. Try using something like { { "" } } instead.

                1 Reply Last reply
                0
                • K ksss_maheshece

                  Hi friends, I need to declere some optional parameter to the function. Is it any possible to fix the problem. Thanks & Regards, MAHESH

                  V Offline
                  V Offline
                  Vasudevan Deepak Kumar
                  wrote on last edited by
                  #8

                  C# would not support that but VB.NET has support for that. Your best bet would be to go in for overloads.

                  Vasudevan Deepak Kumar Personal Homepage
                  Tech Gossips
                  The woods are lovely, dark and deep, But I have promises to keep, And miles to go before I sleep, And miles to go before I sleep!

                  1 Reply Last reply
                  0
                  • K ksss_maheshece

                    Hi friends, I need to declere some optional parameter to the function. Is it any possible to fix the problem. Thanks & Regards, MAHESH

                    D Offline
                    D Offline
                    Dave Kreskowiak
                    wrote on last edited by
                    #9

                    Current versions of C# do not support optional parameters. This functionality will not be released until Visual Studio 2010 and .NET 4.0 comes out.

                    A guide to posting questions on CodeProject[^]
                    Dave Kreskowiak Microsoft MVP Visual Developer - Visual Basic
                         2006, 2007, 2008
                    But no longer in 2009...

                    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