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. how can i know whether an optional parameter is passed or not?

how can i know whether an optional parameter is passed or not?

Scheduled Pinned Locked Moved C#
question
20 Posts 11 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.
  • A Offline
    A Offline
    abhiram_nayan
    wrote on last edited by
    #1

    thanks.

    N E D D 5 Replies Last reply
    0
    • A abhiram_nayan

      thanks.

      N Offline
      N Offline
      N a v a n e e t h
      wrote on last edited by
      #2

      How about explaining it in detail?

      Navaneeth How to use google | Ask smart questions

      A 1 Reply Last reply
      0
      • A abhiram_nayan

        thanks.

        E Offline
        E Offline
        Expert Coming
        wrote on last edited by
        #3

        If it is there then it is passed...

        The best way to accelerate a Macintosh is at 9.8m/sec² - Marcus Dolengo

        1 Reply Last reply
        0
        • A abhiram_nayan

          thanks.

          E Offline
          E Offline
          Expert Coming
          wrote on last edited by
          #4

          For a pretty damn perfect explaination MSDN[^]

          The best way to accelerate a Macintosh is at 9.8m/sec² - Marcus Dolengo

          D 1 Reply Last reply
          0
          • A abhiram_nayan

            thanks.

            D Offline
            D Offline
            dan sh
            wrote on last edited by
            #5

            There is nothing like optional parameter in C#.

            Time is the best teacher; unfortunately it kills all of its students. जय हिंद

            M 1 Reply Last reply
            0
            • N N a v a n e e t h

              How about explaining it in detail?

              Navaneeth How to use google | Ask smart questions

              A Offline
              A Offline
              abhiram_nayan
              wrote on last edited by
              #6

              basically, i can achieve it by having an overloaded funciton, but, it is just a small issue that i do not want to have another overload of the function. I am having a funciton with 2 parameters and now i want to introduce a 3rd optional parameter. I just want to know whether the caller of the function passed the 3rd argument or not?

              T D 2 Replies Last reply
              0
              • A abhiram_nayan

                basically, i can achieve it by having an overloaded funciton, but, it is just a small issue that i do not want to have another overload of the function. I am having a funciton with 2 parameters and now i want to introduce a 3rd optional parameter. I just want to know whether the caller of the function passed the 3rd argument or not?

                T Offline
                T Offline
                Tom Deketelaere
                wrote on last edited by
                #7

                Simple When declaring an optional parameter you have to give it a default value. Just check if the value is different than the default, if it is it was passed, if its the same it wasn't.

                A 1 Reply Last reply
                0
                • T Tom Deketelaere

                  Simple When declaring an optional parameter you have to give it a default value. Just check if the value is different than the default, if it is it was passed, if its the same it wasn't.

                  A Offline
                  A Offline
                  abhiram_nayan
                  wrote on last edited by
                  #8

                  dude! Default parameter specifiers are not permitted in C#!!! http://blogs.msdn.com/csharpfaq/archive/2004/03/07/85556.aspx[^]

                  L 1 Reply Last reply
                  0
                  • E Expert Coming

                    For a pretty damn perfect explaination MSDN[^]

                    The best way to accelerate a Macintosh is at 9.8m/sec² - Marcus Dolengo

                    D Offline
                    D Offline
                    Dragonfly_Lee
                    wrote on last edited by
                    #9

                    Hi, dude, optional keyword is for VB.net, not C#.

                    :) I Love KongFu~

                    1 Reply Last reply
                    0
                    • A abhiram_nayan

                      basically, i can achieve it by having an overloaded funciton, but, it is just a small issue that i do not want to have another overload of the function. I am having a funciton with 2 parameters and now i want to introduce a 3rd optional parameter. I just want to know whether the caller of the function passed the 3rd argument or not?

                      D Offline
                      D Offline
                      Dragonfly_Lee
                      wrote on last edited by
                      #10

                      abhiram_nayan wrote:

                      now i want to introduce a 3rd optional parameter.

                      Would you please paste the code? I really do not know how to do this...

                      :) I Love KongFu~

                      1 Reply Last reply
                      0
                      • D dan sh

                        There is nothing like optional parameter in C#.

                        Time is the best teacher; unfortunately it kills all of its students. जय हिंद

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

                        Not until C# 4, at least. (Oook, DaveyM69 posted the same thing at the same time... ;P)

                        1 Reply Last reply
                        0
                        • A abhiram_nayan

                          thanks.

                          D Offline
                          D Offline
                          DaveyM69
                          wrote on last edited by
                          #12

                          Can't do that until C# 4.0 which hasn't been released yet.

                          Dave
                          BTW, in software, hope and pray is not a viable strategy. (Luc Pattyn)
                          Visual Basic is not used by normal people so we're not covering it here. (Uncyclopedia)

                          A A 2 Replies Last reply
                          0
                          • D DaveyM69

                            Can't do that until C# 4.0 which hasn't been released yet.

                            Dave
                            BTW, in software, hope and pray is not a viable strategy. (Luc Pattyn)
                            Visual Basic is not used by normal people so we're not covering it here. (Uncyclopedia)

                            A Offline
                            A Offline
                            abhiram_nayan
                            wrote on last edited by
                            #13

                            this means, i should go with an overloaded function, that's it?!

                            M 1 Reply Last reply
                            0
                            • A abhiram_nayan

                              this means, i should go with an overloaded function, that's it?!

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

                              If there is not anything that stops you from adding another overload (such as conflicting signature with another overload), yes. Alternatively, you can mimic an optional parameter by addin a parameter that accepts null values, and using a default value in the method body if null has been received.

                              1 Reply Last reply
                              0
                              • D DaveyM69

                                Can't do that until C# 4.0 which hasn't been released yet.

                                Dave
                                BTW, in software, hope and pray is not a viable strategy. (Luc Pattyn)
                                Visual Basic is not used by normal people so we're not covering it here. (Uncyclopedia)

                                A Offline
                                A Offline
                                ABitSmart
                                wrote on last edited by
                                #15

                                In reference to C# 4.0, I was just curious, would it be possible to know if an optional parameter was passed or no ? If we did not pass the value, it would take the default value which is defined. But how will the body know if the optional parameter was passed or no ?

                                M 1 Reply Last reply
                                0
                                • A ABitSmart

                                  In reference to C# 4.0, I was just curious, would it be possible to know if an optional parameter was passed or no ? If we did not pass the value, it would take the default value which is defined. But how will the body know if the optional parameter was passed or no ?

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

                                  Well, it will not know. It will use the default value without worring if it was passed by the caller or automatically used. The whole point of optional parameters is to make that logic transparent to the method implementation.

                                  A 1 Reply Last reply
                                  0
                                  • M Mirko1980

                                    Well, it will not know. It will use the default value without worring if it was passed by the caller or automatically used. The whole point of optional parameters is to make that logic transparent to the method implementation.

                                    A Offline
                                    A Offline
                                    ABitSmart
                                    wrote on last edited by
                                    #17

                                    Well, I understand that. Looking at the OP's question and the given reference to C# 4.0 Optional parameters, made me curious.

                                    1 Reply Last reply
                                    0
                                    • A abhiram_nayan

                                      dude! Default parameter specifiers are not permitted in C#!!! http://blogs.msdn.com/csharpfaq/archive/2004/03/07/85556.aspx[^]

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

                                      I don't want to be nit-picky, but C# 4.0 actually does support default parameters :)

                                      N 1 Reply Last reply
                                      0
                                      • L Lost User

                                        I don't want to be nit-picky, but C# 4.0 actually does support default parameters :)

                                        N Offline
                                        N Offline
                                        naresh_prakash
                                        wrote on last edited by
                                        #19

                                        In Microsoft published Framework 2.0, few examples exists when you need to passing optional parameters... i haven't book with me now but i know it is possible. I'll show you examples later bec i already used it.... Thanks Naresh Prakash

                                        L 1 Reply Last reply
                                        0
                                        • N naresh_prakash

                                          In Microsoft published Framework 2.0, few examples exists when you need to passing optional parameters... i haven't book with me now but i know it is possible. I'll show you examples later bec i already used it.... Thanks Naresh Prakash

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

                                          I also heard they were already supported in the .NET 2.0 CLR, but for some reason the features were not exposed to the C# language.

                                          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