optional keyword in c#

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Tapasvi Mehta

    #1

    optional keyword in c#

    how to define the optional parameters for a method or
    function in c# ?

    I am trying to convert an application from VB.NET to c#,
    and don't know how to do the above without overloading.

    Thanks,

    Tapasvi

  • Frank Oquendo

    #2
    Re: optional keyword in c#

    Tapasvi Mehta wrote:[color=blue]
    > how to define the optional parameters for a method or
    > function in c# ?[/color]

    You can't. The closest you can get is a params which allows for an
    arbitrary number of parameters.

    --
    There are 10 kinds of people. Those who understand binary and those who
    don't.


    (Pull the pin to reply)


    Comment

    • Daniel O'Connell

      #3
      Re: optional keyword in c#


      "Tapasvi Mehta" <anonymous@disc ussions.microso ft.com> wrote in message
      news:0c9001c3ae b1$62a76d90$a50 1280a@phx.gbl.. .[color=blue]
      > how to define the optional parameters for a method or
      > function in c# ?
      >
      > I am trying to convert an application from VB.NET to c#,
      > and don't know how to do the above without overloading.
      >[/color]

      You can't, you have to use overloading.
      [color=blue]
      > Thanks,
      >
      > Tapasvi
      >[/color]


      Comment

      • Tapasvi Mehta

        #4
        Re: optional keyword in c#

        Is it possible to default to a value for a parameter ?
        [color=blue]
        >-----Original Message-----
        >
        >"Tapasvi Mehta" <anonymous@disc ussions.microso ft.com>[/color]
        wrote in message[color=blue]
        >news:0c9001c3a eb1$62a76d90$a5 01280a@phx.gbl. ..[color=green]
        >> how to define the optional parameters for a method or
        >> function in c# ?
        >>
        >> I am trying to convert an application from VB.NET to c#,
        >> and don't know how to do the above without overloading.
        >>[/color]
        >
        >You can't, you have to use overloading.
        >[color=green]
        >> Thanks,
        >>
        >> Tapasvi
        >>[/color]
        >
        >
        >.
        >[/color]

        Comment

        • Nicholas Paldino [.NET/C# MVP]

          #5
          Re: optional keyword in c#

          Tapasvi,

          No, it is not. Basically, say you have a method like this:

          public void DoSomething(int myParam)

          If you want to have a default value, you would create an overload that
          does this:

          public void DoSomething()
          {
          // Call the overload, passing a default value of one.
          DoSomething(1);
          }

          Hope this helps.


          --
          - Nicholas Paldino [.NET/C# MVP]
          - mvp@spam.guard. caspershouse.co m

          "Tapasvi Mehta" <anonymous@disc ussions.microso ft.com> wrote in message
          news:0cb701c3ae b5$357d3ee0$a30 1280a@phx.gbl.. .[color=blue]
          > Is it possible to default to a value for a parameter ?
          >[color=green]
          > >-----Original Message-----
          > >
          > >"Tapasvi Mehta" <anonymous@disc ussions.microso ft.com>[/color]
          > wrote in message[color=green]
          > >news:0c9001c3a eb1$62a76d90$a5 01280a@phx.gbl. ..[color=darkred]
          > >> how to define the optional parameters for a method or
          > >> function in c# ?
          > >>
          > >> I am trying to convert an application from VB.NET to c#,
          > >> and don't know how to do the above without overloading.
          > >>[/color]
          > >
          > >You can't, you have to use overloading.
          > >[color=darkred]
          > >> Thanks,
          > >>
          > >> Tapasvi
          > >>[/color]
          > >
          > >
          > >.
          > >[/color][/color]


          Comment

          • Jon Skeet [C# MVP]

            #6
            Re: optional keyword in c#

            Tapasvi Mehta <anonymous@disc ussions.microso ft.com> wrote:[color=blue]
            > Is it possible to default to a value for a parameter ?[/color]

            No. What would be the point, if you then couldn't also make it
            optional?

            --
            Jon Skeet - <skeet@pobox.co m>
            Pobox has been discontinued as a separate service, and all existing customers moved to the Fastmail platform.

            If replying to the group, please do not mail me too

            Comment

            • Tapasvi Mehta

              #7
              Re: optional keyword in c#

              Thanks to all for helping out.[color=blue]
              >-----Original Message-----
              >Tapasvi,
              >
              > No, it is not. Basically, say you have a method like[/color]
              this:[color=blue]
              >
              >public void DoSomething(int myParam)
              >
              > If you want to have a default value, you would create[/color]
              an overload that[color=blue]
              >does this:
              >
              >public void DoSomething()
              >{
              > // Call the overload, passing a default value of one.
              > DoSomething(1);
              >}
              >
              > Hope this helps.
              >
              >
              >--
              > - Nicholas Paldino [.NET/C# MVP]
              > - mvp@spam.guard. caspershouse.co m
              >
              >"Tapasvi Mehta" <anonymous@disc ussions.microso ft.com>[/color]
              wrote in message[color=blue]
              >news:0cb701c3a eb5$357d3ee0$a3 01280a@phx.gbl. ..[color=green]
              >> Is it possible to default to a value for a parameter ?
              >>[color=darkred]
              >> >-----Original Message-----
              >> >
              >> >"Tapasvi Mehta" <anonymous@disc ussions.microso ft.com>[/color]
              >> wrote in message[color=darkred]
              >> >news:0c9001c3a eb1$62a76d90$a5 01280a@phx.gbl. ..
              >> >> how to define the optional parameters for a method or
              >> >> function in c# ?
              >> >>
              >> >> I am trying to convert an application from VB.NET to[/color][/color][/color]
              c#,[color=blue][color=green][color=darkred]
              >> >> and don't know how to do the above without[/color][/color][/color]
              overloading.[color=blue][color=green][color=darkred]
              >> >>
              >> >
              >> >You can't, you have to use overloading.
              >> >
              >> >> Thanks,
              >> >>
              >> >> Tapasvi
              >> >>
              >> >
              >> >
              >> >.
              >> >[/color][/color]
              >
              >
              >.
              >[/color]

              Comment

              Working...