Single Method With Mutiple Paremeters
-
I know I can overload the same function to take specific parememters, but I'm looking for something a little more than that. I want to return an array of ints, based on the number of ints passed into the function (really, I'm using this with objects). public static int[] BuildArray( /* ACCEPT A MULTIPLE NUMBER OF INTS, WITHOUT BEING IN AN ARRAY ALREADY /* ) { int[] ints = new ints[ /*NUMBER OF INTS PASSED INTO THE FUNCTION /* ]; int i = 0; foreach( /* INT PASSED INTO THE FUNCTION */ ) ints[i] = /* EACH INT PASSED IN */; i++; } return ints; } ... The method accepts a multiple number of paremeters, all int. I want it to build an array of them, then pass that back out. Is this possible? Like I said, I know I can overload the function a whole bunch of times like so: public static int[] BuildArray(int i1, int i2) { // BUILD ARRAY AND RETURN IT } public static int[] BuildArray(int i1, i2, i3) { // BUILD ARRAY AND RETURN IT } public static int[] BuildArray(int i1, i2, i3, i4) { // BUILD ARRAY AND RETURN IT } ... but that's not elegant, and I'll have to write a method for every freaking number of ints that could be passed in. Ugh. Any solution here? Thanks!
-
I know I can overload the same function to take specific parememters, but I'm looking for something a little more than that. I want to return an array of ints, based on the number of ints passed into the function (really, I'm using this with objects). public static int[] BuildArray( /* ACCEPT A MULTIPLE NUMBER OF INTS, WITHOUT BEING IN AN ARRAY ALREADY /* ) { int[] ints = new ints[ /*NUMBER OF INTS PASSED INTO THE FUNCTION /* ]; int i = 0; foreach( /* INT PASSED INTO THE FUNCTION */ ) ints[i] = /* EACH INT PASSED IN */; i++; } return ints; } ... The method accepts a multiple number of paremeters, all int. I want it to build an array of them, then pass that back out. Is this possible? Like I said, I know I can overload the function a whole bunch of times like so: public static int[] BuildArray(int i1, int i2) { // BUILD ARRAY AND RETURN IT } public static int[] BuildArray(int i1, i2, i3) { // BUILD ARRAY AND RETURN IT } public static int[] BuildArray(int i1, i2, i3, i4) { // BUILD ARRAY AND RETURN IT } ... but that's not elegant, and I'll have to write a method for every freaking number of ints that could be passed in. Ugh. Any solution here? Thanks!
-
Works perfectly. Thank you very much. One thing I noticed is that having the following two constructors threw an error: public Hand(Card[] cards) { m_Hand = cards.Clone(); } public Hand(params Card[] cards) { m_Hand = cards; } Caused a "redefinition" error. The first allowed me to call it as such: Hand myHand = new Hand(cards); // REQUIRING A PREBUILT ARRAY OF CARDS But did not allow me to do: Hand myHand = new Hand(card1, card2, card3); The second will take a prebuild array of cards and work fine, or allow me pass in any number of cards and will automatically build the cards array from them. I had to delete the first constructor, but that's fine. Works perfectly. Thank you.
-
Works perfectly. Thank you very much. One thing I noticed is that having the following two constructors threw an error: public Hand(Card[] cards) { m_Hand = cards.Clone(); } public Hand(params Card[] cards) { m_Hand = cards; } Caused a "redefinition" error. The first allowed me to call it as such: Hand myHand = new Hand(cards); // REQUIRING A PREBUILT ARRAY OF CARDS But did not allow me to do: Hand myHand = new Hand(card1, card2, card3); The second will take a prebuild array of cards and work fine, or allow me pass in any number of cards and will automatically build the cards array from them. I had to delete the first constructor, but that's fine. Works perfectly. Thank you.
How about a static helper method on your Hand class
class Hand { private Card[] cards; public Hand(Card[] cards) { this.cards = cards; } public static Hand CreateFrom(params Card[] cards) { return new Hand(cards); } }
now you can do Card [] myCards = new Card[]{...}; Hand myHand = new Hand(myCards); or Hand myHand = Hand.CreateFrom(Card1, Card2, Card3); -- modified at 16:14 Saturday 5th November, 2005 -
How about a static helper method on your Hand class
class Hand { private Card[] cards; public Hand(Card[] cards) { this.cards = cards; } public static Hand CreateFrom(params Card[] cards) { return new Hand(cards); } }
now you can do Card [] myCards = new Card[]{...}; Hand myHand = new Hand(myCards); or Hand myHand = Hand.CreateFrom(Card1, Card2, Card3); -- modified at 16:14 Saturday 5th November, 2005J4amieC wrote:
How about a static helper method on your Hand class
:doh: Why? Its already
Card[]
! xacc-ide 0.0.99-preview7 now with C#, C, C++, IL, XML, Nemerle, IronPython, Perl, Caml, SML, Ruby, Flex, Yacc, Java, Javascript, Lua, Prolog and Boo highlighting support! -
J4amieC wrote:
How about a static helper method on your Hand class
:doh: Why? Its already
Card[]
! xacc-ide 0.0.99-preview7 now with C#, C, C++, IL, XML, Nemerle, IronPython, Perl, Caml, SML, Ruby, Flex, Yacc, Java, Javascript, Lua, Prolog and Boo highlighting support! -
because he couldnt have 2 constructors one as "Card[] cards" and one as "params Card[] cards". It was just a suggestion, call it syntactic sugar :|
The reason is that the methods or constructors would have the same signature and that, of course, isn't allowed for overloads. See the IL for a method defined using the
params
keyword in C#:.method public hidebysig instance void Foo(object[] objects) cil managed
{
.param [1]
.custom instance void [mscorlib]System.ParamArrayAttribute::.ctor() = ( 01 00 00 00 )
// Code size 2 (0x2)
.maxstack 8
IL_0000: nop
IL_0001: ret
} // end of method Test::FooAs you can see, the effect is achieved using an attribute but the parameter itself is just an
object[]
array. This posting is provided "AS IS" with no warranties, and confers no rights. Software Design Engineer Developer Division Customer Product-lifecycle Experience Microsoft [My Articles] [My Blog]