1

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.

kapa
  • 77,694
  • 21
  • 158
  • 175
HelpNeeder
  • 6,383
  • 24
  • 91
  • 155

4 Answers4

2

The issue is that your properties lack a getter, i.e.

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.

RoccoC5
  • 4,185
  • 16
  • 20
  • So I should use variables instead? – HelpNeeder Sep 12 '11 at 23:00
  • @HelpNeeder - you can change your `display` method to refer to the private member variables instead: `Console.Write("{0}\n{1}\n{2}\n{3}", title, getAuthorName(), publisherName, price);`. Also not that `getAuthorName` is a method, so the proper syntax to invoke it is `getAuthorName()`. – RoccoC5 Sep 12 '11 at 23:03
  • If so, I get error: Error CS1503: Argument `#3' cannot convert `method group' expression to type `object' (CS1503) – HelpNeeder Sep 12 '11 at 23:06
  • @HelpNeeder - this is because `getAuthorName` is a method. You must invoke it to pass its return value to Console.Write. See my previous comment. – RoccoC5 Sep 12 '11 at 23:07
  • Look at tkeE2036 answer and comments. I get the error when I use variables. – HelpNeeder Sep 12 '11 at 23:09
  • 1
    @HelpNeeder - Modify your `display()` method per my edited answer. – RoccoC5 Sep 12 '11 at 23:13
2

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);
Ian Dallas
  • 12,451
  • 19
  • 58
  • 82
1

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
Kyle W
  • 3,702
  • 20
  • 32
  • Professor gave us VS class diagram formatted this way. My guess is to follow it. Properties are set as we were asked. – HelpNeeder Sep 12 '11 at 22:54
  • Updated my post with that info. – HelpNeeder Sep 12 '11 at 22:58
  • You can either add private setters, or access the backing variables themselves, but you can't access it as it is, because they don't exist. – Kyle W Sep 12 '11 at 22:58
  • Ok, well I need to keep properties as they are. So should I use variable names instead? Although I get the same problem. – HelpNeeder Sep 12 '11 at 23:02
  • Ok, but I still need to use getAuthorName method. Then I get error: Error CS1502: The best overloaded method match for `System.Console.Write(string, params object[])' has some invalid arguments (CS1502) AND Error CS1503: Argument `#3' cannot convert `method group' expression to type `object' (CS1503) – HelpNeeder Sep 12 '11 at 23:13
-1

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?