C
C#14mo 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
Buddy14mo 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
Buddy14mo ago
- private: The type or member can be accessed only by code in the same class or struct.
Ronnie
Ronnie14mo ago
thank you, ive read it but didnt find an answer
Buddy
Buddy14mo ago
Just make the property to public
Ronnie
Ronnie14mo ago
its a requirement for the assignment unfortunately : - (
Buddy
Buddy14mo 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
Ronnie14mo 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
Buddy14mo ago
Book is not in the same class which means Titel is not accessible, because it is private.
Ronnie
Ronnie14mo 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
Ronnie14mo ago
I get it to run this far, but still cant target the varjeBok.Titel
Pobiega
Pobiega14mo ago
can you show the assignment so we know the actual requirements swedish is fine
Ronnie
Ronnie14mo 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
More Posts