Create Array In Method Paremeters
-
I've written classes for cards, hands, and decks. The hand constructor takes an array of cards as it's paremeter, like so: public Hand(Card[] Cards) { ... perform actions } But here's the thing. When I create a new hand, I'm current doing this: Card[] cards = { deck.TopCard, deck.TopCard, deck.TopCard }; Hand hand1 = new Hand(cards); Rather, I should be doing something like the following, only I can't figure out the correct syntax: Hand hand1 = new Hand( { deck.TopCard, deck.TopCard, deck.TopCard } ); ... Obviously, that throws an error, but I'm pretty sure you see what I want to do. I want to implicitly create the card array in the constructor paremeter. Is this possible?
-
I've written classes for cards, hands, and decks. The hand constructor takes an array of cards as it's paremeter, like so: public Hand(Card[] Cards) { ... perform actions } But here's the thing. When I create a new hand, I'm current doing this: Card[] cards = { deck.TopCard, deck.TopCard, deck.TopCard }; Hand hand1 = new Hand(cards); Rather, I should be doing something like the following, only I can't figure out the correct syntax: Hand hand1 = new Hand( { deck.TopCard, deck.TopCard, deck.TopCard } ); ... Obviously, that throws an error, but I'm pretty sure you see what I want to do. I want to implicitly create the card array in the constructor paremeter. Is this possible?
you need to do
new Card[] {... }
or use theparams
attribute. Look on MSDN for this info. 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! -
I've written classes for cards, hands, and decks. The hand constructor takes an array of cards as it's paremeter, like so: public Hand(Card[] Cards) { ... perform actions } But here's the thing. When I create a new hand, I'm current doing this: Card[] cards = { deck.TopCard, deck.TopCard, deck.TopCard }; Hand hand1 = new Hand(cards); Rather, I should be doing something like the following, only I can't figure out the correct syntax: Hand hand1 = new Hand( { deck.TopCard, deck.TopCard, deck.TopCard } ); ... Obviously, that throws an error, but I'm pretty sure you see what I want to do. I want to implicitly create the card array in the constructor paremeter. Is this possible?