c# - 我的数组类错误 CS0120 C# 的问题
问题描述
我还在学习编码,所以如果有什么问题,请告诉我!这是给学校的作文。
我正在编写一个代码,询问有关游戏的一些信息:游戏名称、背后的开发人员、发行商及其成本。
这篇文章有多个不同的任务,例如:制作一个使用 get set、数组、方法等的程序。我把它全部变成了一个程序,但我遇到了错误
CS0120 非静态字段、方法或属性“Program.gameInfoArray”需要对象引用
编码:
class Program
{
//the class array that stores the games
public gameInfo[] gameInfoArray = new gameInfo[10];
static void Main(string[] args)
{
bool done = false;
// this is where i get the issue
for (int i = 0; i >= gameInfoArray.Length || done == false; i++)
{
//asks what the game is called
Console.WriteLine("What is the game called:");
string gameName = Console.ReadLine();
//asks who the devolopers are
Console.WriteLine("Who where the devolopers behind the game:");
string devoloper = Console.ReadLine();
//asks who published the game
Console.WriteLine("Who released the game:");
string publisher = Console.ReadLine();
//ask how much the game costs
Console.WriteLine("How much does the game cost:");
string cost = Console.ReadLine();
//inputs the information in to the class array, this is also where i get the issue
gameInfoArray[i].gameInformationGrabber(gameName, devoloper, publisher, cost);
//asks if the
Console.WriteLine("are there any more games? Y/N");
while(true)
{
ConsoleKeyInfo yesOrNo = Console.ReadKey();
if ((yesOrNo.KeyChar == 'Y') || (yesOrNo.KeyChar == 'y'))
{
done = true;
break;
}
else if ((yesOrNo.KeyChar == 'N') || (yesOrNo.KeyChar == 'n'))
{
break;
}
}
}
}
}
剧本:
class gameInfo
{
private string gameName;
private string devoloper;
private string publisher;
private string cost;
public void gameInformationGrabber(string game, string dev, string publisher, string cost)
{
theGameName = game;
theDevs = dev;
thePublisher = publisher;
theCost = cost;
}
public string theGameName
{
get { return gameName; }
set { gameName = value; }
}
public string theDevs
{
get { return devoloper; }
set { devoloper = value; }
}
public string thePublisher
{
get { return publisher; }
set { publisher = value; }
}
public string theCost
{
get { return cost; }
set { cost = value; }
}
}
提前致谢。对不起,如果我在代码的某个地方搞砸了。
解决方案
您有两条评论,这对您来说是完美的解决方案。我希望你花时间从中获得一个基本的概念。但是,这将对您有所帮助。
1. 仅静态
这是一个解决方案提到的评论。
class Program
{
// Add static keyword. Because Main() method is static.
// So, every variable inside it should be static.
// public gameInfo[] gameInfoArray = new gameInfo[10];
public static gameInfo[] gameInfoArray = new gameInfo[10];
static void Main(string[] args)
{
......
}
}
2. 使用局部变量
class Program
{
//public gameInfo[] gameInfoArray = new gameInfo[10];
static void Main(string[] args)
{
// Declare as local variable.
// In static method will now complain using local variable.
gameInfo[] gameInfoArray = new gameInfo[10];
......
}
}
我试过运行这段代码,请不要忘记初始化gameInfoArray
以防止空引用异常。在进入 for 循环之前你可能需要这个,
for (int index = 0; index < gameInfoArray.Length; index++)
{
gameInfoArray[index] = new gameInfo();
}
推荐阅读
- excel - Excel - 如果条件为真,则从其他工作表复制
- javascript - Dynamics 365 FetchXml 返回不同的结果
- javascript - 排队事件直到准备好
- excel - 如何在 CSV 源文件中直接将 Microsoft Excel 中的公式格式化为百分比格式
- docker - Docker 容器中的联合挂载
- php - 我该如何解决这个数字加倍和减半的问题?
- groff - Groff Eqn - 如何做更高的根(立方根等)
- c# - Azure Blob 存储授权
- google-apps-script - 例外:数据验证规则的项目数超过了 500 的限制。请改用“范围列表”条件
- c# - Simple Injector 在注册后缺少一种开放的泛型类型 - 可能会发生什么?