在使用REF和数组以及方法方面需要帮助 [英] Need help using REF and Arrays, and Methods

查看:75
本文介绍了在使用REF和数组以及方法方面需要帮助的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

好的,我正在使用C#类进行实验,涉及使用ref参数,数组和方法.在执行此操作时遇到一些问题,我正在寻求帮助.所以..首先,我将问题修改为最简单的部分,以帮助我解释存在的问题.这是一段简化的代码:

Ok, I'm doing my lab from C# class which involve using ref parameters, arrays, and methods. There is a few problems which I encounter while doing this and I'm begging for help. So.. First I modified problem into simplest chunks to help me explain which problems I have. Here is a piece of simplified code:

using System;

public class Repository 
{
    string[] titles;

    static void Main(string[] args)
    {
        string title;

        Console.Write("Title of book: ");
        title = Console.ReadLine();

        getBookInfo(ref title);
    }

    static void getBookInfo(ref string title)
    {
        titles[0] = title;
    }

    static void displayBooks(string[] titles)
    {
        Console.WriteLine("{0}", titles[0]);
    }
}

现在,当您尝试编译代码时,您会注意到无法编译,因为错误提示访问非静态成员'Repository.titles'需要对象引用".问题在于,这3种方法的格式必须与作业中所说明的完全相同.现在,如何在保留此模板的同时避免该问题?

Now, as u will try to compile code, you notice that can not be compiled because error say "An object reference is required to access non-static member 'Repository.titles'". The problem is that the format of 3 methods must b exactly as posted as told in the assignment. Now, how can I avoid this problem while keeping this template in place?

其他问题,我该如何在main中显示方法displayBooks的内容? (由于问题,我还没走这么远).

Other question, how would I display content of method displayBooks in main? (I haven't got this far because of problems).

致谢,请帮忙!

-----------------------谢谢您的帮助!!! ---------

----------------------- THANK YOU FOR HELP !!! ---------

推荐答案

首先,除非您要更改title中的title值,否则无需使用ref.以下代码演示了该概念:

Firstly, you don't need to use ref unless you want to alter the value of title as it exists within Main(). The following code demonstrates the concept:

static void Main(string[] args)
{
    string a = "Are you going to try and change this?";
    string b = "Are you going to try and change this?";

    UsesRefParameter(ref a);
    DoesntUseRefParameter(b);
    Console.WriteLine(a); // I changed the value!
    Console.WriteLine(b); // Are you going to try and change this?
}

static void UsesRefParameter(ref string value)
{
    value = "I changed the value!";
}

static void DoesntUseRefParameter(string value)
{
    value = "I changed the value!";
}

需要先创建一个数组,然后才能使用它.因此,这是您的已更正的代码:

An array needs to be created before you can use it. So here is your code that has been corrected:

static string[] titles;

static void Main(string[] args)
{
    string title;
    titles = new string[1]; // We can hold one value.

    Console.Write("Title of book: ");
    title = Console.ReadLine();

    getBookInfo(title);
}

static void getBookInfo(string title)
{
    titles[0] = title;
}

要显示您的图书,您可以尝试以下方法:

To display your books you could try the following method:

static void displayBooks(string[] titles)
{
    // Go over each value.
    foreach (string title in titles)
    {
        // And write it out.
        Console.WriteLine(title);
    }
}
// In Main()
displayBooks(titles);

这篇关于在使用REF和数组以及方法方面需要帮助的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆