简体   繁体   中英

Inaccessible due protection level

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.

The issue is that your properties lack a getter , ie

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

EDIT: Your display() method should read as follows:

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

Note the invocation of the getAuthorName() method for arg #3.

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

Price, PublisherName, and Title do not have getters, only setters.

public string Title { private get; set; } // Private getter, public setter
public string Title { set { title=value; }} // No getter, access...
public void Display() { Console.WriteLine(title); }//not Title, use lowercase member variable

The reason it's not working is because the properties Price, PublisherName and Title don't have getters, they only have setters. Maybe he was asking to have those properties read-only, instead of write-only?

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