12 1616
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" <rs****@gmail.c om> wrote in message news:dY******** ***********@new ssvr19.news.pro digy.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(param s 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" <be******@yahoo .com.au> wrote in message news:%2******** ********@TK2MSF TNGP14.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.Leng th; ++i) > { > if (target - candiates <= error) > { > return i; > } > } > > return -1; > } > > string ListNames(param s 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" <pa**********@g mail.com> wrote in message >> news:e9******** ******@tk2msftn gp13.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.
"benben" <be******@yahoo .com.au> wrote in message
news:%2******** ********@TK2MSF TNGP10.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" <rs****@gmail.c om> wrote in message > news:dY******** ***********@new ssvr19.news.pro digy.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(param s 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" <be******@yahoo .com.au> wrote in message > > news:%2******** ********@TK2MSF TNGP14.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.Leng th; ++i) > > > { > > > if (target - candiates <= error) > > > { > > > return i; > > > } > > > } > > > > > > return -1; > > > } > > > > > > string ListNames(param s 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" <pa**********@g mail.com> wrote in message > > >> news:e9******** ******@tk2msftn gp13.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.
"benben" <be******@yahoo .com.au> wrote in message
news:OI******** ******@TK2MSFTN GP15.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(param s 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" <be******@yahoo .com.au> wrote in message news:%2******** ********@TK2MSF TNGP14.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.Leng th; ++i) > { > if (target - candiates <= error) > { > return i; > } > } > > return -1; > } > > string ListNames(param s 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" <pa**********@g mail.com> wrote in message >> news:e9******** ******@tk2msftn gp13.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 thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
by: ad |
last post by:
Does C#2.0 support optional parameters like VB.NET:
Function MyFunction(Optional ByVal isCenter As Boolean = False)
|
by: Imran Aziz |
last post by:
Hello All,
I am new to C# , how can I delare parameters to a function as optional,
and also how are default values assigned ?
consider the function
public String myFunc(String thisisOptional, String OptWithDefaultValue)
{
}
|
by: guy |
last post by:
found this oddity-
copying a vb6 function with optional parameters and pasting it into a vb2003
class, for conversion purposes, it all works fine except that the collapsing
"-" on the LHS goes away, only to return if "Optional" is removed. Strange.
anyone else had this?
guy
|
by: deko |
last post by:
In VB, I could do this:
MyFuncrion(this As String, that As Integer, Optional otherThing As Boolean)
do stuff here
End Function
In C#, I can use "out" to return multiple values from a method, and "params"
to send in an array, but is it possible to have optional parameters? How do
I do this in C#?
|
by: cody |
last post by:
I got a similar idea a couple of months ago, but now this one will require
no change to the clr, is relatively easy to implement and would be a great
addition to C# 3.0 :)
so here we go..
To make things simpler and better readable I'd make all default parameters
named parameters so that you can decide for yourself which one to pass and
which not, rather than relying on massively overlaoded methods which
hopefully provide the best...
| |
by: info |
last post by:
I need to use DAO in one of my applications. It was too hard to pass
through by optional parameters for several methods, but now I'm totally
lost at DAO seek method (the main method for key s).
The method has a next declaration:
Sub Seek(Comparison As String, Key1, , , , ,
, , , , , , , )
Member of DAO.Recordset
12 optional parameters. I tried anything for the Key2-Key13, without
|
by: pamelafluente |
last post by:
Hi guys,
In the past I have used several time optional parameters in my
function.
But Now I am more inclined to think that they are more dangerous than
useful, and probably better to be avoided.
I'd like to hear your various opinions on this matter.
|
by: Atli |
last post by:
This is an easy to digest 12 step guide on basics of using MySQL. It's a great refresher for those who need it and it work's great for first time MySQL users.
Anyone should be able to get through this without much trouble. Programming knowledge is not required.
Index
What is SQL?
Why MySQL?
Installing MySQL.
Using the MySQL command line interface
|
by: MartinRinehart |
last post by:
Working on parser for my language, I see that all classes (Token,
Production, Statement, ...) have one thing in common. They all
maintain start and stop positions in the source text. So it seems
logical to have them all inherit from a base class that defines those,
but this doesn't work:
import tok
class code:
def __init__( self, start, stop ):
|
by: marktang |
last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look !
Part I. Meaning of...
|
by: Hystou |
last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it.
First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
| |
by: jinu1996 |
last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth.
The Art of Business Website Design
Your website is...
|
by: tracyyun |
last post by:
Dear forum friends,
With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
|
by: isladogs |
last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM).
In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules.
He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms.
Adolph will...
|
by: conductexam |
last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one.
At the time of converting from word file to html my equations which are in the word document file was convert into image.
Globals.ThisAddIn.Application.ActiveDocument.Select();...
|
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
|
by: 6302768590 |
last post by:
Hai team
i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
| |
by: bsmnconsultancy |
last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...
| |