473,395 Members | 1,652 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

basic: Is it possible to define optional parameters in a method declaration? how?

Thanks,
Juan.
Nov 16 '05 #1
12 1586
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
<><
Nov 16 '05 #2
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**********@gmail.com> wrote in message
news:e9**************@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
<><

Nov 16 '05 #3
I see. Thanks a los, i will overload then.

"Chris Rolon" <cr****@hotmail.com> escribió en el mensaje
news:eM**************@TK2MSFTNGP12.phx.gbl...
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**********@gmail.com> wrote in message
news:e9**************@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
<><


Nov 16 '05 #4
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" <pa**********@gmail.com> wrote in message
news:e9**************@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
<><


Nov 16 '05 #5
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" <pa**********@gmail.com> wrote in message
news:e9**************@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
<><



Nov 16 '05 #6
RCS
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" <be******@yahoo.com.au> wrote in message
news:%2****************@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" <pa**********@gmail.com> wrote in message
news:e9**************@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
> <><



Nov 16 '05 #7
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.com> wrote in message
news:dY*******************@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" <be******@yahoo.com.au> wrote in message
news:%2****************@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" <pa**********@gmail.com> wrote in message
news:e9**************@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
> <><



Nov 16 '05 #8
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.com> wrote in message
news:dY*******************@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" <be******@yahoo.com.au> wrote in message
news:%2****************@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" <pa**********@gmail.com> wrote in message
> news:e9**************@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
> > <><
>
>



Nov 16 '05 #9

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" <be******@yahoo.com.au> wrote in message
news:%2****************@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" <pa**********@gmail.com> wrote in message
news:e9**************@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
> <><



Nov 16 '05 #10
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.com> wrote in message
news:dY*******************@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" <be******@yahoo.com.au> wrote in message
news:%2****************@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" <pa**********@gmail.com> wrote in message
>> news:e9**************@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
>> > <><
>>
>>
>
>
>



Nov 16 '05 #11
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****************@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" <rs****@gmail.com> wrote in message
> news:dY*******************@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" <be******@yahoo.com.au> wrote in message
> > news:%2****************@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" <pa**********@gmail.com> wrote in message
> > >> news:e9**************@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
> > >> > <><
> > >>
> > >>
> > >
> > >
> > >
> >
> >
>
>



Nov 16 '05 #12
RCS
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**************@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" <be******@yahoo.com.au> wrote in message
news:%2****************@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" <pa**********@gmail.com> wrote in message
>> news:e9**************@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
>> > <><
>>
>>
>
>
>



Nov 16 '05 #13

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

16
by: ad | last post by:
Does C#2.0 support optional parameters like VB.NET: Function MyFunction(Optional ByVal isCenter As Boolean = False)
5
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,...
3
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...
10
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,...
14
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...
1
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)....
12
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...
6
Atli
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...
14
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...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
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,...
0
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...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
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...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.