1

I have never programmed in C# in my life but i understand java well enough. I am running this program and everytime it keeps saying count/IEnumerator has not been implemented. I have tried various different ways and have put it in various different locations in file. I understand that this will be an extremely easy thing to do but I don't understand it. I also presume these are some sort of methods so don't ask why they are sitting where the instance variables are.

Can someone explain to me how to use collections? The collection being used here is ireadonlycollection which is implemented by IPackOfCards that is implemented in this class.

Are C# interfaces allowed to have implementation in them?

using System;
using CodedTests.NUnit;
using System.Collections.Generic;
using System.Collections.ObjectModel;
namespace  CodedTests.NUnit
{

public class PackOfCards : IPackOfCards
{
        private const int NUMCARDS = 52;
        private int cardsInPack;
        //private Card[] cards;
        private Collection<ICard> pack = new Collection<ICard>();
        int Count { get; }
        public IEnumerator<ICard> GetEnumerator(){return pack.GetEnumerator();}
        //IEnumerator IEnumerable.GetEnumerator(){ return GetEnumerator();}

        public Collection<ICard> Create()
        {
             cards = new Card [NUMCARDS];
             String [] values =     {"Ace","Two","Three","Four","Five","Six","Seven","Eight","Nine","Ten","Jack","Queen","King"};
             String [] suits = {"Hearts", "Diamonds", "Spades", "Clubs"};
             int cardsInPack = 0;
             for(int i = 0 ; i<suits.Length; i++){
             for(int j = 0; j<values.Length; j++, cardsInPack++){
                cards[cardsInPack]= new Card(values[j], suits[i]);
                 }
             }

            return new pack;
        }

         public void Shuffle(){
             Random num = new Random();

             for(int i = 0; i<NUMCARDS;i++){
                 int ran =  num.Next(NUMCARDS);
                 Card temp = cards[ran];
                 cards[ran] = cards[i];
                 cards[i] =     temp;
            }
        }
        public ICard TakeCardFromTopOfPack (){

             int topCard = 0;
             ICard cardRemoved = cards[topCard];
              for(int i = 0;i<NUMCARDS-1;i++){
                   cards[i]=cards[i+1];
                }
               cards[cardsInPack] = null;
               cardsInPack--;
               return cardRemoved;
             }
            }

           }

    public class Card : ICard
    {
         private String value;
         private String suit;

         public Card(String v, String s)
         {
              value = v;
              suit = s;
         }
         public String getValue(){
             return value;
         }
         public String getSuit(){
             return suit;
          }
        public String toString(){
             return value+" of  "+suit;
         }
      } 
  }


public interface IPackOfCards : IReadOnlyCollection<ICard>
{
    void Shuffle ();
    ICard TakeCardFromTopOfPack ();
}

 public interface IPackOfCardsCreator
 {
    IPackOfCards Create ();
 }

 public class PackOfCardsCreator : IPackOfCardsCreator
 {
   public IPackOfCards Create()
5
  • Please forget some of the other code it is from a previous code that I wrote for this task before I noticed that they wanted to use collections Commented Jul 11, 2015 at 11:49
  • Any chance of seeing the relevant parts of your IPackOfCards interface? Commented Jul 11, 2015 at 11:54
  • Sure, I just fix the indentation now Commented Jul 11, 2015 at 12:00
  • 1
    I think you're in a bit of a mess here. I'm assuming that you believe that you have implemented your IPackOfCards interface in your class, but actually it's not been implemented. If you right-click on the interface name in your public class PackOfCards : IPackOfCards declaration, and select Implement Interface -> Implicitly (or explicitly), this will stub out the correct methods for you and should point you in the right direction. Hope that helps! Commented Jul 11, 2015 at 12:06
  • ah ok, thank you for your help. I have a new error but at least its a step in the right direction. Although I do not get the implicit explicit option is it the same as extract? Commented Jul 11, 2015 at 12:11

1 Answer 1

1

IPackOfCards implements IReadOnlyCollection. That means you have to implement all members of IReadOnlyCollection in addition to members of IPackOfCards, namely a Count property and a GetEnumerator method, as documented here. All interface members have to be public, but your Count property is private. You didn't specify its accessibility, so it's defaulted to private.

Sign up to request clarification or add additional context in comments.

4 Comments

This is what I was trying to do but it kept throwing errors.
OK so when I put private int Count; i still get the same error. I'm sorry if this is really obvious.
Just noticedi'll try public. Try public int Count but no luck.
Thank you, I put me on the right path. I wasn't implementing the propertyp properly. stackoverflow.com/questions/1593413/… here is the answer for anyone else.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.