CS1061:类型“string”不包含“classChoice”的定义,也没有扩展方法“classChoice”
CS1061: Type `string' does not contain a definition for `classChoice' and no extension method `classChoice'
我正在尝试 return 字符串 classChoice 的值到一个单独的 Class 以便我可以 return 人的选择,我还在学习 C# 并尝试过更好地理解这一点我读到它需要一个实例才能工作所以我创建了 1,无论哪种方式,我已经尝试了大约 8 种不同的方法来做到这一点并且都保持 returning 错误,我做错了什么?
使它无效并失败,取出参数并尝试仅调用 属性,在主 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();
CharSel()
是一种 returns 包含用户选择的值的 string
方法。
您在方法末尾返回了 string
,因此当您调用该方法时,它有效地 是 返回的 string
(包含在classChoice
变量)。这就是为什么 is 给你这个错误:'CharSel()' 是一个 string
而你所写的 (s.CharSel().classChoice
) 试图在 [=18] 上找到一个 classChoice
方法=] class (或扩展方法)。只需从 classChosen
的赋值中删除 .classChoice
,它就会如您所愿地工作。
另一个重点是 classChoice
是 CharSel()
方法的私有变量,在方法之外是不可见的。
我正在尝试 return 字符串 classChoice 的值到一个单独的 Class 以便我可以 return 人的选择,我还在学习 C# 并尝试过更好地理解这一点我读到它需要一个实例才能工作所以我创建了 1,无论哪种方式,我已经尝试了大约 8 种不同的方法来做到这一点并且都保持 returning 错误,我做错了什么?
使它无效并失败,取出参数并尝试仅调用 属性,在主 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();
CharSel()
是一种 returns 包含用户选择的值的 string
方法。
您在方法末尾返回了 string
,因此当您调用该方法时,它有效地 是 返回的 string
(包含在classChoice
变量)。这就是为什么 is 给你这个错误:'CharSel()' 是一个 string
而你所写的 (s.CharSel().classChoice
) 试图在 [=18] 上找到一个 classChoice
方法=] class (或扩展方法)。只需从 classChosen
的赋值中删除 .classChoice
,它就会如您所愿地工作。
另一个重点是 classChoice
是 CharSel()
方法的私有变量,在方法之外是不可见的。