访问来自不同类c#的列表

时间:2013-12-03 23:38:04

标签: c# windows-forms-designer

您好我是c#的新手并且一直试图从另一个类访问我的卡片列表并让它在表单上显示图片但我真正需要的是实例化或引用的方式或者来自的列表我的甲板课。我如何在不同的班级中查看我的名单?

namespace aGameOf21
{ 
   //adds deck class to Ideck interface
   public class Deck : IDeck
   {
      //default deck call
      public Deck()
      {
         Reset();
      }
      // creates list from the card class with get and set accessors
      public List<Card> Cards { get; set; }
      //resets the decks to unshuffled values default when calling class
      public void Reset()
      {
         // using a LINQ statement takes each enumerable from cards list and combines them
         Cards = Enumerable.Range(1, 4)
                           .SelectMany(s => Enumerable.Range(1, 13)
                           .Select(c => new Card()
                           {
                              Suit = (Suit)s,
                              CardNumber = (CardNumber)c
                           }))
                           .ToList();
        /* foreach(Card c in Cards)
         {   
            Console.WriteLine("Number = {0}, Suit = {1}", c.CardNumber , c.Suit);
         }*/
      }

      public void Shuffle()
      { 
         Cards = Cards.OrderBy(c => Guid.NewGuid())
                      .ToList();
      }

      public ICard TakeCard()
      {
         var card = Cards.FirstOrDefault();
         Cards.Remove(card);
         return card;
      }

      public IEnumerable<ICard> TakeCards(int numberOfCards)
      {
         var cards = Cards.Take(numberOfCards);
         var takeCards = cards as Card[] ?? cards.ToArray();
         Cards.RemoveAll(takeCards.Contains);
         return takeCards;
      }
   }
}

2 个答案:

答案 0 :(得分:1)

我不太清楚你在问什么,但如果你使用以下方式创建你的套牌:

var deck = new Deck();

您可以访问您的卡片列表:

var cardList = deck.Cards;

然后,您可以在任何地方传递此列表。

答案 1 :(得分:0)

根据我关于添加变量并通过构造函数传递列表的注释。

//adds deck class to Ideck interface
   public class Deck : IDeck
   {
      private List _list;
      //default deck call
      public Deck(List list)
      {
         _list = list;
         Reset();
      }
   }