Giorgi Aptsiauri
Giorgi Aptsiauri

Reputation: 355

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: My error

Upvotes: 0

Views: 233

Answers (3)

Allan
Allan

Reputation: 7

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.

Upvotes: 0

styx
styx

Reputation: 1925

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"));
}

Upvotes: 1

aman girma
aman girma

Reputation: 694

you need to put

Books.Add(Book1);

in a method not in the class.

Upvotes: 0

Related Questions