2
Answers

NP111 function of “this”

Maha

Maha

16y
2.2k
1

Hi Guys

 

NP111 function of “this

 

http://www.aspfree.com/c/a/C-Sharp/C-Events-Explained/2/

 

Following program is in the above website. Even though this (highlighted in green in the program) is not there that means   

 

public Book Book

{

get { return book; }

}

 

program is producing similar result. Please explain significant of this.

 

 

Thank you

 

using System;

using System.Collections;

 

namespace Events

{

    public delegate void AddBookEventHandler(object source, BookEventArgs e);

 

    public class Book

    {

        private string title;

        private string isbn;

        private decimal price;

 

        public Book(string title, string isbn, decimal price)

        {

            this.title = title;

            this.isbn = isbn;

            this.price = price;

        }

 

        public string Title

        {

            get { return this.title; }

        }

        public string ISBN

        {

            get { return this.isbn; }

        }

        public decimal Price

        {

            get { return this.price; }

        }

    }

 

    public class BookShop

    {

        private ArrayList books;

        private string name;

        public event AddBookEventHandler AddBook;

 

        public BookShop(string name)

        {

            books = new ArrayList();

            this.name = name;

        }

 

        protected void OnAddBook(BookEventArgs e)

        {

            if (AddBook != null)

                AddBook(this, e);

        }

 

        public void AddNewBook(Book book)

        {

            this.books.Add(book);

            BookEventArgs e = new BookEventArgs(book);

            OnAddBook(e);

        }

    }

 

    public class BookEventArgs : EventArgs

    {

        private Book book;

 

        public BookEventArgs(Book book)

        {

            this.book = book;

        }

 

        public Book Book

        {

            get { return this.book; }

        }

    }

 

    public class Notifier

    {

        public void instance_AddBook(object source, BookEventArgs e)

        {

            Console.WriteLine("A Message has been sent to the Members\n" + " regarding the book '{0}' with the ISBN '{1}'\n", e.Book.Title, e.Book.ISBN);

        }

    }

 

    //The Sys Class is the application that uses all the above classes.

    //Let's look at the code:

 

    public class Sys

    {

        public static void Main()

        {

            BookShop bookShop = new BookShop(".NET Bookshop");

 

            Book b1 = new Book("The Right Way", "123456789", 19.90m);

            Book b2 = new Book("The .NET Stuff", "987654321", 29.90m);

 

            Notifier notify = new Notifier();

 

            bookShop.AddBook += new

              AddBookEventHandler(notify.instance_AddBook);

 

            bookShop.AddNewBook(b1);

            bookShop.AddNewBook(b2);

            Console.ReadLine();

        }

    }

}

/*

A Message has been sent to the Members

 regarding the book 'The Right Way' with the ISBN '123456789'

 

A Message has been sent to the Members

 regarding the book 'The .NET Stuff' with the ISBN '987654321'

*/

Answers (2)