简体   繁体   English

尝试使用来自另一个 class 的变量时出错

[英]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 有双重定义

  1. An empty Card in the BlackJack namespace: BlackJack 命名空间中的空 Card:

    namespace BlackJack { class Card {命名空间 BlackJack { class 卡 {

    } } } }

  2. 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.

相关问题 如何使用另一个类文件中的变量 - How to use variables from another class file 如何使用另一个类中的变量 - How to use variables from another class 成员与另一个类的公共变量相同时使用Enum - Use of Enum when members are the same as another class's public variables 当我从另一个班级调用时,变量为零 - Variables come zero when I call from another class 尝试使用类时发生错误,提示找不到类型或名称空间 - Error when trying to use class, says that the type or namespace cannot be found 尝试从另一个项目使用实体数据模型时发生异常 - Exception when trying to use Entity Data Model from another project 从另一个类访问一个类中的变量 - Accessing variables in a class from another class 从另一个类访问和设置一个类中的变量 - access and set variables in a class from another class 尝试从静态类获取数据时,Unity中出现NullReferenceException错误 - NullReferenceException error in Unity when trying to get data from a static class 尝试访问将在C#中由Windows窗体多次使用的类中的变量时出现无效标记错误 - Getting Invalid Token error when trying to access variables in a class that will be used multiple times by Windows Forms in C#
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM