简体   繁体   中英

How do I manipulate class objects from my WinForms form event handlers?

This question may seem trivial but I'm still having problems because I am a dummy. So, I am creating a Book Store app with Windows Forms.

I have created a separate class for Book. What I want to do with this Book class is the following: create Book objects, add them to a List; then, I will need to access this list's Book objects from the event handler methods. It seems like I am having problems even with adding the Book object to the list. Can you give me the direction how I should organize this kind of code?

Here are the two codes:

form1.cs:

using System.Collections.Generic;
using System.Windows.Forms;

namespace BookStore
{
    public partial class BookStoreForm : Form
    {
        List<Book> Books = new List<Book>();
        Book Book1 = new Book("Author", "ISBN", 5, "Title");
       // Books.Add(Book1);

        public BookStoreForm()
        {
            InitializeComponent();
        }


    }
}

book.cs:

namespace BookStore
{
    public class Book
    {
        public string Author { get; set; }
        public string ISBN { get; set; }
        public decimal Price { get; set; }
        public string Title { get; set; }

        public Book() { }

        public Book(string Author, string ISBN, decimal Price, string Title)
        {
            this.Author = Author;
            this.ISBN = ISBN;
            this.Price = Price;
            this.Title = Title;
        }

    }
}

This is what happens when I create a Book object and then try to add it to the list: 我的错误

You cant write code outside of a function(besides declaring properties)

Move you code the constructor and it will work(or some other function of your choosing)

Like so

private List<Book> Books; // this will be accessible from anywhere in you form

public BookStoreForm()
{
      InitializeComponent();
      Books = new List<Book>();
      Book Book1 = new Book("Author", "ISBN", 5, "Title");
      Books.Add(Book1);

}

private void myEvnetHandler(object sender, EventArgs e)
{
     Books.Add(new Book("Stephen R. Davis", "0764508148", 12.45m, "C# For Dummies"));
}

you need to put

Books.Add(Book1);

in a method not in the class.

One way to achieve what you are trying to do is to declare your object globally in the class and instance it inside a method.

using System.Collections.Generic;
using System.Windows.Forms;

namespace BookStore
   {
      public partial class BookStoreForm : Form
         {
              List<Book> Books;
              Book Book1;
           // Books.Add(Book1);

              public BookStoreForm()
                 {
                     InitializeComponent();
                     Books = new List<Book>();
                     Book1 = new Book("Author", "ISBN", 5, "Title");
                 }
         }
    }

now your object has global scope and can be accessed from inside any function in the class.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM