在尚未实例化的对象C#中调用函数

时间:2016-09-20 17:15:28

标签: c# oop object

我正在尝试为我的简单应用创建菜单。我想使用switch但我找不到一种方法来调用在应用程序启动时实例化的uninstanced类中的函数。

class Window
{
    public void startLibrary(int sizeX, int sizeY)
    {
        BookList MainLibrary = new BookList();
        this.mainMenu();
    }

    public void mainMenu()
    {
        string userChoice = Console.ReadLine();
        switch  (userChoice)
        {
            case "1":

                break;

如何在案例1中调用MainLibrary的函数。显然这里没有实例化但是我找不到对未实例化对象的传递方式。

书目代码:

class BookList
{
    List<Book> books = new List<Book>();       
    public void addBook() // adding book
    {
        string name = Console.ReadLine();
        string author = Console.ReadLine();
        int relDate = int.Parse(Console.ReadLine());
        int uID = books.Count() + 1;
        books.Add(new Book(name, author, relDate, uID));
    }
    public void deleteBook() // deleting book
    {
        int readForDelete = int.Parse(Console.ReadLine());
        books.RemoveAll(Book => Book.UniqueID == readForDelete);
    }
    public void borrowBook() // borrowing book
    {
        int readForBorrow = int.Parse(Console.ReadLine());
        foreach (Book Book in books)
        {
            if (Book.UniqueID == readForBorrow)
                Book.available = false;
        }
    }
    public void returnBook() // returning book
    {
        int readForReturn = int.Parse(Console.ReadLine());
        foreach (Book Book in books)
        {
            if (Book.UniqueID == readForReturn)
                Book.available = true;
        }
    }
    public void displayBooks() // displaying list of books
    {
        string isAvailable;
        foreach (Book Book in books)
        {
            if (Book.available == true)
                isAvailable = "available";
            else
                isAvailable = "unavailable";
            Console.WriteLine(" {0} {1} {2} {3} {4} ", Book.UniqueID, Book.BookName, Book.BookName, Book.ReleaseDate, isAvailable);
        }
    }
    public string Name
    {
        get; set;
    }
}

}

3 个答案:

答案 0 :(得分:3)

您需要将BookList实例传递给mainMenu()方法,或设置全局变量。我推荐前者。

public void mainMenu(BookList list) { ... }

然后从startLibrary

这样调用它
this.mainMenu(MainLibrary);

作为旁注,你真的需要brush up on your variable, class, and method naming conventions

答案 1 :(得分:1)

你可以用lambda这样实现它:

class Window
{
    public void startLibrary(int sizeX, int sizeY)
    {
        BookList MainLibrary = new BookList();
        Action<BookList> action = this.mainMenu();
        if (action != null)
        {
            action(MainLibrary);
        }
    }

    public Action<BookList> mainMenu()
    {
        string userChoice = Console.ReadLine();
        switch (userChoice)
        {
            case "1":
                return b => b.addBook();
        }

或者您可以将对MainLibrary变量的引用传递给方法:

class Window
{
    public void startLibrary(int sizeX, int sizeY)
    {
        BookList MainLibrary = new BookList();
        this.mainMenu(MainLibrary);
    }

    public Action<BookList> mainMenu(BookList b)
    {
        string userChoice = Console.ReadLine();
        switch (userChoice)
        {
            case "1":
                b.addBook();
                break;
        }

答案 2 :(得分:0)

首先,你在startLibrary方法中得到的是一个局部变量 - 只能在它定义的块中访问。也就是说,MainLibrary不是你可以从其他任何地方引用的东西。 要解决您的问题,您有几种选择。 您可以使MainLibrary成为Window类的字段/成员。然后Window类的所有实例方法都可以访问它 - 所以作为MainMenu方法:

class Window
{
    private BookList MainLibrary;

    public void startLibrary(int sizeX, int sizeY)
    {
        this.MainLibrary = new BookList();
        this.mainMenu();
    }

    public void mainMenu()
    {
        string userChoice = Console.ReadLine();
        switch  (userChoice)
        {
            case "1":
                // do whatever you want with this.MainLibrary here
                break;
            /// ...
        }
    }
}

另一个选项是将您创建的MainLIbrary实例传入mainMenu方法。在这里,您需要更新方法的签名,如下所示:

public void mainMenu(BookList list)
{
    // reference the list now as necessary
}