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 Prameter

Optional Prameter

Scheduled Pinned Locked Moved C#
csharptutorialquestion
10 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.
  • I Offline
    I Offline
    iprasad007
    wrote on last edited by
    #1

    Im a VB.NET programmer currently working with C#, I am having following questions. How to create a function with optional parameters ? Is there any function exist like "CallByName" in VB.NET ?

    C 1 Reply Last reply
    0
    • I iprasad007

      Im a VB.NET programmer currently working with C#, I am having following questions. How to create a function with optional parameters ? Is there any function exist like "CallByName" in VB.NET ?

      C Offline
      C Offline
      Christian Graus
      wrote on last edited by
      #2

      iprasad007 wrote:

      How to create a function with optional parameters ?

      And in a matter of moments, you've found the one thing that VB.NET has and C# does not. I have complained to the C# team, they don't care. So, you need to create two methods, one with the extra param, and one that calls the extra param version with the default. Sucks, I know.

      iprasad007 wrote:

      Is there any function exist like "CallByName" in VB.NET ?

      Depends, what does it do ? Just googled. VB.NET still does that ? That sucks. You should stay clear of anything in the VisualBasic namespace, that's .NET pollution right there. You can use reflection to find methods on an object, perhaps that will do what you need here.

      Christian Graus - Microsoft MVP - C++ Metal Musings - Rex and my new metal blog

      I 1 Reply Last reply
      0
      • C Christian Graus

        iprasad007 wrote:

        How to create a function with optional parameters ?

        And in a matter of moments, you've found the one thing that VB.NET has and C# does not. I have complained to the C# team, they don't care. So, you need to create two methods, one with the extra param, and one that calls the extra param version with the default. Sucks, I know.

        iprasad007 wrote:

        Is there any function exist like "CallByName" in VB.NET ?

        Depends, what does it do ? Just googled. VB.NET still does that ? That sucks. You should stay clear of anything in the VisualBasic namespace, that's .NET pollution right there. You can use reflection to find methods on an object, perhaps that will do what you need here.

        Christian Graus - Microsoft MVP - C++ Metal Musings - Rex and my new metal blog

        I Offline
        I Offline
        iprasad007
        wrote on last edited by
        #3

        Thank you so much Christian. So in first case I need to code same Business Logic twice, or is there any standard method to write the function once and prototyping it many times ? For instance I can write function with all params ones, then can write many functions with same names but with few parameters then of original. But still i need to write code to call original function hence the overhead of maintaining all the functions (while change or documentation). Is this is the last way to do ? And now about my second question, sorry really I didant got what shud I do when I need to call a function or set a property by softcoding such as CallByName does in VB.NET. Waiting for your reply Prasad P.S. BTW ur blog is good

        C 1 Reply Last reply
        0
        • I iprasad007

          Thank you so much Christian. So in first case I need to code same Business Logic twice, or is there any standard method to write the function once and prototyping it many times ? For instance I can write function with all params ones, then can write many functions with same names but with few parameters then of original. But still i need to write code to call original function hence the overhead of maintaining all the functions (while change or documentation). Is this is the last way to do ? And now about my second question, sorry really I didant got what shud I do when I need to call a function or set a property by softcoding such as CallByName does in VB.NET. Waiting for your reply Prasad P.S. BTW ur blog is good

          C Offline
          C Offline
          Christian Graus
          wrote on last edited by
          #4

          iprasad007 wrote:

          P.S. BTW ur blog is good

          *blush* thanks. I need to post something on it tho.

          iprasad007 wrote:

          So in first case I need to code same Business Logic twice, or is there any standard method to write the function once and prototyping it many times ?

          Only ever code your business logic once. Like this int SortStuff(SortBy sortBy, SortOrder sortOrder { // lots of sorting code } int SortStuff(SortBy sortBy) { return SortBy(sortBy, SortOrder.Ascending); } Note, the enums in this method are made up to make the point.

          iprasad007 wrote:

          And now about my second question, sorry really I didant got what shud I do when I need to call a function or set a property by softcoding such as CallByName does in VB.NET.

          Why would you want to do such a thing ? I think it sucks, I don't see how that method is in any way validating that the method in question exists. If you want to discover methods and call them, you need to use reflection ( google reflection C# and you'll see it's a complex subject, but it's exactly what you need here ). Worst case scenario, you can import the VisualBasic dll/namespace into yuor C# app and call the old VB methods, but ideally you shouldn't do that, even if you're using VB.

          Christian Graus - Microsoft MVP - C++ Metal Musings - Rex and my new metal blog

          I 1 Reply Last reply
          0
          • C Christian Graus

            iprasad007 wrote:

            P.S. BTW ur blog is good

            *blush* thanks. I need to post something on it tho.

            iprasad007 wrote:

            So in first case I need to code same Business Logic twice, or is there any standard method to write the function once and prototyping it many times ?

            Only ever code your business logic once. Like this int SortStuff(SortBy sortBy, SortOrder sortOrder { // lots of sorting code } int SortStuff(SortBy sortBy) { return SortBy(sortBy, SortOrder.Ascending); } Note, the enums in this method are made up to make the point.

            iprasad007 wrote:

            And now about my second question, sorry really I didant got what shud I do when I need to call a function or set a property by softcoding such as CallByName does in VB.NET.

            Why would you want to do such a thing ? I think it sucks, I don't see how that method is in any way validating that the method in question exists. If you want to discover methods and call them, you need to use reflection ( google reflection C# and you'll see it's a complex subject, but it's exactly what you need here ). Worst case scenario, you can import the VisualBasic dll/namespace into yuor C# app and call the old VB methods, but ideally you shouldn't do that, even if you're using VB.

            Christian Graus - Microsoft MVP - C++ Metal Musings - Rex and my new metal blog

            I Offline
            I Offline
            iprasad007
            wrote on last edited by
            #5

            Worst case scenario, you can import the VisualBasic dll/namespace into yuor C# app and call the old VB methods, but ideally you shouldn't do that, even if you're using VB. No no I would not do that. I need such functionality for a usual requirement, i.e. to clean all controls in a container. As usual I have wrote a foreach control block for looping through all the controls, depending upon controls type im planning to call its clear method or .Text = "", some controls on my form or container requires some other methods to clear them. Hence needed that function. I think I need to dig in reflection. Thanx for ur replies. (In persuit of reflection) Prasad

            C 1 Reply Last reply
            0
            • I iprasad007

              Worst case scenario, you can import the VisualBasic dll/namespace into yuor C# app and call the old VB methods, but ideally you shouldn't do that, even if you're using VB. No no I would not do that. I need such functionality for a usual requirement, i.e. to clean all controls in a container. As usual I have wrote a foreach control block for looping through all the controls, depending upon controls type im planning to call its clear method or .Text = "", some controls on my form or container requires some other methods to clear them. Hence needed that function. I think I need to dig in reflection. Thanx for ur replies. (In persuit of reflection) Prasad

              C Offline
              C Offline
              Christian Graus
              wrote on last edited by
              #6

              If that's all you need, then you can use as. For example foreach ( Control c in Controls) { TextBox t = c as TextBox; if (t != null ) // c was a TextBox { t.Whateveryoulike } // and so on }

              Christian Graus - Microsoft MVP - C++ Metal Musings - Rex and my new metal blog

              I 1 Reply Last reply
              0
              • C Christian Graus

                If that's all you need, then you can use as. For example foreach ( Control c in Controls) { TextBox t = c as TextBox; if (t != null ) // c was a TextBox { t.Whateveryoulike } // and so on }

                Christian Graus - Microsoft MVP - C++ Metal Musings - Rex and my new metal blog

                I Offline
                I Offline
                iprasad007
                wrote on last edited by
                #7

                Great tip indeed ... I have done by following way, foreach(Control ctl in this.Controls) { if(ctl.GetType() == typeof(TextBox)) { TextBox t=ctl as TextBox; t.Text =""; } else if(ctl.GetType() == typeof(CheckBox)) { CheckBox t=ctl as CheckBox; t.Checked =false; } } Thank you so much for ur kind help, now im feeling that im doing it in right way, and also i havent forgot to write ur name in comment of my code :)(alog with url to this page). (Gr88 C# Expert) Prasad ;) -- modified at 6:37 Thursday 24th August, 2006

                M C 2 Replies Last reply
                0
                • I iprasad007

                  Great tip indeed ... I have done by following way, foreach(Control ctl in this.Controls) { if(ctl.GetType() == typeof(TextBox)) { TextBox t=ctl as TextBox; t.Text =""; } else if(ctl.GetType() == typeof(CheckBox)) { CheckBox t=ctl as CheckBox; t.Checked =false; } } Thank you so much for ur kind help, now im feeling that im doing it in right way, and also i havent forgot to write ur name in comment of my code :)(alog with url to this page). (Gr88 C# Expert) Prasad ;) -- modified at 6:37 Thursday 24th August, 2006

                  M Offline
                  M Offline
                  Martin 0
                  wrote on last edited by
                  #8

                  Hello, You better should go on with what Christian Graus told you. foreach ( Control c in Controls) { TextBox t = c as TextBox; if (t != null ) // c was a TextBox { t.Whateveryoulike } else { CheckBox cb = c as CheckBox; if(cb!=null) { cb...... } } In youre case you ask the Type info 2 times. (takes a little longer) (I also learned it some time ago, from Christian Graus I think) :-D All the best, Martin

                  1 Reply Last reply
                  0
                  • I iprasad007

                    Great tip indeed ... I have done by following way, foreach(Control ctl in this.Controls) { if(ctl.GetType() == typeof(TextBox)) { TextBox t=ctl as TextBox; t.Text =""; } else if(ctl.GetType() == typeof(CheckBox)) { CheckBox t=ctl as CheckBox; t.Checked =false; } } Thank you so much for ur kind help, now im feeling that im doing it in right way, and also i havent forgot to write ur name in comment of my code :)(alog with url to this page). (Gr88 C# Expert) Prasad ;) -- modified at 6:37 Thursday 24th August, 2006

                    C Offline
                    C Offline
                    Christian Graus
                    wrote on last edited by
                    #9

                    As someone said, typeof is kind of redundant. The only reason I can see to use it is that you may be able to switch on ctl.GetType(), and a switch is always better than lots of else if statements. I'm sure that 'as' would use getType, so the most efficient way if a switch won't work would probably be to store the result of typeof(ctl) and use it without calling it every time. -- modified at 7:11 Thursday 24th August, 2006

                    Christian Graus - Microsoft MVP - C++ Metal Musings - Rex and my new metal blog

                    I 1 Reply Last reply
                    0
                    • C Christian Graus

                      As someone said, typeof is kind of redundant. The only reason I can see to use it is that you may be able to switch on ctl.GetType(), and a switch is always better than lots of else if statements. I'm sure that 'as' would use getType, so the most efficient way if a switch won't work would probably be to store the result of typeof(ctl) and use it without calling it every time. -- modified at 7:11 Thursday 24th August, 2006

                      Christian Graus - Microsoft MVP - C++ Metal Musings - Rex and my new metal blog

                      I Offline
                      I Offline
                      iprasad007
                      wrote on last edited by
                      #10

                      Thanx Christian and Martin ... i would do by the way u both suggested .. i understood that it is optimum ... thanx alot (Thinking Outside The Box) Prasad :)

                      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