Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/310.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 有关数组、方法和ref参数的更多问题_C#_Arrays_Methods_Ref - Fatal编程技术网

C# 有关数组、方法和ref参数的更多问题

C# 有关数组、方法和ref参数的更多问题,c#,arrays,methods,ref,C#,Arrays,Methods,Ref,今天早些时候,我正在为我的C#班做作业,现在我有另一个问题,我不明白是什么错了。所以在第一篇文章中,我遇到了静态参数的问题,所以我得到了解释和修复。但现在,我得到了一个错误,即“对象引用未设置为对象的实例”。我检查了是否有打字错误,但仍然无法运行它。PS,问题不是在编译时出现的,而是在输入所有数据时,以及在输入数据并按enter键后按N键时出现的。请看一看 using System; public class Repository { static string[] titles;

今天早些时候,我正在为我的C#班做作业,现在我有另一个问题,我不明白是什么错了。所以在第一篇文章中,我遇到了静态参数的问题,所以我得到了解释和修复。但现在,我得到了一个错误,即“对象引用未设置为对象的实例”。我检查了是否有打字错误,但仍然无法运行它。PS,问题不是在编译时出现的,而是在输入所有数据时,以及在输入数据并按enter键后按N键时出现的。请看一看

using System;

public class Repository 
{
    static string[] titles;
    static string[] authorFirstNames;
    static string[] authorLastNames;
    static string[] publisherNames;
    static float[] prices;
    static int number;

    static void Main(string[] args)
    {
        string title = "";
        string authorFirst = "";
        string authorLast = "";
        string publisherName = "";
        float price = 0;

        getBookInfo(ref title, ref authorFirst, ref authorLast, ref publisherName, ref price);      
        displayBooks(titles, authorFirstNames, authorLastNames, publisherNames, prices, number);
    }

    static void getBookInfo(ref string title, ref string authorFirst, 
                            ref string authorLast, ref string publisherName, 
                            ref float price)
    {
        string continued;
        string float_num;
        int i = 0;

        titles = new string[50];

        do
        {
            Console.Write("Title of book: ");
            title = Console.ReadLine();
            Console.Write("Authors first name: ");
            authorFirst = Console.ReadLine();
            Console.Write("Authors last name: ");
            authorLast = Console.ReadLine();
            Console.Write("Publishers Name: ");
            publisherName = Console.ReadLine();
            Console.Write("Price: ");
            float_num = Console.ReadLine();
            Console.Write("Add another book? Y/N ");
            continued = Console.ReadLine().ToLower();

            price = float.Parse(float_num);

            titles[i] = title;
            authorFirstNames[i] = authorFirst;
            authorLastNames[i] = authorLast;
            publisherNames[i] = publisherName;
            prices[i] = price;

            number = i;

            i++;
        }
        while (continued == "y");
    }

    static void displayBooks(string[] titles, string[] authorFirstNames, 
                             string[] authorLastNames, string[] publisherNames, 
                             float[] prices, int number)
    {
        foreach (string title in titles)
        {
            Console.WriteLine(title);
            if(title == null)
                break;
        }
    }
}
原因是什么

问候,并希望得到一些建议


PS,displayBooks方法尚未完成。

我在代码中没有看到任何地方初始化这些变量:

static string[] authorFirstNames;
static string[] authorLastNames;
static string[] publisherNames;
static float[] prices;

我在代码中没有看到任何地方初始化这些变量:

static string[] authorFirstNames;
static string[] authorLastNames;
static string[] publisherNames;
static float[] prices;

问题在于行
静态字符串[]authorFirstNames

您没有初始化数组,因此对象为空。您需要像这样初始化它:
静态字符串[]authorFirstNames=新字符串[1]
您必须指定数组的大小,即[1]的大小。这意味着数组可以容纳1个字符串

以后可以使用Array.Resize()调整数组的大小

如果项目允许,您可能希望使用
列表
,而不是字符串数组


通过查看调试器中的代码,您可以自己解决这类问题。在本例中,假设您使用的是Visual Studio,它将突出显示引发异常的行。如果查看
authorFirstNames
的值,您会看到它为null,这意味着它从未初始化过。

问题在于行
静态字符串[]authorFirstNames

您没有初始化数组,因此对象为空。您需要像这样初始化它:
静态字符串[]authorFirstNames=新字符串[1]
您必须指定数组的大小,即[1]的大小。这意味着数组可以容纳1个字符串

以后可以使用Array.Resize()调整数组的大小

如果项目允许,您可能希望使用
列表
,而不是字符串数组



通过查看调试器中的代码,您可以自己解决这类问题。在本例中,假设您使用的是Visual Studio,它将突出显示引发异常的行。如果查看
authorFirstNames
的值,您会看到它为空,这意味着它从未初始化过。

在哪一行出现异常?请确保用家庭作业标记标记家庭作业问题。然后人们知道如何帮助你,而不是仅仅告诉你答案=)这是第52行,下次我会添加另一个标记,谢谢。你什么时候初始化任何数组(除了标题)?啊哈!这是我的问题!我需要在第32行下面添加初始值设定项。好眼力!例外情况出现在哪一行?确保用家庭作业标签标记家庭作业问题。然后人们知道如何帮助你,而不是仅仅告诉你答案=)这是第52行,下次我会添加另一个标记,谢谢。你什么时候初始化任何数组(除了标题)?啊哈!这是我的问题!我需要在第32行下面添加初始值设定项。好眼力!是的,我现在正在看这个,不,我用的是MonoDevelop。试图远离Micro$oft。你使用MonoDevelop纯粹是因为你不喜欢Microsoft,即使有免费版本的Visual Studio。。但是你用的是C#?@Rob:有可能他是被迫用C#(比如,他的学校/学院的课程是用C#教授的),所以他更喜欢mono,因为他可以尽可能远离微软=)在我的大学里,同样的厄运也伴随着我:(@Abel,即使如此,还是故意用你的方式去用一个劣等的(别误会,单声道令人难以置信,但并不完美)纯粹出于怨恨的产品有点适得其反。这就像买一台Mac电脑,在WINE下运行每一个应用程序,只是为了避开微软。是的,我现在正在看这个,不是的,我正在使用MonoDevelop。试图远离Micro$oft。你使用MonoDevelop纯粹是因为你不喜欢微软——即使有免费版本在visualstudio上……但你却在使用C#?@Rob:有可能他是被迫使用C#(比如,他的学校/学院的课程是用C#教授的),所以他更喜欢用mono作为尽可能远离微软的一种方式=)在我的大学里,同样的厄运也伴随着我:(@Abel,尽管如此,还是故意用你的方式来使用次等的(别误会,Mono令人难以置信,但它并不完美)纯粹出于怨恨的产品有点适得其反。这就像购买Mac电脑,在WINE下运行每个应用程序,只是为了避开微软。