Optional Prameter
-
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
-
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
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
-
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
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
-
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
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
-
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
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
-
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
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
-
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
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
-
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
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
-
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
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 :)