C# – Inaccessible due protection level

c

Here is yet another problem which I would like some explanation on. On line 47 I am getting errors saying:

Error CS0122: 'PROPERTY NAMES' is inaccessible due to its protection level (CS0122)

The question is why is there an error, and how can I avoid this? And if I'm doing this homework correctly. And if I should be calling properties or variables? My guess is for properties.

PS. Code isn't finished yet.

NOTE! My professor gave us class diagram telling us what properties should be in place and which should have get/set and which just set.

Problem with code:

    public void display()
    {
        Console.Write("{0}\n{1}\n{2}\n{3}", Title, getAuthorName(), PublisherName, Price);
    }

Whole code:

using System;

namespace Lab_3
{
    class BookTest
    {
        static void Main(string[] args)
        {
            Book book1 = new Book();
            Book book2 = new Book("Advenced C#", "Joe", "Robertson", 29.99f, "PUC Press");
        }
    }

    public class Book
    {
        string authorFirstName;
        string authorLastName;
        float price;
        string publisherName;
        string title;

        public Book()
        {
            Console.Write("Enter book title: ");
            Title = Console.ReadLine();
            Console.Write("Enter author's first name: ");
            AuthorFirstName = Console.ReadLine();
            Console.Write("Enter author's last name: ");
            AuthorLastName = Console.ReadLine();
            Console.Write("Enter price: ");
            Price = float.Parse(Console.ReadLine());
            Console.Write("Enter publisher's name: ");
            PublisherName = Console.ReadLine();
        }

        public Book(string bookTitle, string firstName, string lastName, float bookPrice, string publisher)
        {
            authorFirstName = firstName;
            authorLastName = lastName;
            price = bookPrice;
            publisherName = publisher;
            title = bookTitle;
        }

        public void display()
        {
            Console.Write("{0}\n{1}\n{2}\n{3}", Title, getAuthorName, PublisherName, Price);
        }

        public string getAuthorName()
        {
            return AuthorFirstName + AuthorLastName;
        }

        public string AuthorFirstName
        {
            get
            {
                return authorFirstName;
            }
            set
            {
                authorFirstName = value;
            }
        }

        public string AuthorLastName
        {
            get
            {
                return authorLastName;
            }
            set
            {
                authorLastName = value;
            }
        }

        public float Price
        {
            set
            {
                price = value;
            }
        }

        public string PublisherName
        {
            set
            {
                publisherName = value;
            }
        }

        public string Title
        {
            set
            {
                title = value;
            }
        }

    }
}

EDIT: Solved! Thank you all for help.

In conclusion I CAN'T use properties because some are READ-ONLY which caused me problems. So I needed to use private variables to display them.

Best Answer

Since you're in the class where the method display() is in don' event bother using the Properties, just use the data members themselves since this is probably what your professor wanted. Properties are used to expose the data to users of your class. In your class you are free to use the data members without needed the getters or setters:

Console.Write("{0}\n{1}\n{2}\n{3}", title, getAuthorName(), publisherName, price);
Related Topic