c# - 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
解决方案
您的程序中存在语法错误:
string classChosen = s.CharSel().classChoice;
它应该是:
string classChosen = s.CharSel();
推荐阅读
- python - 匹配固定长度的字符和数字并在 Python 中出现一次
- flutter - 如何在没有 FutureBuilder 的情况下监听一堆 Futures?
- excel - 在多个范围内替换#Ref
- java - 使用日期值迭代 JSON 对象
- swift - 确定 Admob 的测试设备
- javascript - 如何更改输入内闪烁光标的大小
- vb.net - 在 iTextSharp 中,可以更改 PdfTemplate 对象的宽度和高度。我们可以对 iText7 PdfCanvas/Xobject 做同样的事情吗?
- javascript - gojs - 选择时更改节点的颜色,否则返回原始颜色?
- android - 通过协程创建两个序列的http请求。第二个请求必须在第一个完成时等待
- typescript - 如何在fp-ts中组合Either对象的左侧?