[英]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.”我正在尝试在另一个名为 Deck.cs 的 class 中使用名为 Card.cs 的 class 中定义的变量,但是我在变量下看到红线,它给了我错误:“‘Card’不包含‘cardsuit 的定义’ ' 并且没有可访问的扩展方法。”
//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";
}
}
}
}
You have a double definition of the Card class你对 Card class 有双重定义
An empty Card in the BlackJack namespace: BlackJack 命名空间中的空 Card:
namespace BlackJack { class Card {命名空间 BlackJack { class 卡 {
} } } }
A public Card in the default namespace that is the one you want to use:默认名称空间中的公共 Card 是您要使用的名称空间:
public class Card { public string cardsuit { set; public class Card { public string cardsuit { set; get;
得到; } public int cardvalue { set;
} public int cardvalue { 设置; get;
得到; } public int cardnum { set;
} public int cardnum { 设置; get;
得到; }
}
public Card() { cardvalue = -1; public Card() { cardvalue = -1; cardsuit = "";
纸牌套装 = ""; cardnum = -1;
卡数=-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.因此,如果在 Deck.cs 中您使用的是 BlackJack 命名空间,则 Card 将是空的,而不是您要使用的那个。
I am almost sure that replacing your Card.cs code by this one will suffice to have everything working:我几乎可以肯定,用这个替换你的 Card.cs 代码就足以让一切正常工作:
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;
}
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.