No, C# does not give you the ability have optional parameters like in C++ or VB. The best that you could do is function overloading, as Patrick pointed out. -- Chris Rolon This posting is provided "AS IS" with no warranties, and confers no rights. [quoted text, click to view] "Patrick Altman" <paltman.news@gmail.com> wrote in message news:e9Kgci0CFHA.3840@tk2msftngp13.phx.gbl... > Yes. Just overload the method. > > public class MyClass > { > public void MyMethod(int x, int y) > { > ... > } > > public void MyMethod(int x) > { > MyMethod(x, 0); > } > } > > Now you have a class with a "single" method, with two "options" of how > to call it. > > Hope that helps. > > Patrick Altman > <>< > > > Juan wrote: > > Thanks, > > Juan. > > > > > > -- > -- > Patrick Altman > <><
Yes. Just overload the method. public class MyClass { public void MyMethod(int x, int y) { ... } public void MyMethod(int x) { MyMethod(x, 0); } } Now you have a class with a "single" method, with two "options" of how to call it. Hope that helps. Patrick Altman <>< [quoted text, click to view] Juan wrote: > Thanks, > Juan. > >
-- -- Patrick Altman
I see. Thanks a los, i will overload then. "Chris Rolon" <crolon@hotmail.com> escribió en el mensaje news:eMc24D1CFHA.3596@TK2MSFTNGP12.phx.gbl... [quoted text, click to view] > No, C# does not give you the ability have optional parameters like in C++ or > VB. The best that you could do is function overloading, as Patrick pointed > out. > > -- > > Chris Rolon > > This posting is provided "AS IS" with no warranties, and confers no rights. > > "Patrick Altman" <paltman.news@gmail.com> wrote in message > news:e9Kgci0CFHA.3840@tk2msftngp13.phx.gbl... > > Yes. Just overload the method. > > > > public class MyClass > > { > > public void MyMethod(int x, int y) > > { > > ... > > } > > > > public void MyMethod(int x) > > { > > MyMethod(x, 0); > > } > > } > > > > Now you have a class with a "single" method, with two "options" of how > > to call it. > > > > Hope that helps. > > > > Patrick Altman > > <>< > > > > > > Juan wrote: > > > Thanks, > > > Juan. > > > > > > > > > > -- > > -- > > Patrick Altman > > <>< > >
This the most inappropriate use of params I've seen. I assumed there were people that did this sort of thing, but this is the first time I've seen. This is *NOT* how you should use params. Can you? Yes. Should you? No. In fact, if you are going to propose that, why not just have EVERY method be defined like this: int Match(params object[] theobjects) { } int Average(params object[] theobjects) { } int ListNames(params object[] theobjects) { } Why even bother defining specific parameters? I'll tell you why - because it gives the developer a firm/known interface to write to and gaurantees the expected data types and number of parameters. The point is, making a loosely-coupled interface like this is really, really asking for trouble - from the fact that it's not self-documenting, which leads to developer confusion and assumptions (which lead to bugs), to an inherent bug of what if you change the underlying behaviour - but don't need to change the interface? That can and will likely lead to bugs with applications that consume this object. In other words, if I give you an object I made it has: string CalculateItems(params object[] theobjects) You immediately need more information: "What do I pass in there? Does the order matter? What kind of data types?". Versus if I gave you: string CalculateItems (int[] ProductIDs) Then it's very clear (read: "self-documenting") - that this takes an array of ProductIDs.. You see? This is just bad, bad form, please don't encourage people to do this!! If you want loosely coupled, go write in VB6 and make everything a variant!! :-) [quoted text, click to view] "benben" <benhongh@yahoo.com.au> wrote in message news:%231nKifDDFHA.3732@TK2MSFTNGP14.phx.gbl... >I shall disagree with you. C# comes with even better support for variable > number of arguments. It is the params keyword: > > class Demo > { > double Average(params double[] numbers) > { > double total = 0; > > foreach (double i in numbers) > { > total += i; > } > > return total / numbers.Length; > } > > int Match(double target, double error, params double[] candidates) > { > for (int i = 0; i < candidates.Length; ++i) > { > if (target - candiates <= error) > { > return i; > } > } > > return -1; > } > > string ListNames(params object[] objs) > { > string result = ""; > > foreach (object obj in objs) > { > result += obj.ToString(); > result += "\r\n"; > } > > return result; > } > > void Main() > { > double x = Average(1, 2, 3, 4); // yield 2.5 > double y = Average(2, 3, 4, 5); // yield 3.5 > > // using array directly > double z = Average(double[] {3, 4, 5, 6}); // yield 4.5 > > int a = Match(3.1415926, 0.0001, > 1.23456, 2.34567, 3.1416270, 4.7319032); // yield 2 > > // just another version > int b = Match(3.1415926, 0.0001, > double[]{1.23456, 2.34567, 3.1416270, 4.7319032}); > > string whatever("ben", 1, Color.Black, new MyClass()); > > // ... > } > } > >> No, C# does not give you the ability have optional parameters like in C++ > or >> VB. The best that you could do is function overloading, as Patrick >> pointed >> out. >> >> -- >> >> Chris Rolon >> >> This posting is provided "AS IS" with no warranties, and confers no > rights. >> >> "Patrick Altman" <paltman.news@gmail.com> wrote in message >> news:e9Kgci0CFHA.3840@tk2msftngp13.phx.gbl... >> > Yes. Just overload the method. >> > >> > public class MyClass >> > { >> > public void MyMethod(int x, int y) >> > { >> > ... >> > } >> > >> > public void MyMethod(int x) >> > { >> > MyMethod(x, 0); >> > } >> > } >> > >> > Now you have a class with a "single" method, with two "options" of how >> > to call it. >> > >> > Hope that helps. >> > >> > Patrick Altman >> > <>< >> > >> > >> > Juan wrote: >> > > Thanks, >> > > Juan. >> > > >> > > >> > >> > -- >> > -- >> > Patrick Altman >> > <>< >> >> > > >
I shall disagree with you. C# comes with even better support for variable number of arguments. It is the params keyword: class Demo { double Average(params double[] numbers) { double total = 0; foreach (double i in numbers) { total += i; } return total / numbers.Length; } int Match(double target, double error, params double[] candidates) { for (int i = 0; i < candidates.Length; ++i) { if (target - candiates <= error) { return i; } } return -1; } string ListNames(params object[] objs) { string result = ""; foreach (object obj in objs) { result += obj.ToString(); result += "\r\n"; } return result; } void Main() { double x = Average(1, 2, 3, 4); // yield 2.5 double y = Average(2, 3, 4, 5); // yield 3.5 // using array directly double z = Average(double[] {3, 4, 5, 6}); // yield 4.5 int a = Match(3.1415926, 0.0001, 1.23456, 2.34567, 3.1416270, 4.7319032); // yield 2 // just another version int b = Match(3.1415926, 0.0001, double[]{1.23456, 2.34567, 3.1416270, 4.7319032}); string whatever("ben", 1, Color.Black, new MyClass()); // ... } } [quoted text, click to view] > No, C# does not give you the ability have optional parameters like in C++ or > VB. The best that you could do is function overloading, as Patrick pointed > out. > > -- > > Chris Rolon > > This posting is provided "AS IS" with no warranties, and confers no rights. > > "Patrick Altman" <paltman.news@gmail.com> wrote in message > news:e9Kgci0CFHA.3840@tk2msftngp13.phx.gbl... > > Yes. Just overload the method. > > > > public class MyClass > > { > > public void MyMethod(int x, int y) > > { > > ... > > } > > > > public void MyMethod(int x) > > { > > MyMethod(x, 0); > > } > > } > > > > Now you have a class with a "single" method, with two "options" of how > > to call it. > > > > Hope that helps. > > > > Patrick Altman > > <>< > > > > > > Juan wrote: > > > Thanks, > > > Juan. > > > > > > > > > > -- > > -- > > Patrick Altman > > <>< > >
I shall disagree with you. C# comes with even better support for variable number of arguments. It is the params keyword: class Demo { double Average(params double[] numbers) { double total = 0; foreach (double i in numbers) { total += i; } return total / numbers.Length; } int Match(double target, double error, params double[] candidates) { for (int i = 0; i < candidates.Length; ++i) { if (target - candiates <= error) { return i; } } return -1; } string ListNames(params object[] objs) { string result = ""; foreach (object obj in objs) { result += obj.ToString(); result += "\r\n"; } return result; } void Main() { double x = Average(1, 2, 3, 4); // yield 2.5 double y = Average(2, 3, 4, 5); // yield 3.5 // using array directly double z = Average(double[] {3, 4, 5, 6}); // yield 4.5 int a = Match(3.1415926, 0.0001, 1.23456, 2.34567, 3.1416270, 4.7319032); // yield 2 // just another version int b = Match(3.1415926, 0.0001, double[]{1.23456, 2.34567, 3.1416270, 4.7319032}); string whatever("ben", 1, Color.Black, new MyClass()); // ... } } [quoted text, click to view] > No, C# does not give you the ability have optional parameters like in C++ or > VB. The best that you could do is function overloading, as Patrick pointed > out. > > -- > > Chris Rolon > > This posting is provided "AS IS" with no warranties, and confers no rights. > > "Patrick Altman" <paltman.news@gmail.com> wrote in message > news:e9Kgci0CFHA.3840@tk2msftngp13.phx.gbl... > > Yes. Just overload the method. > > > > public class MyClass > > { > > public void MyMethod(int x, int y) > > { > > ... > > } > > > > public void MyMethod(int x) > > { > > MyMethod(x, 0); > > } > > } > > > > Now you have a class with a "single" method, with two "options" of how > > to call it. > > > > Hope that helps. > > > > Patrick Altman > > <>< > > > > > > Juan wrote: > > > Thanks, > > > Juan. > > > > > > > > > > -- > > -- > > Patrick Altman > > <>< > >
As has been pointed out, your use of params is inappropriate. C# does not support default arguments, ala C++, which I believe is what Juan was asking. -- Chris Rolon [quoted text, click to view] "RCS" <rseder@gmail.com> wrote in message news:dYrNd.26722$by5.20218@newssvr19.news.prodigy.com... > This the most inappropriate use of params I've seen. I assumed there were > people that did this sort of thing, but this is the first time I've seen. > > This is *NOT* how you should use params. Can you? Yes. Should you? No. > > In fact, if you are going to propose that, why not just have EVERY method be > defined like this: > > > int Match(params object[] theobjects) > { } > > int Average(params object[] theobjects) > { } > > int ListNames(params object[] theobjects) > { } > > Why even bother defining specific parameters? I'll tell you why - because it > gives the developer a firm/known interface to write to and gaurantees the > expected data types and number of parameters. > > The point is, making a loosely-coupled interface like this is really, really > asking for trouble - from the fact that it's not self-documenting, which > leads to developer confusion and assumptions (which lead to bugs), to an > inherent bug of what if you change the underlying behaviour - but don't need > to change the interface? That can and will likely lead to bugs with > applications that consume this object. > > In other words, if I give you an object I made it has: > > string CalculateItems(params object[] theobjects) > > You immediately need more information: "What do I pass in there? Does the > order matter? What kind of data types?". Versus if I gave you: > > string CalculateItems (int[] ProductIDs) > > Then it's very clear (read: "self-documenting") - that this takes an array > of ProductIDs.. You see? > > > This is just bad, bad form, please don't encourage people to do this!! If > you want loosely coupled, go write in VB6 and make everything a variant!! > :-) > > > > "benben" <benhongh@yahoo.com.au> wrote in message > news:%231nKifDDFHA.3732@TK2MSFTNGP14.phx.gbl... > >I shall disagree with you. C# comes with even better support for variable > > number of arguments. It is the params keyword: > > > > class Demo > > { > > double Average(params double[] numbers) > > { > > double total = 0; > > > > foreach (double i in numbers) > > { > > total += i; > > } > > > > return total / numbers.Length; > > } > > > > int Match(double target, double error, params double[] candidates) > > { > > for (int i = 0; i < candidates.Length; ++i) > > { > > if (target - candiates <= error) > > { > > return i; > > } > > } > > > > return -1; > > } > > > > string ListNames(params object[] objs) > > { > > string result = ""; > > > > foreach (object obj in objs) > > { > > result += obj.ToString(); > > result += "\r\n"; > > } > > > > return result; > > } > > > > void Main() > > { > > double x = Average(1, 2, 3, 4); // yield 2.5 > > double y = Average(2, 3, 4, 5); // yield 3.5 > > > > // using array directly > > double z = Average(double[] {3, 4, 5, 6}); // yield 4.5 > > > > int a = Match(3.1415926, 0.0001, > > 1.23456, 2.34567, 3.1416270, 4.7319032); // yield 2 > > > > // just another version > > int b = Match(3.1415926, 0.0001, > > double[]{1.23456, 2.34567, 3.1416270, 4.7319032}); > > > > string whatever("ben", 1, Color.Black, new MyClass()); > > > > // ... > > } > > } > > > >> No, C# does not give you the ability have optional parameters like in C++ > > or > >> VB. The best that you could do is function overloading, as Patrick > >> pointed > >> out. > >> > >> -- > >> > >> Chris Rolon > >> > >> This posting is provided "AS IS" with no warranties, and confers no > > rights. > >> > >> "Patrick Altman" <paltman.news@gmail.com> wrote in message > >> news:e9Kgci0CFHA.3840@tk2msftngp13.phx.gbl... > >> > Yes. Just overload the method. > >> > > >> > public class MyClass > >> > { > >> > public void MyMethod(int x, int y) > >> > { > >> > ... > >> > } > >> > > >> > public void MyMethod(int x) > >> > { > >> > MyMethod(x, 0); > >> > } > >> > } > >> > > >> > Now you have a class with a "single" method, with two "options" of how > >> > to call it. > >> > > >> > Hope that helps. > >> > > >> > Patrick Altman > >> > <>< > >> > > >> > > >> > Juan wrote: > >> > > Thanks, > >> > > Juan. > >> > > > >> > > > >> > > >> > -- > >> > -- > >> > Patrick Altman > >> > <>< > >> > >> > > > > > > > >
Well yeah, there are definitely appropriate places for using params, but in user functions as described, I don't think it's proper. In places like print() or string.Format() it does make sense, because you specifically don't care what or how many parameters are sent in. In most everything the average developer does, this is NOT the case - that's all I'm saying. I'm not saying params is inherently bad, just the overuse of them in regular code where a cleaner alternative is available. [quoted text, click to view] "benben" <benhongh@yahoo.com.au> wrote in message news:OIjQIDdDFHA.4052@TK2MSFTNGP15.phx.gbl... > > >> This the most inappropriate use of params I've seen. I assumed there were >> people that did this sort of thing, but this is the first time I've seen. >> >> This is *NOT* how you should use params. Can you? Yes. Should you? No. > > > It is quite up to the implementor to decide the STYLE of the interface. In > those C days, we were too familiar with the printf() function, which > exactly > takes variable parameters. They were designed and implemented and used, > now > it is up to you to critique the design. But anyway, it is an option. > > It is true that the overuse of params will lead to ugly designs. > >> >> In fact, if you are going to propose that, why not just have EVERY method > be >> defined like this: >> >> >> int Match(params object[] theobjects) >> { } >> >> int Average(params object[] theobjects) >> { } >> >> int ListNames(params object[] theobjects) >> { } > > Because they won't make sense. Take the Average example. Its semantics > says > "calculate the average of a bundle of numbers", notice the "a bundle of > numbes", not necessarily "a bundle of objects, any objects". > >> >> Why even bother defining specific parameters? I'll tell you why - because > it >> gives the developer a firm/known interface to write to and gaurantees the >> expected data types and number of parameters. >> >> The point is, making a loosely-coupled interface like this is really, > really >> asking for trouble - from the fact that it's not self-documenting, which >> leads to developer confusion and assumptions (which lead to bugs), to an >> inherent bug of what if you change the underlying behaviour - but don't > need >> to change the interface? That can and will likely lead to bugs with >> applications that consume this object. >> >> In other words, if I give you an object I made it has: >> >> string CalculateItems(params object[] theobjects) >> >> You immediately need more information: "What do I pass in there? Does the >> order matter? What kind of data types?". Versus if I gave you: > > the params operator is only used to expand an array at call site. so > instead > of passing an array as a single parameter, you now have a chance to pass > in > a number of parameters to make the invocation look more natural. > > notice I didn't simple use params object[], so "what kind of data types" > are > answered in the declaration. > > the order doesn't matter. Otherwise params won't be used here. What is the > difference of Average(1,2,3,4) and Average(2,4,1,3)? > >> >> string CalculateItems (int[] ProductIDs) >> >> Then it's very clear (read: "self-documenting") - that this takes an >> array >> of ProductIDs.. You see? >> >> >> This is just bad, bad form, please don't encourage people to do this!! If >> you want loosely coupled, go write in VB6 and make everything a variant!! >> :-) >> > > I don't encourage people to do this. I only treat the introduction of > params > as a syntactic sugar. notice that: > > string CalculateItems (params int[] ProductIDs) > > is just compatible with > > string CalculateItems (int[] ProductIDs) > > as I said, the additional params operator is only to allow the caller to > pass in an array without openning a new line to declare the array. > >> >> >> "benben" <benhongh@yahoo.com.au> wrote in message >> news:%231nKifDDFHA.3732@TK2MSFTNGP14.phx.gbl... >> >I shall disagree with you. C# comes with even better support for >> >variable >> > number of arguments. It is the params keyword: >> > >> > class Demo >> > { >> > double Average(params double[] numbers) >> > { >> > double total = 0; >> > >> > foreach (double i in numbers) >> > { >> > total += i; >> > } >> > >> > return total / numbers.Length; >> > } >> > >> > int Match(double target, double error, params double[] candidates) >> > { >> > for (int i = 0; i < candidates.Length; ++i) >> > { >> > if (target - candiates <= error) >> > { >> > return i; >> > } >> > } >> > >> > return -1; >> > } >> > >> > string ListNames(params object[] objs) >> > { >> > string result = ""; >> > >> > foreach (object obj in objs) >> > { >> > result += obj.ToString(); >> > result += "\r\n"; >> > } >> > >> > return result; >> > } >> > >> > void Main() >> > { >> > double x = Average(1, 2, 3, 4); // yield 2.5 >> > double y = Average(2, 3, 4, 5); // yield 3.5 >> > >> > // using array directly >> > double z = Average(double[] {3, 4, 5, 6}); // yield 4.5 >> > >> > int a = Match(3.1415926, 0.0001, >> > 1.23456, 2.34567, 3.1416270, 4.7319032); // yield 2 >> > >> > // just another version >> > int b = Match(3.1415926, 0.0001, >> > double[]{1.23456, 2.34567, 3.1416270, 4.7319032}); >> > >> > string whatever("ben", 1, Color.Black, new MyClass()); >> > >> > // ... >> > } >> > } >> > >> >> No, C# does not give you the ability have optional parameters like in > C++ >> > or >> >> VB. The best that you could do is function overloading, as Patrick >> >> pointed >> >> out. >> >> >> >> -- >> >> >> >> Chris Rolon >> >> >> >> This posting is provided "AS IS" with no warranties, and confers no >> > rights. >> >> >> >> "Patrick Altman" <paltman.news@gmail.com> wrote in message >> >> news:e9Kgci0CFHA.3840@tk2msftngp13.phx.gbl... >> >> > Yes. Just overload the method. >> >> > >> >> > public class MyClass >> >> > { >> >> > public void MyMethod(int x, int y) >> >> > { >> >> > ... >> >> > } >> >> > >> >> > public void MyMethod(int x) >> >> > { >> >> > MyMethod(x, 0); >> >> > } >> >> > } >> >> > >> >> > Now you have a class with a "single" method, with two "options" of > how >> >> > to call it. >> >> > >> >> > Hope that helps. >> >> > >> >> > Patrick Altman >> >> > <>< >> >> > >> >> > >> >> > Juan wrote: >> >> > > Thanks, >> >> > > Juan. >> >> > > >> >> > > >> >> > >> >> > -- >> >> > -- >> >> > Patrick Altman >> >> > <>< >> >> >> >> >> > >> > >> > >> >> > >
String.Format comes leaping to mind as a good example of the use of params. [quoted text, click to view] "benben" <benhongh@yahoo.com.au> wrote in message news:%23MaicFdDFHA.3340@TK2MSFTNGP10.phx.gbl... > But after some afterthoughts I reason that there are situations when/where > the use of params could be appropriate. > > It is only telling people, "hey, you can pass in an array, but if you are > too lazy to start a new line to declare that array, don't bother then, > just > pass in the collection as separate parameters." > > ben > >> I swear to God that I NEVER used params in my formal projects. Yes you >> are >> right, it leads to less elegant designs. >> >> I thought the OP was requesting something like the ... operator in > parameter >> list so I flip though the C# book and found it. >> >> ben >> >> > As has been pointed out, your use of params is inappropriate. C# does > not >> > support default arguments, ala C++, which I believe is what Juan was >> asking. >> > >> > -- >> > >> > Chris Rolon >> > >> > >> > "RCS" <rseder@gmail.com> wrote in message >> > news:dYrNd.26722$by5.20218@newssvr19.news.prodigy.com... >> > > This the most inappropriate use of params I've seen. I assumed there >> were >> > > people that did this sort of thing, but this is the first time I've >> seen. >> > > >> > > This is *NOT* how you should use params. Can you? Yes. Should you? >> > > No. >> > > >> > > In fact, if you are going to propose that, why not just have EVERY >> method >> > be >> > > defined like this: >> > > >> > > >> > > int Match(params object[] theobjects) >> > > { } >> > > >> > > int Average(params object[] theobjects) >> > > { } >> > > >> > > int ListNames(params object[] theobjects) >> > > { } >> > > >> > > Why even bother defining specific parameters? I'll tell you why - >> because >> > it >> > > gives the developer a firm/known interface to write to and gaurantees >> the >> > > expected data types and number of parameters. >> > > >> > > The point is, making a loosely-coupled interface like this is really, >> > really >> > > asking for trouble - from the fact that it's not self-documenting, > which >> > > leads to developer confusion and assumptions (which lead to bugs), to > an >> > > inherent bug of what if you change the underlying behaviour - but > don't >> > need >> > > to change the interface? That can and will likely lead to bugs with >> > > applications that consume this object. >> > > >> > > In other words, if I give you an object I made it has: >> > > >> > > string CalculateItems(params object[] theobjects) >> > > >> > > You immediately need more information: "What do I pass in there? Does >> the >> > > order matter? What kind of data types?". Versus if I gave you: >> > > >> > > string CalculateItems (int[] ProductIDs) >> > > >> > > Then it's very clear (read: "self-documenting") - that this takes an >> array >> > > of ProductIDs.. You see? >> > > >> > > >> > > This is just bad, bad form, please don't encourage people to do >> > > this!! >> If >> > > you want loosely coupled, go write in VB6 and make everything a >> variant!! >> > > :-) >> > > >> > > >> > > >> > > "benben" <benhongh@yahoo.com.au> wrote in message >> > > news:%231nKifDDFHA.3732@TK2MSFTNGP14.phx.gbl... >> > > >I shall disagree with you. C# comes with even better support for >> variable >> > > > number of arguments. It is the params keyword: >> > > > >> > > > class Demo >> > > > { >> > > > double Average(params double[] numbers) >> > > > { >> > > > double total = 0; >> > > > >> > > > foreach (double i in numbers) >> > > > { >> > > > total += i; >> > > > } >> > > > >> > > > return total / numbers.Length; >> > > > } >> > > > >> > > > int Match(double target, double error, params double[] > candidates) >> > > > { >> > > > for (int i = 0; i < candidates.Length; ++i) >> > > > { >> > > > if (target - candiates <= error) >> > > > { >> > > > return i; >> > > > } >> > > > } >> > > > >> > > > return -1; >> > > > } >> > > > >> > > > string ListNames(params object[] objs) >> > > > { >> > > > string result = ""; >> > > > >> > > > foreach (object obj in objs) >> > > > { >> > > > result += obj.ToString(); >> > > > result += "\r\n"; >> > > > } >> > > > >> > > > return result; >> > > > } >> > > > >> > > > void Main() >> > > > { >> > > > double x = Average(1, 2, 3, 4); // yield 2.5 >> > > > double y = Average(2, 3, 4, 5); // yield 3.5 >> > > > >> > > > // using array directly >> > > > double z = Average(double[] {3, 4, 5, 6}); // yield 4.5 >> > > > >> > > > int a = Match(3.1415926, 0.0001, >> > > > 1.23456, 2.34567, 3.1416270, 4.7319032); // yield 2 >> > > > >> > > > // just another version >> > > > int b = Match(3.1415926, 0.0001, >> > > > double[]{1.23456, 2.34567, 3.1416270, 4.7319032}); >> > > > >> > > > string whatever("ben", 1, Color.Black, new MyClass()); >> > > > >> > > > // ... >> > > > } >> > > > } >> > > > >> > > >> No, C# does not give you the ability have optional parameters like > in >> > C++ >> > > > or >> > > >> VB. The best that you could do is function overloading, as Patrick >> > > >> pointed >> > > >> out. >> > > >> >> > > >> -- >> > > >> >> > > >> Chris Rolon >> > > >> >> > > >> This posting is provided "AS IS" with no warranties, and confers >> > > >> no >> > > > rights. >> > > >> >> > > >> "Patrick Altman" <paltman.news@gmail.com> wrote in message >> > > >> news:e9Kgci0CFHA.3840@tk2msftngp13.phx.gbl... >> > > >> > Yes. Just overload the method. >> > > >> > >> > > >> > public class MyClass >> > > >> > { >> > > >> > public void MyMethod(int x, int y) >> > > >> > { >> > > >> > ... >> > > >> > } >> > > >> > >> > > >> > public void MyMethod(int x) >> > > >> > { >> > > >> > MyMethod(x, 0); >> > > >> > } >> > > >> > } >> > > >> > >> > > >> > Now you have a class with a "single" method, with two "options" > of >> > how >> > > >> > to call it. >> > > >> > >> > > >> > Hope that helps. >> > > >> > >> > > >> > Patrick Altman >> > > >> > <>< >> > > >> > >> > > >> > >> > > >> > Juan wrote: >> > > >> > > Thanks, >> > > >> > > Juan. >> > > >> > > >> > > >> > > >> > > >> > >> > > >> > -- >> > > >> > -- >> > > >> > Patrick Altman >> > > >> > <>< >> > > >> >> > > >> >> > > > >> > > > >> > > > >> > > >> > > >> > >> > >> >> > >
I swear to God that I NEVER used params in my formal projects. Yes you are right, it leads to less elegant designs. I thought the OP was requesting something like the ... operator in parameter list so I flip though the C# book and found it. ben [quoted text, click to view] > As has been pointed out, your use of params is inappropriate. C# does not > support default arguments, ala C++, which I believe is what Juan was asking. > > -- > > Chris Rolon > > > "RCS" <rseder@gmail.com> wrote in message > news:dYrNd.26722$by5.20218@newssvr19.news.prodigy.com... > > This the most inappropriate use of params I've seen. I assumed there were > > people that did this sort of thing, but this is the first time I've seen. > > > > This is *NOT* how you should use params. Can you? Yes. Should you? No. > > > > In fact, if you are going to propose that, why not just have EVERY method > be > > defined like this: > > > > > > int Match(params object[] theobjects) > > { } > > > > int Average(params object[] theobjects) > > { } > > > > int ListNames(params object[] theobjects) > > { } > > > > Why even bother defining specific parameters? I'll tell you why - because > it > > gives the developer a firm/known interface to write to and gaurantees the > > expected data types and number of parameters. > > > > The point is, making a loosely-coupled interface like this is really, > really > > asking for trouble - from the fact that it's not self-documenting, which > > leads to developer confusion and assumptions (which lead to bugs), to an > > inherent bug of what if you change the underlying behaviour - but don't > need > > to change the interface? That can and will likely lead to bugs with > > applications that consume this object. > > > > In other words, if I give you an object I made it has: > > > > string CalculateItems(params object[] theobjects) > > > > You immediately need more information: "What do I pass in there? Does the > > order matter? What kind of data types?". Versus if I gave you: > > > > string CalculateItems (int[] ProductIDs) > > > > Then it's very clear (read: "self-documenting") - that this takes an array > > of ProductIDs.. You see? > > > > > > This is just bad, bad form, please don't encourage people to do this!! If > > you want loosely coupled, go write in VB6 and make everything a variant!! > > :-) > > > > > > > > "benben" <benhongh@yahoo.com.au> wrote in message > > news:%231nKifDDFHA.3732@TK2MSFTNGP14.phx.gbl... > > >I shall disagree with you. C# comes with even better support for variable > > > number of arguments. It is the params keyword: > > > > > > class Demo > > > { > > > double Average(params double[] numbers) > > > { > > > double total = 0; > > > > > > foreach (double i in numbers) > > > { > > > total += i; > > > } > > > > > > return total / numbers.Length; > > > } > > > > > > int Match(double target, double error, params double[] candidates) > > > { > > > for (int i = 0; i < candidates.Length; ++i) > > > { > > > if (target - candiates <= error) > > > { > > > return i; > > > } > > > } > > > > > > return -1; > > > } > > > > > > string ListNames(params object[] objs) > > > { > > > string result = ""; > > > > > > foreach (object obj in objs) > > > { > > > result += obj.ToString(); > > > result += "\r\n"; > > > } > > > > > > return result; > > > } > > > > > > void Main() > > > { > > > double x = Average(1, 2, 3, 4); // yield 2.5 > > > double y = Average(2, 3, 4, 5); // yield 3.5 > > > > > > // using array directly > > > double z = Average(double[] {3, 4, 5, 6}); // yield 4.5 > > > > > > int a = Match(3.1415926, 0.0001, > > > 1.23456, 2.34567, 3.1416270, 4.7319032); // yield 2 > > > > > > // just another version > > > int b = Match(3.1415926, 0.0001, > > > double[]{1.23456, 2.34567, 3.1416270, 4.7319032}); > > > > > > string whatever("ben", 1, Color.Black, new MyClass()); > > > > > > // ... > > > } > > > } > > > > > >> No, C# does not give you the ability have optional parameters like in > C++ > > > or > > >> VB. The best that you could do is function overloading, as Patrick > > >> pointed > > >> out. > > >> > > >> -- > > >> > > >> Chris Rolon > > >> > > >> This posting is provided "AS IS" with no warranties, and confers no > > > rights. > > >> > > >> "Patrick Altman" <paltman.news@gmail.com> wrote in message > > >> news:e9Kgci0CFHA.3840@tk2msftngp13.phx.gbl... > > >> > Yes. Just overload the method. > > >> > > > >> > public class MyClass > > >> > { > > >> > public void MyMethod(int x, int y) > > >> > { > > >> > ... > > >> > } > > >> > > > >> > public void MyMethod(int x) > > >> > { > > >> > MyMethod(x, 0); > > >> > } > > >> > } > > >> > > > >> > Now you have a class with a "single" method, with two "options" of > how > > >> > to call it. > > >> > > > >> > Hope that helps. > > >> > > > >> > Patrick Altman > > >> > <>< > > >> > > > >> > > > >> > Juan wrote: > > >> > > Thanks, > > >> > > Juan. > > >> > > > > >> > > > > >> > > > >> > -- > > >> > -- > > >> > Patrick Altman > > >> > <>< > > >> > > >> > > > > > > > > > > > > > > >
[quoted text, click to view] > This the most inappropriate use of params I've seen. I assumed there were > people that did this sort of thing, but this is the first time I've seen. > > This is *NOT* how you should use params. Can you? Yes. Should you? No.
It is quite up to the implementor to decide the STYLE of the interface. In those C days, we were too familiar with the printf() function, which exactly takes variable parameters. They were designed and implemented and used, now it is up to you to critique the design. But anyway, it is an option. It is true that the overuse of params will lead to ugly designs. [quoted text, click to view] > > In fact, if you are going to propose that, why not just have EVERY method be > defined like this: > > > int Match(params object[] theobjects) > { } > > int Average(params object[] theobjects) > { } > > int ListNames(params object[] theobjects) > { }
Because they won't make sense. Take the Average example. Its semantics says "calculate the average of a bundle of numbers", notice the "a bundle of numbes", not necessarily "a bundle of objects, any objects". [quoted text, click to view] > > Why even bother defining specific parameters? I'll tell you why - because it > gives the developer a firm/known interface to write to and gaurantees the > expected data types and number of parameters. > > The point is, making a loosely-coupled interface like this is really, really > asking for trouble - from the fact that it's not self-documenting, which > leads to developer confusion and assumptions (which lead to bugs), to an > inherent bug of what if you change the underlying behaviour - but don't need > to change the interface? That can and will likely lead to bugs with > applications that consume this object. > > In other words, if I give you an object I made it has: > > string CalculateItems(params object[] theobjects) > > You immediately need more information: "What do I pass in there? Does the > order matter? What kind of data types?". Versus if I gave you:
the params operator is only used to expand an array at call site. so instead of passing an array as a single parameter, you now have a chance to pass in a number of parameters to make the invocation look more natural. notice I didn't simple use params object[], so "what kind of data types" are answered in the declaration. the order doesn't matter. Otherwise params won't be used here. What is the difference of Average(1,2,3,4) and Average(2,4,1,3)? [quoted text, click to view] > > string CalculateItems (int[] ProductIDs) > > Then it's very clear (read: "self-documenting") - that this takes an array > of ProductIDs.. You see? > > > This is just bad, bad form, please don't encourage people to do this!! If > you want loosely coupled, go write in VB6 and make everything a variant!! > :-) >
I don't encourage people to do this. I only treat the introduction of params as a syntactic sugar. notice that: string CalculateItems (params int[] ProductIDs) is just compatible with string CalculateItems (int[] ProductIDs) as I said, the additional params operator is only to allow the caller to pass in an array without openning a new line to declare the array. [quoted text, click to view] > > > "benben" <benhongh@yahoo.com.au> wrote in message > news:%231nKifDDFHA.3732@TK2MSFTNGP14.phx.gbl... > >I shall disagree with you. C# comes with even better support for variable > > number of arguments. It is the params keyword: > > > > class Demo > > { > > double Average(params double[] numbers) > > { > > double total = 0; > > > > foreach (double i in numbers) > > { > > total += i; > > } > > > > return total / numbers.Length; > > } > > > > int Match(double target, double error, params double[] candidates) > > { > > for (int i = 0; i < candidates.Length; ++i) > > { > > if (target - candiates <= error) > > { > > return i; > > } > > } > > > > return -1; > > } > > > > string ListNames(params object[] objs) > > { > > string result = ""; > > > > foreach (object obj in objs) > > { > > result += obj.ToString(); > > result += "\r\n"; > > } > > > > return result; > > } > > > > void Main() > > { > > double x = Average(1, 2, 3, 4); // yield 2.5 > > double y = Average(2, 3, 4, 5); // yield 3.5 > > > > // using array directly > > double z = Average(double[] {3, 4, 5, 6}); // yield 4.5 > > > > int a = Match(3.1415926, 0.0001, > > 1.23456, 2.34567, 3.1416270, 4.7319032); // yield 2 > > > > // just another version > > int b = Match(3.1415926, 0.0001, > > double[]{1.23456, 2.34567, 3.1416270, 4.7319032}); > > > > string whatever("ben", 1, Color.Black, new MyClass()); > > > > // ... > > } > > } > > > >> No, C# does not give you the ability have optional parameters like in C++ > > or > >> VB. The best that you could do is function overloading, as Patrick > >> pointed > >> out. > >> > >> -- > >> > >> Chris Rolon > >> > >> This posting is provided "AS IS" with no warranties, and confers no > > rights. > >> > >> "Patrick Altman" <paltman.news@gmail.com> wrote in message > >> news:e9Kgci0CFHA.3840@tk2msftngp13.phx.gbl... > >> > Yes. Just overload the method. > >> > > >> > public class MyClass > >> > { > >> > public void MyMethod(int x, int y) > >> > { > >> > ... > >> > } > >> > > >> > public void MyMethod(int x) > >> > { > >> > MyMethod(x, 0); > >> > } > >> > } > >> > > >> > Now you have a class with a "single" method, with two "options" of how > >> > to call it. > >> > > >> > Hope that helps. > >> > > >> > Patrick Altman > >> > <>< > >> > > >> > > >> > Juan wrote: > >> > > Thanks, > >> > > Juan. > >> > > > >> > > > >> > > >> > -- > >> > -- > >> > Patrick Altman > >> > <>< > >> > >> > > > > > > > >
But after some afterthoughts I reason that there are situations when/where the use of params could be appropriate. It is only telling people, "hey, you can pass in an array, but if you are too lazy to start a new line to declare that array, don't bother then, just pass in the collection as separate parameters." ben [quoted text, click to view] > I swear to God that I NEVER used params in my formal projects. Yes you are > right, it leads to less elegant designs. > > I thought the OP was requesting something like the ... operator in parameter > list so I flip though the C# book and found it. > > ben > > > As has been pointed out, your use of params is inappropriate. C# does not > > support default arguments, ala C++, which I believe is what Juan was > asking. > > > > -- > > > > Chris Rolon > > > > > > "RCS" <rseder@gmail.com> wrote in message > > news:dYrNd.26722$by5.20218@newssvr19.news.prodigy.com... > > > This the most inappropriate use of params I've seen. I assumed there > were > > > people that did this sort of thing, but this is the first time I've > seen. > > > > > > This is *NOT* how you should use params. Can you? Yes. Should you? No. > > > > > > In fact, if you are going to propose that, why not just have EVERY > method > > be > > > defined like this: > > > > > > > > > int Match(params object[] theobjects) > > > { } > > > > > > int Average(params object[] theobjects) > > > { } > > > > > > int ListNames(params object[] theobjects) > > > { } > > > > > > Why even bother defining specific parameters? I'll tell you why - > because > > it > > > gives the developer a firm/known interface to write to and gaurantees > the > > > expected data types and number of parameters. > > > > > > The point is, making a loosely-coupled interface like this is really, > > really > > > asking for trouble - from the fact that it's not self-documenting, which > > > leads to developer confusion and assumptions (which lead to bugs), to an > > > inherent bug of what if you change the underlying behaviour - but don't > > need > > > to change the interface? That can and will likely lead to bugs with > > > applications that consume this object. > > > > > > In other words, if I give you an object I made it has: > > > > > > string CalculateItems(params object[] theobjects) > > > > > > You immediately need more information: "What do I pass in there? Does > the > > > order matter? What kind of data types?". Versus if I gave you: > > > > > > string CalculateItems (int[] ProductIDs) > > > > > > Then it's very clear (read: "self-documenting") - that this takes an > array > > > of ProductIDs.. You see? > > > > > > > > > This is just bad, bad form, please don't encourage people to do this!! > If > > > you want loosely coupled, go write in VB6 and make everything a > variant!! > > > :-) > > > > > > > > > > > > "benben" <benhongh@yahoo.com.au> wrote in message > > > news:%231nKifDDFHA.3732@TK2MSFTNGP14.phx.gbl... > > > >I shall disagree with you. C# comes with even better support for > variable > > > > number of arguments. It is the params keyword: > > > > > > > > class Demo > > > > { > > > > double Average(params double[] numbers) > > > > { > > > > double total = 0; > > > > > > > > foreach (double i in numbers) > > > > { > > > > total += i; > > > > } > > > > > > > > return total / numbers.Length; > > > > } > > > > > > > > int Match(double target, double error, params double[] candidates) > > > > { > > > > for (int i = 0; i < candidates.Length; ++i) > > > > { > > > > if (target - candiates <= error) > > > > { > > > > return i; > > > > } > > > > } > > > > > > > > return -1; > > > > } > > > > > > > > string ListNames(params object[] objs) > > > > { > > > > string result = ""; > > > > > > > > foreach (object obj in objs) > > > > { > > > > result += obj.ToString(); > > > > result += "\r\n"; > > > > } > > > > > > > > return result; > > > > } > > > > > > > > void Main() > > > > { > > > > double x = Average(1, 2, 3, 4); // yield 2.5 > > > > double y = Average(2, 3, 4, 5); // yield 3.5 > > > > > > > > // using array directly > > > > double z = Average(double[] {3, 4, 5, 6}); // yield 4.5 > > > > > > > > int a = Match(3.1415926, 0.0001, > > > > 1.23456, 2.34567, 3.1416270, 4.7319032); // yield 2 > > > > > > > > // just another version > > > > int b = Match(3.1415926, 0.0001, > > > > double[]{1.23456, 2.34567, 3.1416270, 4.7319032}); > > > > > > > > string whatever("ben", 1, Color.Black, new MyClass()); > > > > > > > > // ... > > > > } > > > > } > > > > > > > >> No, C# does not give you the ability have optional parameters like in > > C++ > > > > or > > > >> VB. The best that you could do is function overloading, as Patrick > > > >> pointed > > > >> out. > > > >> > > > >> -- > > > >> > > > >> Chris Rolon > > > >> > > > >> This posting is provided "AS IS" with no warranties, and confers no > > > > rights. > > > >> > > > >> "Patrick Altman" <paltman.news@gmail.com> wrote in message > > > >> news:e9Kgci0CFHA.3840@tk2msftngp13.phx.gbl... > > > >> > Yes. Just overload the method. > > > >> > > > > >> > public class MyClass > > > >> > { > > > >> > public void MyMethod(int x, int y) > > > >> > { > > > >> > ... > > > >> > } > > > >> > > > > >> > public void MyMethod(int x) > > > >> > { > > > >> > MyMethod(x, 0); > > > >> > } > > > >> > } > > > >> > > > > >> > Now you have a class with a "single" method, with two "options" of > > how > > > >> > to call it. > > > >> > > > > >> > Hope that helps. > > > >> > > > > >> > Patrick Altman > > > >> > <>< > > > >> > > > > >> > > > > >> > Juan wrote: > > > >> > > Thanks, > > > >> > > Juan. > > > >> > > > > > >> > > > > > >> > > > > >> > -- > > > >> > -- > > > >> > Patrick Altman > > > >> > <>< > > > >> > > > >> > > > > > > > > > > > > > > > > > > > > > > > >
Don't see what you're looking for? Try a search.
|