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]);
}
}
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?
Other question, how would I display content of method displayBooks in main? (I haven’t got this far because of problems).
Regards, and please help!
———————– THANK YOU FOR HELP !!! ———
Firstly, you don’t need to use
refunless you want to alter the value oftitleas it exists withinMain(). The following code demonstrates the concept:An array needs to be created before you can use it. So here is your code that has been corrected:
To display your books you could try the following method: