为什么我无法访问这些属性?

时间:2017-10-17 20:29:29

标签: c# visual-studio

我遇到了将属性从一个类传递到另一个类的问题。 我收到一个错误,说第二个类中的所有Game属性都需要一个对象引用。它们在底部突出显示。 这是我的第一堂课(游戏):

 class Game
        {


  private string verb= "";
            private string noun= "";
            private string adjective= "";
            private string panimal= "";
            private string pnoun= "";


            public string Verb
                {
                get {return verb; }
                set {verb = value; }
                }    
            public string Noun
                { 
                get {return noun; }
                set {noun = value; }
                }

            public string Adjective 
                {
                get {return adjective;}
                set {adjective = value; }
                }
            public string Panimal
                {
                get {return panimal; }
                set {panimal = value; }
                }

            public string Pnoun
                {
                get {return pnoun; }
                set {pnoun = value; }
                }


            public void InScreen()   
            {        
            Console.WriteLine("First, give me a past tense VERB: ");
            Verb = Console.ReadLine();

            Console.WriteLine("\nNow, give me a NOUN: ");
            Noun = Console.ReadLine();

            Console.WriteLine("\nNext, I will need an ADJECTIVE: ");
            Adjective = Console.ReadLine();

            Console.WriteLine("\nNow, I will need an ANIMAL(plural): ");
            Panimal = Console.ReadLine();

            Console.WriteLine("\nFinally, I neeed a plural NOUN: ");
            Pnoun = Console.ReadLine();
            }

我的第二堂课(InsertFunOOUI)

        public void Poem()
        {    
         Console.WriteLine("Humpty Dumpty " + **Game.Verb** + "on a " + 
        **Game.Noun** 
          );
         Console.WriteLine("Humpty Dumpty had a " + **Game.Adjective** + " 
        fall" 
          );
         }

...你明白了。

3 个答案:

答案 0 :(得分:1)

Game是一个类型。一类。您需要创建它的实例:

Game g = new Game();

然后才使用:

g.Verb

答案 1 :(得分:0)

您需要设置一个Game实例才能使用它,除非您打算将它作为静态类。

deletePatient(patientId) {
        var config = {
            headers: {
                'User-Agent':'',
                'Accept':'',
                'Host':''
            }
        };

        axios.delete(url + 'patients/' + patientId, config).then((res) => 
        {      
            this.props.dispatch(deletePatientAction(res));
        })
        .catch( function(error) {
            console.log(JSON.stringify(error, null, 2));
        });            
}

那样的东西?

答案 2 :(得分:0)

首先创建一个像@ispiro这样的游戏实例。你需要做一次(如果可能的话,在主要的)。然后创建你的类诗的实例。如果可能,使用依赖注入并将游戏对象的属性作为参数传递,而不是整个对象。我甚至会在你的Poem类中创建一个处理控制台输出的方法。根据您的喜好调整此代码:

public void main()
{
    var game = new Game();
    var poem = new Poem();
    poem.Output(game.Verb, game.Adjective);
}

public class Poem()
{
    public void Output(string verb, string adjective)
    {
        // your console writeline code
    }
}
相关问题