简体   繁体   English

无法获得应有的保护级别

[英]Inaccessible due protection level

Here is yet another problem which I would like some explanation on. 这是另一个我想要解释的问题。 On line 47 I am getting errors saying: 在第47行,我收到错误说:

Error CS0122: 'PROPERTY NAMES' is inaccessible due to its protection level (CS0122) 错误CS0122:由于其保护级别(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. 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. 我的教授给了我们一个类图,告诉我们应该使用哪些属性,哪些应该具有get / 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. 总之,我不能使用属性,因为有些是READ-ONLY导致我的问题。 So I needed to use private variables to display them. 所以我需要使用私有变量来显示它们。

The issue is that your properties lack a getter , ie 问题是你的属性缺乏getter ,即

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

EDIT: Your display() method should read as follows: 编辑:您的display()方法应如下所示:

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. 注意为arg#3调用getAuthorName()方法。

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. 由于你在使用属性的方法display()所在的类中,只需使用数据成员本身,因为这可能是你教授想要的。 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: 在您的课程中,您可以自由使用数据成员而无需getter或setter:

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

Price, PublisherName, and Title do not have getters, only setters. Price,PublisherName和Title没有getter,只有setter。

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. 它不起作用的原因是因为属性Price,PublisherName和Title没有getter,它们只有setter。 Maybe he was asking to have those properties read-only, instead of write-only? 也许他要求将这些属性设为只读,而不是只写?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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