danrutley
danrutley

Reputation: 19

Error when trying to use variables from another class

I am trying to use variables defined in a class called Card.cs in another class called Deck.cs however I am getting red lines under the variables, and it is giving me the error: “'Card' does not contain definition for 'cardsuit' and no accessible extension method.”

//This is the code I am trying to inherit from
namespace BlackJack
{
    class Card  {}
    
    public class Card
    {
        public string cardsuit { set; get; }
        public int cardvalue { set; get; }
        public int cardnum { set; get; }
    }
    
    public Card()
    {
        cardvalue = -1;
        cardsuit = "";
        cardnum = -1;
    }
}
public class Deck
{
    public List<Card> Cards { set; get; }//The List is the list of cards in the deck

    public Deck() //creates the pack of cards
    {
        Cards = new List<Card>();
        revealdeck();
    }
    
    public void revealdeck()
    {
        Cards.Clear();
        int card_number = 1;

        for (int e = 1, e < 5; e++) // 4 different suits
        {
            for (int f = 1; f < 14; f++)// 13 different cards in each suit 13x4= 52, 52 cards
            {
                Card cardnow = new Card(); //creates the new card
                cardnow.cardnum = card_number;

                if (e == 1)
                cardnow.cardsuit = "spades";
                if (e == 2)
                    cardnow.cardsuit = "clubs";
                if (e == 3)
                    cardnow.cardsuit = "hearts";
                if (e == 4)
                    cardnow.cardsuit = "diamonds";
            }
        }
    }
}

Upvotes: 0

Views: 60

Answers (1)

xtoik
xtoik

Reputation: 676

You have a double definition of the Card class

  1. An empty Card in the BlackJack namespace:

    namespace BlackJack
    {
    class Card 
    {
    
    }
    }
    
  2. A public Card in the default namespace that is the one you want to use:

    public class Card
    {
        public string cardsuit { set; get; }
        public int cardvalue { set; get; }
        public int cardnum { set; get; }
    
    public Card()
    {
        cardvalue = -1;
        cardsuit = "";
        cardnum = -1;
    }
    }
    

Therefore if in Deck.cs you are using the BlackJack namespace the Card will be the empty one, not the one you want to use.

I am almost sure that replacing your Card.cs code by this one will suffice to have everything working:

namespace BlackJack
{
   public class Card
    {
        public string cardsuit { set; get; }
        public int cardvalue { set; get; }
        public int cardnum { set; get; }

        public Card()
        {
            cardvalue = -1;
            cardsuit = "";
            cardnum = -1;
        }
    }
}

Upvotes: 1

Related Questions