-3

You can see the list i created is private and I want to be able to add objects to the list aswell as showing the user the list through methods AddBook() and ShowBooks().

public class Library
{
    private List<Book> Books = new List<Book>();       

    public static void AddBook()
    {
    }

    public static void ShowBooks()
    {
        foreach (Book item in Books)  
        //This foreach-loop doesn't work since its a private list.
        {
            Console.WriteLine("Books found");
        }
    }
}

The loop doesnt work since "An object reference is required for the non-static field, method, or property" refering to the list.

Marc
  • 3,739
  • 4
  • 19
  • 34
  • The `Books` variable is not static.You should take a look at [this thread about `static`](https://stackoverflow.com/questions/413898/what-does-the-static-keyword-do-in-a-class) – Nik. May 27 '19 at 12:27
  • why would you make the methods static? – Andrei Dragotoniu May 27 '19 at 12:30
  • what is the purpose of this code? how do you plan to use it? what's the purpose of the private list? please explain clearly what you are trying to do – Andrei Dragotoniu May 27 '19 at 12:33

3 Answers3

0

If you do not want to use static on the books collection do this:

public class Library
{
    private List<Book> Books = new List<Book>();       

    public void AddBook()
    {
    }

    public void ShowBooks()
    {
        var library = new Library();
        foreach (Book item in Books)  
        {
            //This foreach-loop doesn't work since its a private list.
            library.Books.Add(item);
            Console.WriteLine("Books found");
        }
    }
}
gandaliter
  • 9,120
  • 1
  • 12
  • 19
vsarunov
  • 1,164
  • 12
  • 22
0

if you want to use static methods you can try this

using System;

using System.Collections.Generic;

public class Program
{
    public static void Main()
    {

        Library.AddBook(new Book(){name = "test"});
        Library.AddBook(new Book(){name = "test1"});

        Library.ShowBooks();
    }
}

public class Book
{
    public string name;
}

public static class Library
{
    private static List<Book> Books = new List<Book>();

    public static void AddBook(Book b1)
    {
        Books.Add(b1);
    }

    public static void ShowBooks()
    {
        foreach (Book item in Books)  
        //This foreach-loop doesn't work since its a private list.
        {
            Console.WriteLine(item.name);
        }
    }
}

without static methods you can try this

using System;
using System.Collections.Generic;


public class Program
{
    public static void Main()
    {
        Library l1 = new Library();
        l1.AddBook(new Book(){name = "test"});
        l1.AddBook(new Book(){name = "test1"});

        l1.ShowBooks();
    }
}


public class Book
{
    public string name;
}

public  class Library
{
    private  List<Book> Books = new List<Book>();

    public  void AddBook(Book b1)
    {
        Books.Add(b1);
    }

    public  void ShowBooks()
    {
        foreach (Book item in Books)  
        //This foreach-loop doesn't work since its a private list.
        {
            Console.WriteLine(item.name);
        }
    }
}
dimath
  • 383
  • 2
  • 12
-1

The problem is that your List-instance is non-static, while your methods are. You either have to change your List to be static, or you methods to be non static. Following good OO Design, I would suggest you remove the static modifier from your methods. I also applied a readonly modifier to your List and changed It to be ICollection, as you do not seem to need indexed access. And also applied some common naming patterns.

public class Library
{
    private readonly ICollection<Book> _books = new List<Book>();       


    public void AddBook(Book book)
    {
        _books.Add(book);
    }

    public void ShowBooks()
    {
        foreach (Book item in _books)  
        {
            Console.WriteLine("Books found");
        }
    }
}
wertzui
  • 3,673
  • 2
  • 26
  • 36
  • Because from the authors code, the list is only created once `new List()`. Note that only the list is read only, not the items in the list. You can still add new items to the list. Otherwise you would expose it as an `IReadOnlyCollection`. – wertzui May 27 '19 at 12:50