Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. Single Method With Mutiple Paremeters

Single Method With Mutiple Paremeters

Scheduled Pinned Locked Moved C#
data-structuresquestion
7 Posts 5 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • B Offline
    B Offline
    budidharma
    wrote on last edited by
    #1

    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!

    M 1 Reply Last reply
    0
    • B budidharma

      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!

      M Offline
      M Offline
      Marc 0
      wrote on last edited by
      #2

      I think you have to use the params[^] keyword:

      public static int[] BuildArray(params int[] numbers){
      return numbers
      }

      Pompiedompiedom... ;)


      "..Commit yourself to quality from day one..it's better to do nothing at all than to do something badly.." -- Mark McCormick

      B 1 Reply Last reply
      0
      • M Marc 0

        I think you have to use the params[^] keyword:

        public static int[] BuildArray(params int[] numbers){
        return numbers
        }

        Pompiedompiedom... ;)


        "..Commit yourself to quality from day one..it's better to do nothing at all than to do something badly.." -- Mark McCormick

        B Offline
        B Offline
        budidharma
        wrote on last edited by
        #3

        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.

        J 1 Reply Last reply
        0
        • B budidharma

          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.

          J Offline
          J Offline
          J4amieC
          wrote on last edited by
          #4

          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

          L 1 Reply Last reply
          0
          • J J4amieC

            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

            L Offline
            L Offline
            leppie
            wrote on last edited by
            #5

            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!

            J 1 Reply Last reply
            0
            • L leppie

              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!

              J Offline
              J Offline
              J4amieC
              wrote on last edited by
              #6

              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 :|

              H 1 Reply Last reply
              0
              • J J4amieC

                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 :|

                H Offline
                H Offline
                Heath Stewart
                wrote on last edited by
                #7

                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::Foo

                As 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]

                1 Reply Last reply
                0
                Reply
                • Reply as topic
                Log in to reply
                • Oldest to Newest
                • Newest to Oldest
                • Most Votes


                • Login

                • Don't have an account? Register

                • Login or register to search.
                • First post
                  Last post
                0
                • Categories
                • Recent
                • Tags
                • Popular
                • World
                • Users
                • Groups