Crystal
Crystal

Reputation: 29468

properties and references in Visual Studio, C#

I'm new to C# and going through an example in a book. The example is based on creating a card class, deck class and another CardClient project that runs the two classes.

I am getting errors on compiling. The first error is:

Inconsistent accessibility: property type 'ConsoleApplication1.Card.Suit' is less accessible than property 'ConsoleApplication1.Card.suit'

  1. Not sure why I get that error or what that error is telling me. It seems to be telling me that the property suit is less accessible than the same property, suit. So not sure what is going there.

Next error:

The type or namespace name 'Deck' could not be found (are you missing a using directive or an assembly reference?)

  1. I included a new reference to the ConsoleApplication1, and I declared that I am using that namespace, but for some reason the CardClient does not see it.

The code for both are below:

namespace ConsoleApplication1
{
    class Card
    {
        public Suit suit
        {
            get
            {
                return suit;
            }
        }
        public  Rank rank { get; }

        enum Suit
        {
            HEART,
            SPADE,
            CLUB,
            DIAMOND
        }

        enum Rank
        {
            ACE,
            TWO,
            THREE,
            FOUR,
            FIVE,
            SIX,
            SEVEN,
            EIGHT,
            NINE,
            TEN,
            JACK,
            QUEEN,
            KING
        }

        private Card()
        {
        }

        public Card(Suit suit, Rank rank)
        {
            this.suit = suit;
            this.rank = rank;
        }

        public override string ToString() 
        {
            return suit + " of " + rank;
        }
    }

    class Deck
    {
        private Card[] cards;

        public Deck()
        {
            cards = new Card[52];
            for (int suitVal = 0; suitVal < 4; suitVal++)
            {
                for (int rankVal = 1; rankVal < 14; rankVal++)
                {
                    cards[suitVal * 13 + rankVal - 1] = new cards[(Suit)suitVal, (Rank)rankVal];
                }
            }
        }

        public Card GetCard(int cardLocation) 
        {
            if (cardLocation >= 0 && cardLocation <= 51) 
                return cards[cardLocation];
            else {
                throw (new System.ArgumentOutOfRangeException("cardLocation", cardLocation, "cardLocation must be between 0 and 51."));
            }
        }

        public void Shuffle() 
        {
            Card[] tempDeck = new Card[52];
            bool[] assigned = new bool[52];
            Random sourceGen = new Random();

            for (int i = 0; i < 52; i++)
            {
                int destCard = 0;
                bool foundCard = false;
                while (foundCard == false)
                {
                    destCard = sourceGen.Next(52);
                    if (assigned[destCard] == false)
                        foundCard = true;
                }
                assigned[destCard] = true;
                tempDeck[destCard] = cards[i];
            }
            tempDeck.CopyTo(cards, 0);
        }
    }
}

code for the CardClient:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using ConsoleApplication1;

namespace CardClient
{
    class CardClient
    {
        static void Main(string[] args)
        {
            Deck myDeck = new Deck();
            myDeck.Shuffle();
            for (int i = 0; i < 52; i++)
            {
                Card tempCard = myDeck.GetCard(i);
                Console.Write(tempCard.ToString());
                if (i != 51)
                    Console.Write(", ");
                else
                    Console.WriteLine();
            }
            Console.ReadKey();
        }
    }
}

Upvotes: 1

Views: 290

Answers (1)

thumbmunkeys
thumbmunkeys

Reputation: 20764

You have a public variable suit but the enum type of that variable is private, so basically you declare the variable to be public accessible but the type isn't, which makes no sense.

You have to declare the enum Suit as being public to solve this problem.

Also you have to declare the classes Deck and Card as public, so that the CardClient in the other namespace can access it.

Upvotes: 3

Related Questions