C
C#•2y ago
Ronnie

Private list problems

Hello, I'm trying to search for a Title, which is located in varjeBok.Titel in the list:
private List<Book> allBooks = new List<Book>();
private List<Book> allBooks = new List<Book>();
But the list is Private (im inside the same class with the search method) Its giving me the error, "Book.Titel is inaccessible due ro its protection level" How come I can access the list but not the Title of the object? How do i fix
12 Replies
Buddy
Buddy•2y ago
Access Modifiers - C# Programming Guide
All types and type members in C# have an accessibility level which controls whether they can be used from other code. Review this list of access modifiers.
Buddy
Buddy•2y ago
- private: The type or member can be accessed only by code in the same class or struct.
Ronnie
RonnieOP•2y ago
thank you, ive read it but didnt find an answer
Buddy
Buddy•2y ago
Just make the property to public
Ronnie
RonnieOP•2y ago
its a requirement for the assignment unfortunately : - (
Buddy
Buddy•2y ago
As it says.
The type or member can be accessed only by code in the same class or struct.
Which means you cannot access private types / members outside of the same class / struct
Ronnie
RonnieOP•2y ago
I am in the same class and i can access the list, but the problem is that I cant specify what element of the object
Buddy
Buddy•2y ago
Book is not in the same class which means Titel is not accessible, because it is private.
Ronnie
RonnieOP•2y ago
aaaaaaaaaaaaaaaaaaaaaaaaaah My private list is in the class Librarian, but the object Book that is saved in the list is in another class so thats why? I'm not sure how to fix this though.. Do i need to add something somewhere?
Ronnie
RonnieOP•2y ago
I get it to run this far, but still cant target the varjeBok.Titel
Pobiega
Pobiega•2y ago
can you show the assignment so we know the actual requirements swedish is fine
Ronnie
RonnieOP•2y ago
Thank you, I somehow got it to work with a
public string GetTitle()
{
return Titel.ToString();
}
public string GetTitle()
{
return Titel.ToString();
}
inside the book-class So ill close this 🙂
Want results from more Discord servers?
Add your server