首页 > 解决方案 > CS1061:“字符串”类型不包含“classChoice”的定义,并且没有扩展方法“classChoice”

问题描述

我试图将字符串 classChoice 的值返回到一个单独的类,以便我可以返回该人的选择,我仍在学习 C# 并试图更好地理解这一点,我读到它需要一个实例才能工作,所以我创建了 1,无论哪种方式,我已经尝试了大约 8 种不同的方式来执行此操作,并且都不断返回错误,我做错了什么?

使其无效并失败,取出参数并尝试仅调用该属性,在主cs中的cs文件之外尝试它仍然没有运气。

public class Selection
{

    public string CharSel(string classChoice = "")
    {

        Console.WriteLine("Welcome to the world of Text Games!");

        Console.WriteLine("To begin you must select a class!");

        Console.WriteLine("Lucky for you there is only 1 class to choose from at this time :-) ");

        Console.WriteLine("Select a Class:");
        Console.WriteLine("1. Wizard");
        Console.WriteLine("2. Nothing");
        Console.WriteLine("3. Nothing");
        Console.WriteLine("4. Nothing");
        Console.WriteLine("5. Nothing");

        Console.Write("Make your selection: ");

        int choice = Convert.ToInt32(Console.ReadLine());

        if (choice == 1)
        {
            classChoice = "Wizard";
            Console.WriteLine("Congrats on selecting {0} now onto your adventure!", classChoice);
        }
        return classChoice;
    }
}

public class Character
{
    public static string Wizard(string name)
    {
        Selection s = new Selection();

        string classChosen = s.CharSel().classChoice;
        Console.WriteLine("Test, You are a {0}", classChosen);
        name = "none yet";
        return name;
    }
}

控制台应该吐出来

Test, You are a Wizard

标签: c#classmethodsinstance

解决方案


您的程序中存在语法错误:

string classChosen = s.CharSel().classChoice;

它应该是:

string classChosen = s.CharSel();

推荐阅读