问题已由csharpler回答。我不知道我在彼此内部创建了两个相同的类(在 MenuMaker 类中)......
X
X
X
这是我遇到问题的代码示例:
namespace MeatMenuProgram
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
MenuMaker menu = new MenuMaker() { Randomizer = new Random() };
label1.Text = menu.GetMenuItem();
label2.Text = menu.GetMenuItem();
label3.Text = menu.GetMenuItem();
label4.Text = menu.GetMenuItem();
label5.Text = menu.GetMenuItem();
label6.Text = menu.GetMenuItem();
}
}
}
我做了一个MenuMaker
类:
namespace MeatMenuProgram
{
class MenuMaker
{
class MenuMaker {
public Random Randomizer;
string[] Meats = { "Roast beef", "Salami", "Turkey", "Ham", "Pastrami" };
string[] Condiments = { "yellow mustard", "brown mustard","honey mustard", "mayo", "relish", "french dressing" };
string[] Breads = { "rye", "white", "wheat", "pumpernickel","italian bread", "a roll" };
public string GetMenuItem() {
string randomMeat = Meats[Randomizer.Next(Meats.Length)];
string randomCondiment = Condiments[Randomizer.Next(Condiments.Length)];
string randomBread = Breads[Randomizer.Next(Breads.Length)];
return randomMeat + " with " + randomCondiment + " on " + randomBread;
}
}
}
}
但它不编译。错误日志:
错误 1 'MeatMenuProgram.MenuMaker' 不包含 'Randomizer' 的定义
错误 2“MeatMenuProgram.MenuMaker”不包含“GetMenuItem”的定义,并且找不到接受“MeatMenuProgram.MenuMaker”类型的第一个参数的扩展方法“GetMenuItem”(您是否缺少 using 指令或程序集引用?)